-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_stats.py
More file actions
323 lines (290 loc) · 9.63 KB
/
Copy pathgithub_stats.py
File metadata and controls
323 lines (290 loc) · 9.63 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
import asyncio
import os
from typing import Dict, List, Optional, Set, Tuple, Any, cast
import aiohttp
import requests
class Queries(object):
"""
Class with functions to query the GitHub GraphQL (v4) API and the REST (v3)
API. Also includes functions to dynamically generate GraphQL queries.
"""
def __init__(
self,
username: str,
access_token: str,
session: aiohttp.ClientSession,
max_connections: int = 10,
):
self.username = username
self.access_token = access_token
self.session = session
self.semaphore = asyncio.Semaphore(max_connections)
async def query(self, generated_query: str) -> Dict:
"""
Make a request to the GraphQL API using the authentication token from
the environment
:param generated_query: string query to be sent to the API
:return: decoded GraphQL JSON output
"""
headers = {
"Authorization": f"Bearer {self.access_token}"
}
try:
async with self.semaphore:
r_async = await self.session.post(
"https://api.github.com/graphql",
headers=headers,
json={"query": generated_query},
)
result = await r_async.json()
if result is not None:
return result
except:
print("aiohttp failed for GraphQL query")
# Fall back on non-async requests
async with self.semaphore:
r_requests = requests.post(
"https://api.github.com/graphql",
headers=headers,
json={"query": generated_query},
)
result = r_requests.json()
if result is not None:
return result
return dict()
async def query_rest(self, path: str, params: Optional[Dict] = None) -> Dict:
"""
Make a request to the REST API
:param path: API path to query
:param params: Query parameters to be passed to the API
:return: deserialized REST JSON output
"""
headers = {
"Authorization": f"token {self.access_token}"
}
for _ in range(60):
if params is None:
params = dict()
if path.startswith("/"):
path = path[1:]
try:
async with self.semaphore:
r_async = await self.session.get(
f"https://api.github.com/{path}",
headers=headers,
params=tuple(params.items()),
)
if r_async.status == 202:
# print(f"{path} returned 202. Retrying...")
print(f"A path returned 202. Retrying...")
await asyncio.sleep(2)
continue
result = await r_async.json()
if result is not None:
return result
except:
print("aiohttp failed for rest query")
# Fall back on non-async requests
async with self.semaphore:
r_requests = requests.get(
f"https://api.github.com/{path}",
headers=headers,
params=tuple(params.items()),
)
if r_requests.status_code == 202:
print(f"A path returned 202. Retrying...")
await asyncio.sleep(2)
continue
elif r_requests.status_code == 200:
return r_requests.json()
# print(f"There were too many 202s. Data for {path} will be incomplete.")
print("There were too many 202s. Data for this repository will be incomplete.")
return dict()
@staticmethod
def repos_overview(
contrib_cursor: Optional[str] = None, owned_cursor: Optional[str] = None
) -> str:
"""
:return: GraphQL query with overview of user repositories
"""
return f"""{{
viewer {{
login,
name,
repositories(
first: 100,
orderBy: {{
field: UPDATED_AT,
direction: DESC
}},
isFork: false,
after: {"null" if owned_cursor is None else '"'+ owned_cursor +'"'}
) {{
pageInfo {{
hasNextPage
endCursor
}}
nodes {{
nameWithOwner
stargazers {{
totalCount
}}
forkCount
}}
}}
repositoriesContributedTo(
first: 100,
includeUserRepositories: true,
orderBy: {{
field: UPDATED_AT,
direction: DESC
}},
contributionTypes: [
COMMIT,
ISSUE,
PULL_REQUEST,
REPOSITORY,
PULL_REQUEST_REVIEW
]
after: {"null" if contrib_cursor is None else '"'+ contrib_cursor +'"'}
) {{
pageInfo {{
hasNextPage
endCursor
}}
}}
}}
}}
"""
class Stats(object):
"""
Retrieve and store statistics about GitHub usage.
"""
def __init__(
self,
username: str,
access_token: str,
session: aiohttp.ClientSession,
):
self.username = username
self.queries = Queries(username, access_token, session)
self._name: Optional[str] = None
self._stargazers: Optional[int] = None
self._forks: Optional[int] = None
self._repos: Optional[Set[str]] = None
async def to_str(self) -> str:
"""
:return: summary of all available statistics
"""
return f"""Name: {await self.name}
Stargazers: {await self.stargazers:,}
Forks: {await self.forks:,}
Repositories with contributions: {len(await self.repos)}
"""
async def get_stats(self) -> None:
"""
Get lots of summary statistics using one big query. Sets many attributes
"""
self._stargazers = 0
self._forks = 0
self._repos = set()
next_owned = None
next_contrib = None
while True:
raw_results = await self.queries.query(
Queries.repos_overview(
owned_cursor=next_owned, contrib_cursor=next_contrib
)
)
raw_results = raw_results if raw_results is not None else {}
self._name = raw_results.get("data", {}).get("viewer", {}).get("name", None)
if self._name is None:
self._name = (
raw_results.get("data", {})
.get("viewer", {})
.get("login", "No Name")
)
contrib_repos = (
raw_results.get("data", {})
.get("viewer", {})
.get("repositoriesContributedTo", {})
)
owned_repos = (
raw_results.get("data", {}).get("viewer", {}).get("repositories", {})
)
repos = owned_repos.get("nodes", [])
# if not self._ignore_forked_repos:
# repos += contrib_repos.get("nodes", [])
for repo in repos:
if repo is None:
continue
name = repo.get("nameWithOwner")
self._repos.add(name)
self._stargazers += repo.get("stargazers").get("totalCount", 0)
self._forks += repo.get("forkCount", 0)
if owned_repos.get("pageInfo", {}).get(
"hasNextPage", False
) or contrib_repos.get("pageInfo", {}).get("hasNextPage", False):
next_owned = owned_repos.get("pageInfo", {}).get(
"endCursor", next_owned
)
next_contrib = contrib_repos.get("pageInfo", {}).get(
"endCursor", next_contrib
)
else:
break
@property
async def name(self) -> str:
"""
:return: GitHub user's name
"""
if self._name is not None:
return self._name
await self.get_stats()
assert self._name is not None
return self._name
@property
async def stargazers(self) -> int:
"""
:return: total number of stargazers on user's repos
"""
if self._stargazers is not None:
return self._stargazers
await self.get_stats()
assert self._stargazers is not None
return self._stargazers
@property
async def forks(self) -> int:
"""
:return: total number of forks on user's repos
"""
if self._forks is not None:
return self._forks
await self.get_stats()
assert self._forks is not None
return self._forks
@property
async def repos(self) -> Set[str]:
"""
:return: list of names of user's repos
"""
if self._repos is not None:
return self._repos
await self.get_stats()
assert self._repos is not None
return self._repos
async def main() -> None:
"""
Used mostly for testing; this module is not usually run standalone
"""
from dotenv import dotenv_values
access_token = dotenv_values('.env')['ACCESS_TOKEN']
# access_token = os.getenv("ACCESS_TOKEN")
async with aiohttp.ClientSession() as session:
s = Stats('ngntrgduc', access_token, session)
print(await s.to_str())
if __name__ == "__main__":
from time import perf_counter
tic = perf_counter()
asyncio.run(main())
print(f'Took {perf_counter()-tic:.2f}s')