-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_contributors_location.py
377 lines (315 loc) · 11.9 KB
/
get_contributors_location.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
import requests
import json
import os
from concurrent.futures import ThreadPoolExecutor, as_completed
import time
import random
from countries_list import FALLBACK_COUNTRIES as fallback_countries
GITHUB_TOKEN = os.getenv('GITHUB_TOKEN')
GOOGLE_MAPS_API_KEY = os.getenv('GOOGLE_MAPS_API_KEY')
OPENCAGE_API_KEY= os.getenv('OPENCAGE_API_KEY')
NATIONALIZE_API_KEY = os.getenv('NATIONALIZE_API_KEY')
added_countries = {}
def get_random_fallback_location():
return random.choice(fallback_countries)
def predict_country_from_name(name):
name_parts = name.split(' ')
surname = None
if len(name_parts) > 0:
surname = name_parts[len(name_parts) - 1]
else:
return None, None
url = f"https://api.nationalize.io?name={surname}&apikey={NATIONALIZE_API_KEY}"
try:
response = requests.get(url)
if response.status_code == 200:
data = response.json()
if data['country']:
top_country = max(data['country'], key=lambda c: c['probability'])
country_code = top_country['country_id']
return country_code
return None, None
except requests.exceptions.RequestException as e:
return None, None
def get_coordinates_by_country_code(country_code):
country_coordinates = {c['code']: c for c in fallback_countries}
return country_coordinates.get(country_code, None)
def create_fallback_geojson(full_name, properties):
# Try to predict the country from the full name
country_code = predict_country_from_name(full_name)
if country_code:
fallback_location = get_coordinates_by_country_code(country_code)
if fallback_location:
# Change the lat a bit so as points don't fall in the same place
if country_code in added_countries.keys():
added_countries[country_code] += 1
lat = (float(fallback_location['lat']) +
( 0.001 * added_countries[country_code]))
lat = round(lat, 5)
else:
added_countries[country_code] = 1
lat = fallback_location['lat']
return {
"type": "Feature",
"properties": {
"Name": full_name,
"Committer": properties.get("committer", ""),
"Username": properties.get("username", ""),
"Country": fallback_location['country'],
},
"geometry": {
"type": "Point",
"coordinates": [fallback_location["lon"], lat],
}
}
return None
def extract_email_website_github(line):
parts = line.split()
if len(parts) > 2:
third_part = parts[-1].strip('<>')
return third_part
return None
def is_email(s):
return '@' in s
def is_github_account(s):
return 'github.com' in s
# Function to handle rate limits
def handle_rate_limit(response):
if (response.status_code == 403 and
'X-RateLimit-Remaining' in response.headers):
rate_limit_remaining = int(response.headers['X-RateLimit-Remaining'])
if rate_limit_remaining == 0:
reset_time = int(response.headers['X-RateLimit-Reset'])
wait_time = reset_time - int(time.time())
print(f"Rate limit reached, waiting for {wait_time} seconds...")
time.sleep(wait_time)
return True
return False
def search_github_user_by_name(full_name):
url = f"https://api.github.com/search/users?q={full_name}+in:fullname"
headers = {"Authorization": f"token {GITHUB_TOKEN}" \
if GITHUB_TOKEN else None}
while True:
response = requests.get(url, headers=headers)
if handle_rate_limit(response):
continue
if response.status_code == 200:
search_results = response.json()
if search_results["total_count"] > 0:
return search_results["items"][0]
else:
return None
else:
return None
def search_github_user_by_email(email):
url = f"https://api.github.com/search/users?q={email}+in:email"
headers = {"Authorization": f"token {GITHUB_TOKEN}" \
if GITHUB_TOKEN else None}
while True:
response = requests.get(url, headers=headers)
if handle_rate_limit(response):
continue
if response.status_code == 200:
search_results = response.json()
if search_results["total_count"] > 0:
return search_results["items"][0] # Return the first result
else:
return None
else:
return None
# Function to get a user's detailed profile using their username
def get_github_user(username):
url = f"https://api.github.com/users/{username}"
headers = {
"Authorization": f"token {GITHUB_TOKEN}" \
if GITHUB_TOKEN else None
}
while True:
response = requests.get(url, headers=headers)
if handle_rate_limit(response):
continue
if response.status_code == 200:
return response.json()
else:
return None
def geocode_nominatim(location_str):
url = "https://nominatim.openstreetmap.org/search"
params = {"q": location_str, "format": "json", "limit": 1}
try:
response = requests.get(url, params=params)
if response.status_code == 200:
data = response.json()
if data:
return {
"lat": float(data[0]["lat"]),
"lon": float(data[0]["lon"])
}
else:
return None
else:
return None
except requests.exceptions.RequestException as e:
return None
# OpenCage geocoding
def geocode_opencage(location_str):
if not OPENCAGE_API_KEY:
# print("OpenCage API key not provided.")
return None
url = "https://api.opencagedata.com/geocode/v1/json"
params = {"q": location_str, "key": OPENCAGE_API_KEY, "limit": 1}
try:
response = requests.get(url, params=params)
if response.status_code == 200:
data = response.json()
if data and data['results']:
geometry = data['results'][0]['geometry']
return {"lat": geometry['lat'], "lon": geometry['lng']}
else:
return None
else:
return None
except requests.exceptions.RequestException as e:
return None
def geocode_google_maps(location_str):
if not GOOGLE_MAPS_API_KEY:
return None
url = "https://maps.googleapis.com/maps/api/geocode/json"
params = {"address": location_str, "key": GOOGLE_MAPS_API_KEY}
try:
response = requests.get(url, params=params)
if response.status_code == 200:
data = response.json()
if data['results']:
location = data['results'][0]['geometry']['location']
return {"lat": location['lat'], "lon": location['lng']}
else:
return None
else:
return None
except requests.exceptions.RequestException as e:
return None
def geocode_location(location_str, properties):
# Try Nominatim first
location = geocode_nominatim(location_str)
if location:
return create_geojson(location, properties)
location = geocode_opencage(location_str)
if location:
return create_geojson(location, properties)
location = geocode_google_maps(location_str)
if location:
return create_geojson(location, properties)
return None
def create_geojson(location, properties):
return {
"type": "Feature",
"properties": properties,
"geometry": {
"type": "Point",
"coordinates": [location['lon'], location['lat']]
}
}
def location_to_geojson(location_str, properties):
geocoded_location = geocode_location(location_str, properties)
if geocoded_location:
geojson = {
"type": "Feature",
"properties": properties,
"geometry": {
"type": "Point",
"coordinates": [
geocoded_location['lon'],
geocoded_location['lat']
]
}
}
return geojson
else:
return None
# Function to find and return the user's location in GeoJSON format with extra properties
# Main function to find user details and return GeoJSON
def find_user_location_and_commit_details(full_name, third_part):
if is_email(third_part):
user_data = None
elif is_github_account(third_part):
username = third_part.split('github.com/')[-1]
user_data = {"login": username}
else:
user_data = None
if user_data:
username = user_data.get("login")
user_profile = None
if user_profile:
location = user_profile.get("location")
properties = {
"full_name": full_name,
"username": username,
"committer": "Yes",
"first_commit_message": "Initial revision",
"first_commit_date": "06-07-2002", # Example commit info
# "correct_location": True
}
if location:
geojson = location_to_geojson(location, properties)
if geojson:
return geojson
else:
return create_fallback_geojson(full_name, properties)
else:
return create_fallback_geojson(full_name, properties)
else:
return create_fallback_geojson(full_name, {"full_name": full_name, "committer": "No"})
else:
return create_fallback_geojson(full_name, {"full_name": full_name, "committer": "No"})
# Final function to build a FeatureCollection GeoJSON
def build_geojson_feature_collection(file_path):
entries = read_names_from_file(file_path)
feature_collection = {"type": "FeatureCollection", "features": []}
with ThreadPoolExecutor(max_workers=10) as executor:
futures = {executor.submit(
find_user_location_and_commit_details,
name, third_part): name for name, third_part
in entries}
for future in as_completed(futures):
name = futures[future]
try:
geojson_feature = future.result()
if geojson_feature:
feature_collection["features"].append(geojson_feature)
except Exception as e:
pass
return feature_collection
def read_names_from_file(file_path):
with open(file_path, 'r') as file:
entries = []
for line in file:
line = line.strip()
if line:
third_part = extract_email_website_github(line)
# Remove the last part (email/website/GitHub link)
name = line.rsplit(' ', 1)[0]
entries.append((name, third_part or ''))
return entries
def process_names(file_path):
entries = read_names_from_file(file_path)
results = []
with ThreadPoolExecutor(max_workers=10) as executor:
futures = {executor.submit(
find_user_location_and_commit_details,
name, third_part): name for name, third_part
in entries}
for future in as_completed(futures):
name = futures[future]
try:
result = future.result()
results.append({name: result})
except Exception as e:
results.append({name: {"error": str(e)}})
return results
file_path = "data/contributors_list.txt"
geojson_results = build_geojson_feature_collection(file_path)
# Write the results to a JSON file
output_file_path = "data/contributors.json"
with open(output_file_path, 'w') as json_file:
json.dump(geojson_results, json_file, indent=4)
print(f"GeoJSON results saved to {output_file_path}")