Datasets:
Add script for fetching historical versions
Browse files- fetch_history.py +63 -0
fetch_history.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import csv
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
import sys
|
5 |
+
|
6 |
+
import requests
|
7 |
+
import requests_ratelimiter
|
8 |
+
import tqdm
|
9 |
+
|
10 |
+
|
11 |
+
def get_commits(session, repo, path):
|
12 |
+
query = {"path": path}
|
13 |
+
headers = {
|
14 |
+
"Accept": "application/vnd.github+json",
|
15 |
+
"Authorization": "Bearer " + os.environ["GITHUB_TOKEN"],
|
16 |
+
"X-GitHub-Api-Version": "2022-11-28",
|
17 |
+
}
|
18 |
+
|
19 |
+
try:
|
20 |
+
r = session.get(
|
21 |
+
"https://api.github.com/repos/" + repo + "/commits",
|
22 |
+
params=query,
|
23 |
+
headers=headers,
|
24 |
+
timeout=10,
|
25 |
+
)
|
26 |
+
except (
|
27 |
+
requests.exceptions.ConnectionError,
|
28 |
+
requests.exceptions.ReadTimeout,
|
29 |
+
):
|
30 |
+
# Skip on request error
|
31 |
+
return set()
|
32 |
+
else:
|
33 |
+
# Get the commit hashes
|
34 |
+
return set(c["sha"] for c in r.json())
|
35 |
+
|
36 |
+
|
37 |
+
def main():
|
38 |
+
# Initialize a new session
|
39 |
+
session = requests.Session()
|
40 |
+
adapter = requests_ratelimiter.LimiterAdapter(per_second=2)
|
41 |
+
session.mount("http://", adapter)
|
42 |
+
session.mount("https://", adapter)
|
43 |
+
|
44 |
+
with open("repos.csv", "r") as csvfile:
|
45 |
+
# Count number of rows and reset
|
46 |
+
reader = csv.DictReader(csvfile)
|
47 |
+
rows = sum(1 for row in reader)
|
48 |
+
csvfile.seek(0)
|
49 |
+
|
50 |
+
reader = csv.DictReader(csvfile)
|
51 |
+
for row in tqdm.tqdm(reader, total=rows):
|
52 |
+
# Remove github.com/ from the beginning and fetch commits
|
53 |
+
repo = row["repository"].split("/", maxsplit=1)[1]
|
54 |
+
commits = get_commits(session, repo, row["path"])
|
55 |
+
|
56 |
+
# Write the collected commits
|
57 |
+
obj = {"repository": repo, "path": row["path"], "commits": list(commits)}
|
58 |
+
json.dump(obj, sys.stdout)
|
59 |
+
sys.stdout.write("\n")
|
60 |
+
|
61 |
+
|
62 |
+
if __name__ == "__main__":
|
63 |
+
main()
|