-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate
executable file
·81 lines (59 loc) · 1.92 KB
/
update
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
#!/usr/bin/env python3
import argparse
import numpy as np
import os
import pandas as pd
import requests
import sys
GITHUB_TOKEN = os.getenv('GITHUB_TOKEN')
HEADERS = {'Authorization': 'Bearer %s' % (GITHUB_TOKEN)}
def gh_query(query: str):
request = requests.post('https://api.github.com/graphql', json={'query': query}, headers=HEADERS)
if request.status_code != 200:
raise Exception(f'Query failed with status code {request.status_code}: {query}')
return request.json()
def parse_github_url(url: str):
if not url.startswith('https://github.com'):
return None
tokens = url.split('/')
if len(tokens) != 5:
return None
return tokens[-2:]
def update(x: pd.Series) -> pd.Series:
# check whether url is a github url
gh_repo = parse_github_url(x.url)
# update github stars
if gh_repo is not None:
owner, name = gh_repo
query = '''
query {
repository(owner: "%s", name: "%s") {
stargazerCount
}
}
''' % (owner, name)
result = gh_query(query)
x.github_stars = result['data']['repository']['stargazerCount']
else:
x.github_stars = np.nan
return x
def update_safe(x: pd.Series) -> pd.Series:
try:
sys.stderr.write('Updating \'%s\'...\n' % (x['name']))
return update(x)
except Exception as e:
sys.stderr.write('Failed to update \'%s\': %s\n' % (x['name'], e))
return x
def main():
# parse command-line args
parser = argparse.ArgumentParser()
parser.add_argument('input', help='Input dataset (CSV)')
args = parser.parse_args()
# load input file
df = pd.read_csv(args.input)
# fetch github metadata
df = df.apply(lambda x: update_safe(x), axis='columns')
# save output file
df.to_csv(args.input, index=False, float_format='%.0f')
if __name__ == '__main__':
main()