Skip to content

Commit d6de177

Browse files
authored
Merge pull request #51 from awasthishubh/master
Usesd npm api to get weekly dowloads count (#50)
2 parents 5eb05c9 + c11840e commit d6de177

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

main/management/commands/updatecomponents.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ def get_npm_data():
2727
data = json.load(response)
2828
return data
2929

30+
def get_npm_downloads(url):
31+
package=url.split('/')[-1]
32+
# dateRange='1980-02-12:'+str(datetime.date(datetime.now()))
33+
dateRange='last-week'
34+
url='https://api.npmjs.org/downloads/range/'+dateRange+'/'+package
35+
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11'}
36+
req = urllib2.Request(url, headers=hdr)
37+
response = urllib2.urlopen(req)
38+
data = json.load(response)
39+
download_count=0
40+
for i in data['downloads']:
41+
download_count+=i['downloads']
42+
return download_count
43+
3044
def send_GET_request(url, user=None, password=None):
3145
request = urllib2.Request(url)
3246
if (user is not None and password is not None):
@@ -252,9 +266,9 @@ def handle(self, *args, **options):
252266
except:
253267
print ('Error')
254268
continue
255-
response = send_GET_request(github_data['downloads_url'], GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET)
256-
downloads_array = json.load(response)
257-
_component.downloads = len(downloads_array)
269+
# response = send_GET_request(github_data['downloads_url'], GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET)
270+
# downloads_array = json.load(response)
271+
_component.downloads = get_npm_downloads(_component.npm_url)
258272
_component.commits = commits
259273
_component.no_of_contributors = count
260274
_component.save()

0 commit comments

Comments
 (0)