-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
233 lines (178 loc) · 6.49 KB
/
main.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
import urllib.request
from bs4 import BeautifulSoup
import json
import geocoder
import threading
class StarThread(threading.Thread):
"""
Start a new thread to collect the data from either the
scraper or the API.
"""
def __init__(self, threadID, name, stargazers, data_file, use_api):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.stargazers = stargazers
self.data_file = data_file
self.use_api = use_api
def run(self):
print ("Starting " + self.name)
store_in_json(self.stargazers, self.data_file, self.use_api)
print ("Exiting " + self.name)
def scrape_stargazers(url):
"""
Scrape the stargazers page of requested repo.
e.g: http://github.com/kivy/plyer/stargazers?page=1
http://github.com/kivy/plyer/stargazers?page=2
returns the untire valud users list.
"""
page_count = 1
stargazers = []
while page_count>0:
url_ = url.format(page_count)
r = urllib.request.urlopen(url_).read()
soup = BeautifulSoup(r, "lxml")
follow = soup.find_all("h3", class_="follow-list-name")
if follow:
for human in follow:
h = human.a['href'].split("/")[1]
stargazers.append(h)
page_count += 1
else:
page_count = -1
return stargazers
# Get user data from API.
def get_user_profile(user_name):
"""
Use the GitHub's user API to get the user location and id.
"""
url_ = "https://api.github.com/users/{}".format(user_name)
r = urllib.request.urlopen(url_).read()
result = json.loads(r)
return result['location'], result['id']
# Scraper for user Profile.
def get_user_id(soup):
"""
Returns the user id via the scraper.
"""
follow = soup.find("meta", {"name": "octolytics-dimension-user_id"})["content"]
return int(follow)
def get_user_country(soup):
"""
Returns the location of the user via the scraper.
"""
follow = soup.find("li", itemprop="homeLocation")
if not follow:
country = "N/A"
else:
country = (follow.get_text())
return str((country.strip()))
def scrape_profile(url):
"""
Scrape the user's profile to get user location and id.
"""
r = urllib.request.urlopen(url).read()
soup = BeautifulSoup(r, "lxml")
user_country = get_user_country(soup)
user_id = get_user_id(soup)
return user_country, user_id
def chunk_stargazers(seq, num):
"""
Divide the huge User set into smaller lists.
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print (chunk_stargazers(l, 3))
print (chunk_stargazers(l, 2))
output:
[[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]
"""
avg = len(seq) / float(num)
out = []
last = 0.0
while last < len(seq):
out.append(seq[int(last):int(last + avg)])
last += avg
return out
def store_in_json(stargazers, data_file, use_api):
"""
Store the user data in a a json file in folder jsons/.
eg: jsons/data_1.json
"""
for user in stargazers:
if use_api:
user_country, user_id = get_user_profile(user)
else:
url = "https://github.com/{}".format(user)
user_country, user_id = scrape_profile(url)
if user_country == "N/A":
# If no information is available about the user
# location, just ignore it.
pass
else:
user_country = geocoder.google(user_country).country_long
if user_country:
#user_country, user_id = get_user_profile(url)
user_ = {"user": user_id,
"country": user_country}
try:
outfile = open('jsons/{}.json'.format(data_file), 'r')
data = json.load(outfile)
outfile.close()
data["users"].append(user_)
with open('jsons/{}.json'.format(data_file), 'w') as outfile:
json.dump(data, outfile, indent=4)
print ("Added user from " + user_country)
outfile.close()
except:
users = []
users.append(user_)
map_data = {"users": users}
with open('jsons/{}.json'.format(data_file), 'w') as outfile:
json.dump(map_data, outfile, indent=4)
outfile.close()
#map_data = {"users": users}
print ("Part 2/2 complete.")
print ("Done. Check the data.json file.")
#with open('jsons/{}.json'.format(data_file), 'w') as outfile:
# json.dump(map_data, outfile, indent=4)
#outfile.close()
if __name__ == "__main__":
"""
python3 main.py https://github.com/kivy/plyer 10 10
url = https://github.com/kivy/plyer
Number of chunks of the entire user set = 10
Number of threads working on I/O and requests = 10
user api or scraper = 1
If use api is set to 1 then it will use GitHub's official API
to get the user data, else it will use the scraper.
"""
import sys
url = str(sys.argv[1])
number_of_threads = int(sys.argv[2])
use_api = int(sys.argv[3])
read_from_stargazer_json = int(sys.argv[4])
star = "/stargazers?page={}"
if read_from_stargazer_json:
with open("stargazers.json", "r") as stargazers_file:
stargazers = json.load(stargazers_file)
stargazers_file.close()
else:
print ("Collecting users ...")
stargazers = scrape_stargazers(url+star)
with open('stargazers.json', 'w') as outfile:
json.dump(stargazers, outfile, indent=4)
outfile.close()
chunked_data = chunk_stargazers(stargazers, number_of_threads)
if chunked_data:
print ("Made {} chunks of entire user set.".format(number_of_threads))
print ("Part 1/2 complete.")
print ("Working on collecting user data ....")
thread_list = {}
print ("Creating {} threads ...".format(number_of_threads))
for i in range(number_of_threads):
thread_list["thread{}".format(i)] = StarThread(i, "Thread-{}".format(i),
chunked_data[i], "data_{}".format(i),
use_api)
thread_list["thread{}".format(i)].start()
for i in range(number_of_threads):
thread_list["thread{}".format(i)].join()