as-cle-bert commited on
Commit
430f31c
verified
1 Parent(s): bd84e95

Create scrape.py

Browse files
Files changed (1) hide show
  1. scrape.py +88 -0
scrape.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+
3
+ def fetch_contribution_details(username, token):
4
+ url = "https://api.github.com/graphql"
5
+ headers = {"Authorization": f"Bearer {token}"}
6
+
7
+ query = """
8
+ query($username: String!) {
9
+ user(login: $username) {
10
+ contributionsCollection {
11
+ commitContributionsByRepository {
12
+ repository {
13
+ name
14
+ }
15
+ contributions(first: 100) {
16
+ nodes {
17
+ occurredAt
18
+ commitCount
19
+ }
20
+ }
21
+ }
22
+ pullRequestContributionsByRepository {
23
+ repository {
24
+ name
25
+ }
26
+ contributions(first: 100) {
27
+ nodes {
28
+ occurredAt
29
+ pullRequest {
30
+ title
31
+ url
32
+ }
33
+ }
34
+ }
35
+ }
36
+ issueContributionsByRepository {
37
+ repository {
38
+ name
39
+ }
40
+ contributions(first: 100) {
41
+ nodes {
42
+ occurredAt
43
+ issue {
44
+ title
45
+ url
46
+ }
47
+ }
48
+ }
49
+ }
50
+ }
51
+ }
52
+ }
53
+ """
54
+ variables = {"username": username}
55
+
56
+ response = requests.post(url, json={"query": query, "variables": variables}, headers=headers)
57
+ if response.status_code == 200:
58
+ return response.json()
59
+ else:
60
+ raise Exception(f"Query failed with status code {response.status_code}: {response.text}")
61
+
62
+
63
+ def parse_contribution_details(contrib_dets):
64
+ commits_repos = []
65
+ pull_reqs_repos = []
66
+ issues_repos = []
67
+ commits = 0
68
+ issues = 0
69
+ prs = 0
70
+ # Process the data for better readability
71
+ if contrib_dets:
72
+ user_contributions = contrib_dets["data"]["user"]["contributionsCollection"]
73
+ for repo in user_contributions["commitContributionsByRepository"]:
74
+ repo_name = repo["repository"]["name"]
75
+ commits_repos.append(repo_name)
76
+ for commit in repo["contributions"]["nodes"]:
77
+ commits+=commit['commitCount']
78
+ for repo in user_contributions["pullRequestContributionsByRepository"]:
79
+ repo_name = repo["repository"]["name"]
80
+ pull_reqs_repos.append(repo_name)
81
+ for pr in repo["contributions"]["nodes"]:
82
+ prs+=1
83
+ for repo in user_contributions["issueContributionsByRepository"]:
84
+ repo_name = repo["repository"]["name"]
85
+ issues_repos.append(repo_name)
86
+ for issue in repo["contributions"]["nodes"]:
87
+ issues+=1
88
+ return f"- 馃殌 You created **{commits} commits** in {len(set(commits_repos))} personal repositories\n\n- 猡达笍 You created **{prs} pull requests** in {len(set(pull_reqs_repos))} repositories\n\n- 馃 You created **{issues} issues** in {len(set(issues_repos))} repositories\n\n"