-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetchProjects.py
96 lines (79 loc) · 2.44 KB
/
fetchProjects.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
import json
import requests
import time
from requests.auth import HTTPBasicAuth
PER_PAGE_COUNT = 100
LANGUAGE = 'python'
GITHUB_MAX_RESULTS = 1000
INITIAL_STAR_COUNT = 30
REQUEST_LIMIT = 30
MAX_NEEDED_RESULTS = 5000
requestCounter = 0
credsFile = open("../github-creds.json")
creds = json.load(credsFile)
username = creds["username"]
password = creds["password"]
credsFile.close()
def pageResultsFetcher(starCountOp, starCount, pageNum):
global requestCounter,username,password
queryDict = {
'language' : LANGUAGE,
'stars' : starCountOp+str(starCount)
}
def querySerializer(qDict):
queryChunks = ''
for key, value in qDict.items():
queryChunks += ("%s:%s" % (key, value))+' '
return queryChunks.rstrip()
paramDict = {
'q' : querySerializer(queryDict),
'per_page' : str(PER_PAGE_COUNT),
'sort' : 'stars',
'order' : 'desc',
'page' : pageNum
}
if(requestCounter == REQUEST_LIMIT):
requestCounter = 0
time.sleep(60)
requestCounter += 1
r = requests.get('https://api.github.com/search/repositories',params=paramDict, auth=HTTPBasicAuth(username,password))
print r.url
if(r.ok):
results = json.loads(r.text)
return results
else:
print "ERROR: Can't fetch results."
print "URL: "+r.url
exit(1)
initialResults = pageResultsFetcher('>=', INITIAL_STAR_COUNT, 1)
print initialResults["incomplete_results"]
HIGHEST_STAR_COUNT = initialResults["items"][0]["watchers_count"]
if(initialResults["total_count"] <= PER_PAGE_COUNT):
dumpProjectMetaData(initialResults["items"])
else:
resultsCounter = PER_PAGE_COUNT
resultsDict = initialResults["items"]
starCount = HIGHEST_STAR_COUNT
resultPageNumber = 2
starCountList = []
blockCounter = PER_PAGE_COUNT
while(resultsCounter < MAX_NEEDED_RESULTS and starCount > INITIAL_STAR_COUNT):
pageResults = pageResultsFetcher('<=', starCount, resultPageNumber)
#print pageResults['incomplete_results']
resultsDict.extend(pageResults["items"])
resultsCounter += len(pageResults["items"])
resultPageNumber += 1
blockCounter += PER_PAGE_COUNT
print resultsCounter
if(blockCounter%GITHUB_MAX_RESULTS == 0):
starCount = int(pageResults["items"][-1]["watchers_count"])
while(resultsDict[-1]["watchers_count"] == starCount):
del resultsDict[-1]
resultsCounter -= 1
starCountList.append(starCount)
resultPageNumber = 1
with open('projects_meta_data.json', 'w') as outfile:
json.dump(resultsDict, outfile)
outfile.close()
print(len(resultsDict))
print starCountList