forked from mmilidoni/github-downloads-count
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdc
executable file
·70 lines (56 loc) · 1.78 KB
/
gdc
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
#!/usr/bin/env python
import os
import sys
if (len(sys.argv) < 2):
print "Usage: " + sys.argv[0] + " github-user [github-project]"
exit(1)
try:
import requests
except ImportError:
print "Error: requests is not installed"
print "Installing Requests is simple with pip:\n pip install requests"
print "More info: http://docs.python-requests.org/en/latest/"
exit(1)
import cStringIO
import json
def dict_to_object(d):
if '__class__' in d:
class_name = d.pop('__class__')
module_name = d.pop('__module__')
module = __import__(module_name)
class_ = getattr(module, class_name)
args = dict((key.encode('ascii'), value) for key, value in d.items())
inst = class_(**args)
else:
inst = d
return inst
def ensure_str(s):
if isinstance(s, unicode):
s = s.encode('utf-8')
return s
full_names = []
headers = {}
if "GITHUB_TOKEN" in os.environ:
headers["Authorization"] = "token %s" % os.environ["GITHUB_TOKEN"]
if len(sys.argv) == 3:
full_names.append(sys.argv[1] + "/" + sys.argv[2])
else:
buf = cStringIO.StringIO()
r = requests.get('https://api.github.com/users/' + sys.argv[1] + '/repos', headers=headers)
myobj = r.json()
for rep in myobj:
full_names.insert(0, ensure_str(rep['full_name']))
for full_name in full_names:
buf = cStringIO.StringIO()
try:
r = requests.get('https://api.github.com/repos/' + full_name + '/releases', headers=headers)
myobj = r.json()
for p in myobj:
if "assets" in p:
for asset in p['assets']:
print (asset['name'] + ": " + str(asset['download_count']) +
" downloads")
else:
print "No data"
except:
pass