-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathscrape.py
executable file
·181 lines (153 loc) · 6.4 KB
/
scrape.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
from optparse import OptionParser
import json
import re
import os, errno
import shutil
from datetime import datetime, timedelta
import time
import urllib
from bs4 import BeautifulSoup
# python scrape.py --get scrapes the first page of HN
# python scrape.py --process --combine --upload
# python scrape.py --deploy
# Set up Regexes
RE_NUM = re.compile(r'\d+')
RE_TIME_AGO = re.compile(r'\d+\s\w+?\sago')
# silently remove a file
def silent_remove(filename):
try:
os.remove(filename)
except OSError as e: # this would be "except OSError, e:" before Python 2.6
if e.errno != errno.ENOENT: # errno.ENOENT = no such file or directory
raise # re-raise exception if a different error occured
# Download the first page of HN
def get(filename):
f = open(filename, 'w')
url = 'http://news.ycombinator.com/news'
r = urllib.urlopen(url)
if r.getcode() == 200:
f.write(r.read())
f.close()
print "Wrote the data into file " + filename
else:
print "Could not get HN feed"
print "Status code is:" + r.getcode()
exit()
# Extract info from the HTML
def process(infile, outfile):
f = open(infile, 'r')
soup = BeautifulSoup(f.read())
summary = []
now = time.time()
print 'Current epoch time: %d' % int(now)
# [2:] because first matches with a login thingy
for row in soup.find_all('tr')[2:]:
if len(row.find_all('td')) == 3:
cells = row.find_all('td')
info_row = row.next_sibling
order = cells[0].text
link_data = cells[2]
title = cells[2].find('a').text
url = cells[2].find('a')['href']
domain = cells[2].find('span').text if cells[2].find('span') else ''
points = info_row.find('span').text if info_row.find('span') else ''
user = info_row.find('a').text if info_row.find('a') else ''
num_comments = info_row.find_all('a')[2].text if len(info_row.find_all('a')) > 1 else ''
thread_id = info_row.find_all('a')[1]['href'] if len(info_row.find_all('a')) > 1 else ''
time_ago_str = RE_TIME_AGO.search(info_row.text).group(0)
num, time_type, other = time_ago_str.split(' ')
if 'minute' in time_type:
posted = now - int(num) * 60
elif 'hour' in time_type:
posted = now - int(num) * 60 * 60
elif 'day' in time_type:
posted = now - int(num) * 60 * 60 * 24
else:
print 'Error processing time ago string: %s' % time_ago_str
exit()
thread_type = 'Jobs' if thread_id == '' else 'Other'
data = {'order': RE_NUM.search(order).group(0),
'title': title,
'url': url,
'domain': domain.strip('() '),
'points': int(RE_NUM.search(points).group(0)) if 'point' in points else 0,
'user': user,
'num_comments': int(RE_NUM.search(num_comments).group(0)) if 'comments' in num_comments else 0,
'thread_id': RE_NUM.search(thread_id).group(0) if 'item' in thread_id else '',
'type': thread_type,
'posted_time': int(posted),
}
summary.append(data)
f.close()
print json.dumps(summary, indent=2)
o = open(outfile, 'w')
o.write(json.dumps(summary, indent=2))
o.close()
# Combine the recent files into a big file of data
def combine(now):
recent_24 = now - timedelta(hours=24)
# Combine files
current_data = []
datetime_cnt = recent_24
while datetime_cnt <= now:
print 'Processing data for %s?' % datetime_cnt.strftime('%Y-%m-%d-%H-%M'),
fn = 'hn-data-%s.json' % datetime_cnt.strftime('%Y-%m-%d-%H-%M')
fp = "/var/www/tshn/"
fp += os.path.join('data', fn)
if os.path.exists(fp):
print 'Y'
f = open(fp, 'r')
current_data.extend(json.loads(f.read()))
f.close()
else:
print 'N'
datetime_cnt += timedelta(minutes=15)
print 'Got %d rows' % len(current_data)
fp = "/var/www/tshn/"
fp += os.path.join('data', 'now.json')
f = open(fp, 'w')
f.write(json.dumps(current_data, indent=2))
f.close()
# Save this snapshot if it's midnight so we have a daily history
if now.hour == 0 and now.minute == 0:
fn = 'hn-data-%s.json' % now.strftime('%Y-%m-%d')
shutil.copyfile(os.path.join('data', 'now.json'), os.path.join('data', fn))
# clean previous day's counterpart file
def clean(now):
past = datetime.now() - timedelta(days=1)
past = past.replace(minute=(past.minute / 15) * 15)
filename = "/var/www/tshn"
filename = filename + "/" + os.path.join('data', 'hn-data-%s.html' % past.strftime('%Y-%m-%d-%H-%M'))
filename_json = filename.replace('.html', '.json')
silent_remove(filename)
silent_remove(filename_json)
if __name__ == '__main__':
parser = OptionParser()
parser.add_option("-a", "--all", action="store_true", dest="all", default=None, help="Run the entire script")
parser.add_option("-g", "--get", action="store_true", dest="get", default=None, help="Get most recent data")
parser.add_option("-p", "--process", action="store_true", dest="process", default=None,
help="Process most recent data")
parser.add_option("-c", "--combine", action="store_true", dest="combine", default=None,
help="Combine recent data files")
parser.add_option("-d", "--clean", action="store_true", dest="clean", default=None,
help="Clean previous days data files")
(options, args) = parser.parse_args()
if options.all:
options.get = options.process = options.combine = True
now = datetime.now()
now_15 = now.replace(minute=(now.minute / 15) * 15)
basedir = "/var/www/tshn"
base_filename = basedir + "/" + os.path.join('data', 'hn-data-%s.html' % now_15.strftime('%Y-%m-%d-%H-%M'))
filename_js = base_filename.replace('.html', '.json')
if options.get:
print 'Getting HN data'
get(base_filename)
if options.process:
print 'Processing HN data'
process(base_filename, filename_js)
if options.combine:
print 'Generating a data file'
combine(now_15)
if options.clean:
print 'Removing previous data files'
clean(now_15)