-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountStars.py
54 lines (44 loc) · 1.2 KB
/
CountStars.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
#!/usr/bin/env python3
"""
Count how many GitHub Stars a user has received, using GitHub API v4 GraphQL
"""
import requests
from pathlib import Path
import argparse
ENDPOINT = "https://api.github.com/graphql"
def run_query(query: str, token_file: Path):
request = requests.post(
ENDPOINT, json={"query": query}, headers={"Authorization": token_file.read_text()}
)
if request.status_code == 200:
return request.json()
else:
raise ValueError(f"Query failed with code {request.status_code}")
if __name__ == "__main__":
p = argparse.ArgumentParser(description="Totals up stars with GitHub v4 API")
p.add_argument("oauth", help="path to oauth key")
p.add_argument("username", help="GitHub username(s) to count stars for", nargs="+")
P = p.parse_args()
query = """
query {
search(type: REPOSITORY, user: %s query: "sort:stars stars:>1") {
userCount
edges {
node {
... on Repository {
name
description
stargazers {
totalCount
}
url
}
}
}
}
}
""" % (
P.username
)
token_file = Path(P.oauth).expanduser()
dat = run_query(query, token_file)