-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_scraper.py
More file actions
435 lines (365 loc) · 15 KB
/
github_scraper.py
File metadata and controls
435 lines (365 loc) · 15 KB
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
import os
import csv
import time
import json
import requests
from typing import List, Dict, Any, Optional
from dotenv import load_dotenv
from datetime import datetime
from pathlib import Path
# Load environment variables
load_dotenv()
# GitHub API configuration
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
if not GITHUB_TOKEN:
raise ValueError("GITHUB_TOKEN environment variable is not set")
HEADERS = {
"Authorization": f"token {GITHUB_TOKEN}",
"Accept": "application/vnd.github.v3+json"
}
# Rate limiting parameters
MAX_RETRIES = 3
RETRY_DELAY = 10 # seconds
def search_repositories(keywords: List[str], min_stars: int = 10, limit: int = 1000) -> List[Dict[str, Any]]:
"""
Search GitHub repositories based on keywords.
Args:
keywords: List of keywords to search for
min_stars: Minimum number of stars for repositories
limit: Maximum number of repositories to return
Returns:
List of repository data dictionaries
"""
all_repos = []
for keyword in keywords:
print(f"Searching repositories with keyword: {keyword}")
page = 1
per_page = 100 # GitHub API maximum
while len(all_repos) < limit:
query = f"{keyword} in:description stars:>={min_stars}"
url = f"https://api.github.com/search/repositories?q={query}&sort=stars&order=desc&page={page}&per_page={per_page}"
response = make_github_request(url)
if not response:
break
repos = response.get("items", [])
if not repos:
break
# Extract relevant information from each repository
for repo in repos:
repo_data = {
"id": repo.get("id"),
"name": repo.get("name"),
"full_name": repo.get("full_name"),
"html_url": repo.get("html_url"),
"description": repo.get("description"),
"created_at": repo.get("created_at"),
"updated_at": repo.get("updated_at"),
"pushed_at": repo.get("pushed_at"),
"homepage": repo.get("homepage"),
"size": repo.get("size"),
"stargazers_count": repo.get("stargazers_count"),
"watchers_count": repo.get("watchers_count"),
"language": repo.get("language"),
"forks_count": repo.get("forks_count"),
"open_issues_count": repo.get("open_issues_count"),
"license": (repo.get("license") or {}).get("name"),
"topics": repo.get("topics", []),
"has_wiki": repo.get("has_wiki"),
"has_pages": repo.get("has_pages"),
"has_projects": repo.get("has_projects"),
"has_downloads": repo.get("has_downloads"),
"archived": repo.get("archived"),
"disabled": repo.get("disabled"),
"visibility": repo.get("visibility"),
"default_branch": repo.get("default_branch"),
"matched_keyword": keyword
}
# Check if repo is already in the list (from another keyword)
if not any(r["id"] == repo_data["id"] for r in all_repos):
all_repos.append(repo_data)
if len(all_repos) >= limit:
break
# Check if we've reached the last page
if len(repos) < per_page:
break
page += 1
time.sleep(1) # Be nice to the API
print(f"Found {len(all_repos)} unique repositories")
return all_repos
def get_repository_details(repo_full_name: str) -> Optional[Dict[str, Any]]:
"""
Get detailed information about a specific repository.
Args:
repo_full_name: Full name of the repository (owner/repo)
Returns:
Dictionary with repository details or None if request fails
"""
url = f"https://api.github.com/repos/{repo_full_name}"
return make_github_request(url)
def get_repository_languages(repo_full_name: str) -> Dict[str, int]:
"""
Get language breakdown for a repository.
Args:
repo_full_name: Full name of the repository (owner/repo)
Returns:
Dictionary mapping language names to byte counts
"""
url = f"https://api.github.com/repos/{repo_full_name}/languages"
response = make_github_request(url)
return response or {}
def get_repository_contributors(repo_full_name: str, limit: int = 100) -> List[Dict[str, Any]]:
"""
Get contributors for a repository.
Args:
repo_full_name: Full name of the repository (owner/repo)
limit: Maximum number of contributors to return
Returns:
List of contributor data dictionaries
"""
contributors = []
page = 1
per_page = 100 # GitHub API maximum
while len(contributors) < limit:
url = f"https://api.github.com/repos/{repo_full_name}/contributors?page={page}&per_page={per_page}"
response = make_github_request(url)
if not response or not isinstance(response, list):
break
if not response:
break
contributors.extend(response)
if len(response) < per_page:
break
page += 1
time.sleep(1) # Be nice to the API
# Limit the number of contributors
return contributors[:limit]
def get_user_details(username: str) -> Optional[Dict[str, Any]]:
"""
Get detailed information about a GitHub user.
Args:
username: GitHub username
Returns:
Dictionary with user details or None if request fails
"""
url = f"https://api.github.com/users/{username}"
return make_github_request(url)
def make_github_request(url: str) -> Optional[Any]:
"""
Make a request to the GitHub API with retry logic for rate limiting.
Args:
url: GitHub API URL
Returns:
JSON response data or None if all retries fail
"""
for attempt in range(MAX_RETRIES):
try:
response = requests.get(url, headers=HEADERS)
# Check for rate limiting
if response.status_code == 403 and 'X-RateLimit-Remaining' in response.headers and int(response.headers['X-RateLimit-Remaining']) == 0:
reset_time = int(response.headers['X-RateLimit-Reset'])
sleep_time = max(reset_time - time.time(), 0) + 1
print(f"Rate limit exceeded. Sleeping for {sleep_time:.2f} seconds.")
time.sleep(sleep_time)
continue
# Check for other errors
if response.status_code != 200:
print(f"Error: {response.status_code} - {response.text}")
time.sleep(RETRY_DELAY)
continue
return response.json()
except Exception as e:
print(f"Request error: {str(e)}")
time.sleep(RETRY_DELAY)
print(f"Failed to get data from {url} after {MAX_RETRIES} attempts")
return None
def save_to_csv(data: List[Dict[str, Any]], filename: str, fieldnames: Optional[List[str]] = None) -> None:
"""
Save data to a CSV file.
Args:
data: List of dictionaries to save
filename: Output filename
fieldnames: List of field names for CSV header (optional)
"""
if not data:
print(f"No data to save to {filename}")
return
# If fieldnames not provided, use keys from first item
if not fieldnames:
fieldnames = list(data[0].keys())
with open(filename, 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for item in data:
# Handle lists and dictionaries by converting to JSON strings
row = {}
for key, value in item.items():
if key in fieldnames:
if isinstance(value, (list, dict)):
row[key] = json.dumps(value)
else:
row[key] = value
writer.writerow(row)
print(f"Data saved to {filename}")
def process_repositories(keywords: List[str], min_stars: int = 10, repo_limit: int = 1000,
contributor_limit: int = 50) -> None:
"""
Main function to process repositories and contributors.
Args:
keywords: List of keywords to search for
min_stars: Minimum number of stars for repositories
repo_limit: Maximum number of repositories to process
contributor_limit: Maximum number of contributors per repository
"""
# Create output directory
output_dir = Path("github_data_2")
output_dir.mkdir(exist_ok=True)
# Generate timestamp for filenames
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
# Search repositories
repositories = search_repositories(keywords, min_stars, repo_limit)
if not repositories:
print("No repositories found matching the criteria")
return
# Save repositories to CSV
repo_filename = output_dir / f"repositories_{timestamp}.csv"
save_to_csv(repositories, repo_filename)
# Process each repository for additional details and contributors
# Using a dict to track aggregated contributor data keyed by username
all_contributors: Dict[str, Dict[str, Any]] = {}
for i, repo in enumerate(repositories):
repo_full_name = repo["full_name"]
print(f"Processing repository {i+1}/{len(repositories)}: {repo_full_name}")
# Get repository languages and add to repo data
languages = get_repository_languages(repo_full_name)
repo["languages"] = languages
# Get contributors for the repository
contributors = get_repository_contributors(repo_full_name, contributor_limit)
print(f"Found {len(contributors)} contributors for {repo_full_name}")
# Process each contributor
for contributor in contributors:
username = contributor.get("login")
if not username:
continue
# Prepare contribution details for the current repository
contribution_info = {
"repository": repo_full_name,
"contributions": contributor.get("contributions"),
"repository_stars": repo.get("stargazers_count"),
"repository_language": repo.get("language")
}
if username in all_contributors:
# Already processed: update aggregated info
all_contributors[username]["repository_contributions"].append(contribution_info)
all_contributors[username]["total_contributions"] += contributor.get("contributions", 0)
else:
# New contributor, fetch detailed user information
user_details = get_user_details(username)
if not user_details:
continue
all_contributors[username] = {
"username": username,
"name": user_details.get("name"),
"company": user_details.get("company"),
"blog": user_details.get("blog"),
"location": user_details.get("location"),
"email": user_details.get("email"),
"bio": user_details.get("bio"),
"twitter_username": user_details.get("twitter_username"),
"public_repos": user_details.get("public_repos"),
"public_gists": user_details.get("public_gists"),
"followers": user_details.get("followers"),
"following": user_details.get("following"),
"created_at": user_details.get("created_at"),
"updated_at": user_details.get("updated_at"),
"html_url": user_details.get("html_url"),
"type": user_details.get("type"),
"site_admin": user_details.get("site_admin"),
"total_contributions": contributor.get("contributions", 0),
"repository_contributions": [contribution_info]
}
# Save updated repository and contributor data periodically
if (i + 1) % 10 == 0 or i == len(repositories) - 1:
repo_filename = output_dir / f"repositories_detailed_{timestamp}.csv"
save_to_csv(repositories, repo_filename)
# Convert the contributors dict to a list for CSV export
all_contributors_list = list(all_contributors.values())
if all_contributors_list:
contributor_filename = output_dir / f"contributors_{timestamp}.csv"
save_to_csv(all_contributors_list, contributor_filename)
time.sleep(1) # Be kind to the GitHub API
print(f"Processed {len(repositories)} repositories and tracked contributions from {len(all_contributors)} contributors")
def main():
"""Main entry point for the script."""
# Define keywords to search for
keywords = [
"data pipeline",
"ETL",
"data engineering",
"data infrastructure",
"data warehouse",
"data lake",
"data mesh",
"data orchestration",
"data transformation",
"data integration",
"ELT",
"batch processing",
"stream processing",
"Apache Airflow",
"Dagster",
"Prefect",
"Luigi",
"dbt",
"Apache Spark",
"Apache Flink",
"Apache Beam",
"Kafka",
"Apache Kafka",
"streaming data",
"data ingestion",
"data catalog",
"CDC", # Change Data Capture
"extract transform load",
"workflow orchestration",
"data modeling",
"data validation",
"data quality",
"data governance",
"data lineage",
"metadata management",
"data scheduling",
"distributed processing",
"big data",
"real-time data",
"Snowflake",
"BigQuery",
"Redshift",
"Athena",
"lakehouse",
"Delta Lake",
"Apache Hudi",
"Apache Iceberg",
"OLAP",
"analytics engineering",
"SQL transformations",
"data observability",
"Fivetran",
"Stitch",
"Singer",
"OpenMetadata",
"Apache NiFi",
"workflow scheduler",
"data automation",
"schema evolution",
"data migration",
"event-driven architecture"
]
# Process repositories with the given keywords
process_repositories(
keywords=keywords,
min_stars=50, # Repositories with at least 50 stars
repo_limit=5000, # Process up to 500 repositories
contributor_limit=100 # Get up to 30 contributors per repository
)
if __name__ == "__main__":
main()