This repository has been archived by the owner on Nov 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
gdc.py
77 lines (63 loc) · 2.02 KB
/
gdc.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
75
76
77
#!/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 io
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 = io.StringIO()
total_count = 0
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']:
total_count += asset['download_count']
date = asset['updated_at'].split('T')[0]
print('%d\t%s\t%s'%(
asset['download_count'],
date,
asset['name'],
))
else:
print ("No data")
except:
pass
print('%d\tTotal Downloads'%total_count)