This repository has been archived by the owner on Nov 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
executable file
·74 lines (62 loc) · 1.85 KB
/
app.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import json
import requests
from collections import defaultdict
class GitHub:
rest_url = 'https://api.github.com'
graphql_url = 'https://api.github.com/graphql'
_headers = {
'Authorization': f'token {os.getenv('API_TOKEN')}',
'User-Agent': os.getenv('USER_AGENT', 'Unknown')
}
@staticmethod
def _query(url, method, *args, **kwargs):
res = getattr(requests, method)(
url, *args,
headers=GitHub._headers, **kwargs
)
try:
res.raise_for_status()
except:
sys.stderr.write(res.text)
raise
else:
return res.json()
@staticmethod
def languages(login, repos=10, languages=3):
query = """query {
user(login: "%s") {
repositories(first:%s) {
nodes {
languages(first:%s) {
nodes {
name
}
}
}
}
}
}""" % (login, repos, languages)
res = GitHub._query(
GitHub.graphql_url, 'post',
json.dumps({'query': query})
)
# create a dict of results
languages = defaultdict(int)
# loop through the graphql results
for node in res['data']['user']['repositories']['nodes']:
for language in node['languages']['nodes']:
languages[language['name']] += 1
# sort by popularity
languages = sorted(
languages.items(),
key=lambda a: a[1],
reverse=True
)
# pop our the languages into a list
return list(map(lambda l: l[0], languages))
if __name__ == '__main__':
sys.stdout.write(json.dumps(getattr(GitHub, sys.argv[1])(*sys.argv[2:])))