-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcrawler.py
484 lines (379 loc) · 15.5 KB
/
crawler.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
'''
Created on Jul 4, 2015
@author: Tommi Unruh
'''
import sys
import re
import os
import shutil
from github.session import Session as GithubSession
from github.repository_list import RepositoryList
from github.exceptions import RatelimitExceededException
import signal
from github.oauthManager import *
import errno
from github.data_manager import DataManager
from time import sleep
from threading import Thread
class Crawler(object):
'''
classdocs
'''
# constants
FILE_AUTHENTICATION = "authentication"
LINK_API = "https://api.github.com"
LINK_REPO_API = LINK_API + "/repositories"
LINK_SEARCH_API = LINK_API + "/search/repositories"
LINK_RATE_LIMIT = LINK_API + "/rate_limit"
HEADER_USER_AGENT = None
HEADER_XRATELIMIT_LIMIT = "X-RateLimit-Limit"
HEADER_XRATELIMIT_REMAINING = "X-RateLimit-Remaining"
KEY_NEXT = "next"
KEY_SINCE = "since"
KEY_COUNT = "count"
KEY_START = "start"
KEY_CLONE_URL = "clone_url"
KEY_RL_REMAIN = "X-RateLimit-Remaining"
KEY_STATUS_CODE = "status_code"
KEY_CRAWLED_LINKS = "crawled_links"
# GitHub Session object
s = None
def __init__(self, file_path):
'''
Constructor
'''
# DataManager handles file reading/writing.
self.datamanager = DataManager()
# Get OAuth from file 'authentication'.
auth_file = file_path
auth_manager = OAuthManager(filename=auth_file)
auth = None
try:
auth = auth_manager.getAuthData()
except (AuthFileNotFoundException, AuthException):
# Authentication file not found or malformatted. Recreate it.
auth = self.initiateAuthCreation(auth_manager)
except NoCredentialsException:
oauth = None
user_agent = None
if auth:
oauth = auth[auth_manager.KEY_OAUTH]
user_agent = auth[auth_manager.KEY_USER_AGENT]
self.OAUTH = oauth
self.HEADER_USER_AGENT = user_agent
self.HEADERS = {
'User-Agent': self.HEADER_USER_AGENT,
'Authorization': "token %s" % self.OAUTH,
}
# Setup authentication and settings
self.s = GithubSession(self.OAUTH, self.HEADER_USER_AGENT)
def initiateAuthCreation(self, auth_manager):
try:
auth_manager.createAuth()
auth = auth_manager.getAuthData()
print "Authentication process done. Continuing..."
except OAuthCreationException:
# OAuth error. Maybe the OAuth token could not be created, because
# it already exists.
print (
"OAuth error. Maybe authentication file could not be written "
"because of missing write-privilege."
)
sys.exit()
return auth
def crawlReposWUpdate(self, data_filename):
self.crawlRepos(data_filename, skip=False)
def crawlRepos(self, file_links, skip=True, _filter=None):
current_ratelimit = self.getRateLimit()["core"]["remaining"]
if current_ratelimit == 0:
self.endExecution()
url = None
copy_only = False
file_links_backup = ""
# Filehandle for writing.
fw = None
f_links = None
TEXT_PROCESSING = "Processing contents of file: "
# If a links file already exists from earlier crawls, then parse it.
if os.path.isfile(file_links):
print "File '%s' exists already. Will be appending to it." % (file_links)
file_links_backup = file_links + "_backup"
def restoreBackup(signum, frame):
"""
Inner function: Restore original file from backup upon
termination in backup process.
"""
msg = "Got exit signal. Restoring original file from backup..."
print "\n%s\r" % (msg),
if fw:
fw.close()
if f_links:
f_links.close()
# Copy backup file back.
shutil.copyfile(file_links_backup, file_links)
print "%s Done." % (msg)
sys.exit()
# Catch process-kill signal.
signal.signal(signal.SIGTERM, restoreBackup)
# Also catch Ctrl-C/D.
signal.signal(signal.SIGINT, restoreBackup)
os.rename(file_links, file_links_backup)
f_links = open(file_links_backup, 'r')
if skip:
# We do not want to recrawl old data, so
# just copy-paste it.
shutil.copyfile(file_links_backup, file_links)
# Open fh for writing.
fw = open(file_links, 'a')
print TEXT_PROCESSING + str(file_links) + "..."
sys.stdout.flush()
if skip:
# We do not want to recrawl old data.
# Therefore, get the last next-link from the old data,
# so that we can continue crawling from there.
data = self.datamanager.getDataLikeTail(file_links,
1, stepsize=65)
url = self.datamanager.extractNextURL(data)
else:
old_data = f_links
etag = None
repos = None
next_url = None
file_pos = None
# Parse old data if skip was not specified.
while 1 and not skip:
try:
file_pos = old_data.tell()
parsed_data = self.datamanager.parseNextBlock(old_data)
if parsed_data:
_repos, url, etag, next_url = parsed_data
repos = RepositoryList(
url, etag, repos=_repos,
next_url=next_url
)
if not skip:
try:
# Update data, by requesting Github API.
self.nextBackupCrawl(fw, repos,
copy_only=copy_only,
_filter=_filter)
except RatelimitExceededException:
# No ratelimit remaining, continue
# to only copy the old data and finish.
copy_only = True
# We finished parsing the old data.
else:
break
# Encountered malformatted block, probably because
# the original data file was cut/edited.
# Rewind the file position and skip one line.
except IOError as err:
old_data.seek(file_pos, os.SEEK_SET)
old_data.readline()
print err, " Skipping this line!"
if repos:
url = repos.getNextURL()
# Remove backup signal handlers.
# SIG_DFL is the standard signal handle for any signal.
signal.signal(signal.SIGTERM, signal.SIG_DFL)
signal.signal(signal.SIGINT, signal.SIG_DFL)
print "Done parsing old data."
if copy_only:
self.endExecution()
repos = None
try:
# Parsing finished or no backup file found. Start crawling new data.
if not fw:
# There was no backup file
fw = open(file_links, 'a')
if not url:
# We do not have a URL to start form yet.
# Start crawling from the beginning.
repos = self.nextCrawl(fw, _filter=_filter)
url = repos.getNextURL()
# Parse until ratelimit is reached.
while url:
# Crawl next page
repos = self.nextCrawl(fw, url=url, _filter=_filter)
url = repos.getNextURL()
fw.close()
except RatelimitExceededException:
self.endExecution()
def nextBackupCrawl(self, fh, repository_list,
copy_only=False, _filter=None):
"""
Get up-to-date data for already crawled repositories.
If 'copy_only' is specified, we only copy old data from
the backup file to not lose any already crawled data.
"""
result = None
if not copy_only:
# We do not want to simply copy the old data -
# check for an update.
print "Updating from: %s" % repository_list.getURL()
result = self.s.update(repository_list)
if result:
print "Found update!"
if _filter:
# Filter results
repository_list.filter(self.s, self.DEFAULT_REPO_FILTER)
self.datamanager.writeRepositoryList(fh, repository_list)
return result
def nextCrawl(self, fh, url=None, _filter=None):
"""
Crawl repositories from GitHub.
'url' is used to specify the next parse-URL.
"""
result = None
_format = "Crawling: %s"
# Setup visual feedback thread.
visual_feedback = visualCrawlingFeedback()
if url:
_format = _format % url
sys.stdout.write(_format + "\r")
sys.stdout.flush()
visual_feedback.setMsg(_format)
visual_feedback.start()
result = self.s.getRepos(url=url)
else:
_format = _format % "From beginning."
sys.stdout.write(_format + "\r")
sys.stdout.flush()
visual_feedback.setMsg(_format)
visual_feedback.start()
result = self.s.getRepos()
if _filter:
# Filter results
result.filter(self.s, _filter)
# Write new results from Github.
self.datamanager.writeRepositoryList(fh, result)
visual_feedback.stopFeedback()
print visual_feedback.getMsg() + "Saved to file."
return result
@staticmethod
def getKeyFromCrawlData(input_file, output_file,
keys=KEY_CLONE_URL):
"""
Extract the value for 'key' from every crawled repository in file
'input_file'.
Output is redirected into 'output_file'.
"""
DataManager.getKeysFromCrawlData(input_file, output_file, keys)
@staticmethod
def extractReposFiltered(input_file, output_file,
_filter=None):
"""
Extract any repository from 'input_file' that matches 'filter',
into 'output_file'.
"""
DataManager.extractReposFiltered(input_file, output_file, _filter)
def endExecution(self):
print "Ratelimit reached. Quitting..."
sys.exit()
def getNextURL(self, _dict, next_link=None):
"""
Find the URL in _dict and return it.
Empty string if it does not exist.
'next_link' can be used to specify an alternative if there is no
link in _dict.
"""
if self.KEY_NEXT_URL in _dict:
return _dict[self.KEY_NEXT_URL]
else:
if next_link:
return next_link
else:
return ""
def search(self, q="language:PHP", sort=None, order=None):
"""
Search GitHub for 'q'.
Any search is limited to 1000 results.
"""
# Could yield problems, because no deep copy is done.
# TODO: (maybe)
resp = r.get(self.addOAuth(self.LINK_SEARCH_API + "?q=" + q),
headers=self.HEADERS)
decoded = json.loads(resp.text)
for _dict in decoded["items"]:
print _dict["clone_url"]
return decoded
def getRateLimit(self):
return self.s.getRatelimit()
def addOAuth(self, url):
"""
Add the OAuth get-parameter to the specified 'url'.
"""
token_query = "access_token=" + self.OAUTH
if url.find('?') != -1:
url += "&" + token_query
else:
url += "?" + token_query
return url
### LEGACY CODE
### ~~~~~~~~~~~
def crawlSearchDays(self, start, end, q="langauge:PHP", sort=None, order=None):
"""
Crawl the clone urls for the search query 'q'.
However, the query will be modified to only show results of
a certain day.
This will be repeated until each day in [start, end] was queried.
Therefore, 'start' and 'end' have to be dates of format YYYY-MM-DD.
Some days may be skipped due to different length of months.
"""
# Check start and end format first.
r = re.compile('^[0-9]{4}-[0-9]{2}-[0-9]{2}$')
if not r.match(start) or not r.match(end):
# 'start' or 'end' have a wrong format.
print (
"'start' and 'end' are expected to be of format YYYY-MM-DD."
"'%s' and '%s' were given." % (start, end)
)
return -1
else:
# Parameters are ok, continue
pass
def crawlSearching(self, q="language:PHP", sort=None, order=None):
"""
Crawl the clone urls for the search query 'q'.
The response is split into 10 URLs with 100 repositories each.
"""
per_page = 100
page = 0
for page in range(1, 11):
resp = self.search(q + "&per_page=" + str(per_page) +
"&page=" + str(page))
# Check if the response was empty, so that we can reduce
# the load on the GitHub API servers.
if not resp["items"]:
break
class visualCrawlingFeedback(Thread):
def __init__(self):
super(visualCrawlingFeedback, self).__init__()
self.done = False
# Set every new thread to a 'daemon'-thread, so that it is killed
# upon exiting parent, i.e. in case of CTRL-C.
self.daemon = True
def run(self):
counter = 0
self.msg += "."
sys.stdout.write(self.msg + "\r")
sys.stdout.flush()
sleep(1)
while not self.done:
if counter < 3:
self.msg += "."
counter += 1
else:
self.msg = self.msg[:-3] + " "
counter = 0
sys.stdout.write(self.msg + "\r")
sys.stdout.flush()
if counter == 0:
self.msg = self.msg[:-3]
sleep(1)
def setMsg(self, msg):
self.msg = msg
def stopFeedback(self):
self.done = True
def getMsg(self):
return self.msg