1
1
from django .core .management import BaseCommand
2
- import urllib2 , json , urllib
2
+ import urllib2 , json , urllib , base64
3
3
from main .models import *
4
4
try :
5
- from biojs .config import *
5
+ from biojs .settings import GITHUB_CLIENT_ID , GITHUB_CLIENT_SECRET
6
6
except :
7
+ print ('ERROR: Could not load config!' )
7
8
GITHUB_CLIENT_ID = ''
8
9
GITHUB_CLIENT_SECRET = ''
9
10
from datetime import datetime
10
11
import pytz
11
12
import ast
13
+ import re
12
14
13
15
# Get sniper data
14
16
'''
15
17
https://rawgit.com/cytoscape/cytoscape.js/master/package.json
16
18
"sniper" key, has "js" and "css". Search for "first"
17
19
'''
18
20
19
- def get_commit_hash (commits_url ):
20
- commits_url = commits_url .split ('{' )[0 ] + '?client_id=' + GITHUB_CLIENT_ID + '&client_secret=' + GITHUB_CLIENT_SECRET
21
- print (commits_url )
22
- response = urllib .urlopen (commits_url )
23
- data = json .loads (response .read ())[0 ]
24
- return data ['sha' ]
21
+ def get_npm_data ():
22
+ # response = urllib2.urlopen()
23
+ hdr = {'User-Agent' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11' }
24
+ req = urllib2 .Request ("http://registry.npmjs.com/-/v1/search?text=keywords:biojs,bionode&size=500" , headers = hdr )
25
+ # req = urllib2.Request("http://registry.npmjs.com/-/v1/search?text=biojs-vis-msa&size=1", headers=hdr)
26
+ response = urllib2 .urlopen (req )
27
+ data = json .load (response )
28
+ return data
29
+
30
+ def send_GET_request (user , password , url ):
31
+ request = urllib2 .Request (url )
32
+ if (user is not None and password is not None ):
33
+ base64string = base64 .encodestring ('%s:%s' % (user , password )).replace ('\n ' , '' )
34
+ request .add_header ('Authorization' , 'Basic %s' % base64string )
35
+ return urllib2 .urlopen (request )
36
+
37
+ def create_jsdelivr_link (owner , repo , file_path , commit = None ):
38
+ return str ('https://cdn.jsdelivr.net/gh/' + owner + '/' + repo + ('@' + commit if commit else '' ) + file_path )
39
+
40
+ def build_dependency_link (owner , repo , dependency , commit = None ):
41
+ if (re .match ('^(?:[a-z]+:)?//' , dependency )):
42
+ print ('Absolute dependency!' , dependency )
43
+ return dependency
44
+ else :
45
+ print ('relative dependency!' , dependency )
46
+ return create_jsdelivr_link (owner , repo , dependency , commit )
25
47
26
48
### store the dependency urls and snippet urls
27
49
def update_visualizations (component , commit_hash , test = False ):
28
50
github_url_list = component .github_url .split ('?' )[0 ].split ('/' )
29
51
owner = github_url_list [4 ]
30
52
repo_name = github_url_list [5 ]
31
53
try :
32
- sniper_data = json .load (urllib .urlopen ("https://cdn.rawgit.com/" + str (owner ) +
33
- '/' + str (repo_name ) + "/" + commit_hash + "/package.json" ))["sniper" ]
34
- except :
54
+ url = create_jsdelivr_link (owner , repo_name , '/package.json' , commit_hash )
55
+ # url = str('https://cdn.jsdelivr.net/gh/' + str(owner) + '/' + str(repo_name) + '@' + commit_hash + '/package.json')
56
+ response = send_GET_request (None , None , url )
57
+ package = json .load (response )
58
+ sniper_data = package ["sniper" ]
59
+ except KeyError :
60
+ print ('No sniper info in ' , repo_name )
35
61
return
62
+ # buildJS and js are thrown together and then separated by string matching?
36
63
try :
37
64
buildJS = sniper_data ["buildJS" ]
38
65
except :
@@ -51,56 +78,8 @@ def update_visualizations(component, commit_hash, test=False):
51
78
except :
52
79
css = []
53
80
css_dependencies = buildCSS + css
54
- for dependency in js_dependencies :
55
- if dependency .startswith ('http' ) :
56
- dependency , created = JSDependency .objects .get_or_create (component = component , js_url = dependency )
57
- elif dependency .startswith ('/' ):
58
- dependency_string = dependency
59
- if 'build/' in dependency_string :
60
- continue
61
- dependency = "https://cdn.rawgit.com/" + str (owner ) + '/' + str (repo_name ) + "/" + commit_hash + dependency
62
- try :
63
- req_dependency = JSDependency .objects .get (component = component , sniper_data_value = dependency_string )
64
- req_dependency .js_url = dependency
65
- req_dependency .save ()
66
- except :
67
- req_dependency = JSDependency .objects .create (component = component , sniper_data_value = dependency_string , js_url = dependency )
68
- else :
69
- dependency_string = dependency
70
- if 'build/' in dependency_string :
71
- continue
72
- dependency = "https://cdn.rawgit.com/" + str (owner ) + '/' + str (repo_name ) + "/" + commit_hash + "/" + dependency
73
- try :
74
- req_dependency = JSDependency .objects .get (component = component , sniper_data_value = dependency_string )
75
- req_dependency .js_url = dependency
76
- req_dependency .save ()
77
- except :
78
- req_dependency = JSDependency .objects .create (component = component , sniper_data_value = dependency_string , js_url = dependency )
79
- for dependency in css_dependencies :
80
- if dependency .startswith ('http' ):
81
- dependency , created = CSSDependency .objects .get_or_create (component = component , css_url = dependency )
82
- elif dependency .startswith ('/' ):
83
- dependency_string = dependency
84
- if 'build/' in dependency_string :
85
- continue
86
- dependency = "https://cdn.rawgit.com/" + str (owner ) + '/' + str (repo_name ) + "/" + commit_hash + dependency
87
- try :
88
- req_dependency = CSSDependency .objects .get (component = component , sniper_data_value = dependency_string )
89
- req_dependency .css_url = dependency
90
- req_dependency .save ()
91
- except :
92
- req_dependency = CSSDependency .objects .create (component = component , sniper_data_value = dependency_string , css_url = dependency )
93
- else :
94
- dependency_string = dependency
95
- if 'build/' in dependency_string :
96
- continue
97
- dependency = "https://cdn.rawgit.com/" + str (owner ) + '/' + str (repo_name ) + "/" + commit_hash + dependency
98
- try :
99
- req_dependency = CSSDependency .objects .get (component = component , sniper_data_value = dependency_string )
100
- req_dependency .css_url = dependency
101
- req_dependency .save ()
102
- except :
103
- req_dependency = CSSDependency .objects .create (component = component , sniper_data_value = dependency_string , css_url = dependency )
81
+ js_deps = map (lambda l : build_dependency_link (owner , repo_name , l , commit_hash ), js_dependencies )
82
+ css_deps = map (lambda l : build_dependency_link (owner , repo_name , l , commit_hash ), css_dependencies )
104
83
try :
105
84
sniperData = SniperData .objects .get (component = component )
106
85
except :
@@ -114,7 +93,7 @@ def update_visualizations(component, commit_hash, test=False):
114
93
if no_browserify :
115
94
sniperData .wzrd_url = '#'
116
95
else :
117
- sniperData .wzrd_url = "https://wzrd.in/bundle/" + component .name
96
+ sniperData .wzrd_url = "https://wzrd.in/bundle/" + component .name
118
97
except :
119
98
sniperData .wzrd_url = "https://wzrd.in/bundle/" + component .name
120
99
try :
@@ -147,35 +126,6 @@ def update_visualizations(component, commit_hash, test=False):
147
126
except :
148
127
pass
149
128
150
- def get_github_data (github_url ):
151
- response = urllib .urlopen (github_url )
152
- data = json .load (response )
153
- return data
154
-
155
- def get_npm_data ():
156
- # response = urllib2.urlopen()
157
- hdr = {'User-Agent' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11' }
158
- req = urllib2 .Request ("http://registry.npmjs.com/-/v1/search?text=keywords:biojs,bionode&size=500" , headers = hdr )
159
- # req = urllib2.Request("http://registry.npmjs.com/-/v1/search?text=biojs-vis-msa&size=1", headers=hdr)
160
- response = urllib2 .urlopen (req )
161
- data = json .load (response )
162
- return data
163
-
164
- def get_contributors_data (contributors_url ):
165
- response = urllib .urlopen (contributors_url )
166
- data = json .load (response )
167
- return data
168
-
169
- def get_downloads (downloads_url ):
170
- print (downloads_url )
171
- response = urllib .urlopen (downloads_url )
172
- downloads = 0
173
- data = ast .literal_eval (response .read ())
174
- for download in data :
175
- downloads += int (download ['download_count' ])
176
- return downloads
177
-
178
-
179
129
class Command (BaseCommand ):
180
130
# during --help
181
131
help = "Command to update the details of all the components from Github"
@@ -228,7 +178,7 @@ def handle(self, *args, **options):
228
178
try :
229
179
github_url = component_data ['links' ]['repository' ]
230
180
url_list = github_url .split ('/' )
231
- _component .github_url = 'https://api.github.com/repos/' + str (url_list [3 ]) + '/' + str (url_list [4 ]) + '?client_id=' + GITHUB_CLIENT_ID + '&client_secret=' + GITHUB_CLIENT_SECRET
181
+ _component .github_url = 'https://api.github.com/repos/' + str (url_list [3 ]) + '/' + str (url_list [4 ])
232
182
except :
233
183
pass
234
184
try :
@@ -244,8 +194,15 @@ def handle(self, *args, **options):
244
194
if _component .github_url :
245
195
print (_component .github_url )
246
196
try :
247
- github_data = get_github_data (_component .github_url )
248
- except :
197
+ response = send_GET_request (GITHUB_CLIENT_ID , GITHUB_CLIENT_SECRET , _component .github_url )
198
+ github_data = json .load (response )
199
+ except urllib2 .HTTPError as e :
200
+ print ('Error getting github data!' )
201
+ print (e )
202
+ continue
203
+ except Exception as e :
204
+ print ('Unexpected error accessing Github!' )
205
+ print (e )
249
206
continue
250
207
_component .stars = github_data ['stargazers_count' ]
251
208
_component .forks = github_data ['forks' ]
@@ -277,17 +234,21 @@ def handle(self, *args, **options):
277
234
# else:
278
235
_component .github_update_time = aware_date
279
236
try :
280
- latest_commit_hash = get_commit_hash (github_data ['commits_url' ])
237
+ response = send_GET_request (GITHUB_CLIENT_ID , GITHUB_CLIENT_SECRET , github_data ['commits_url' ].split ('{' )[0 ])
238
+ latest_commit = json .load (response )[0 ]
239
+ latest_commit_hash = latest_commit ['sha' ]
240
+ _component .latest_commit_hash = latest_commit_hash
241
+ update_visualizations (_component , latest_commit_hash )
281
242
except :
282
- continue
283
- _component .latest_commit_hash = latest_commit_hash
284
- update_visualizations (_component , latest_commit_hash )
243
+ print ('Error getting commit hash!' )
244
+ pass
285
245
# except:
286
246
# pass
287
247
_component .save ()
288
- print (str (github_data ['contributors_url' ]) + '?client_id=' + GITHUB_CLIENT_ID + '&client_secret=' + GITHUB_CLIENT_SECRET )
248
+ print (str (github_data ['contributors_url' ]))
289
249
try :
290
- contributors_data = get_contributors_data (str (github_data ['contributors_url' ]) + '?client_id=' + GITHUB_CLIENT_ID + '&client_secret=' + GITHUB_CLIENT_SECRET )
250
+ response = send_GET_request (GITHUB_CLIENT_ID , GITHUB_CLIENT_SECRET , str (github_data ['contributors_url' ]))
251
+ contributors_data = json .load (response )
291
252
except :
292
253
continue
293
254
commits = 0
@@ -307,12 +268,11 @@ def handle(self, *args, **options):
307
268
commits += _contribution .contributions
308
269
count += 1
309
270
except :
310
- print 'Error'
271
+ print ( 'Error' )
311
272
continue
312
- try :
313
- _component .downloads = get_downloads (str (github_data ['downloads_url' ]) + '?client_id=' + GITHUB_CLIENT_ID + '&client_secret=' + GITHUB_CLIENT_SECRET )
314
- except :
315
- pass
273
+ response = send_GET_request (GITHUB_CLIENT_ID , GITHUB_CLIENT_SECRET , github_data ['downloads_url' ])
274
+ downloads_array = json .load (response )
275
+ _component .downloads = len (downloads_array )
316
276
_component .commits = commits
317
277
_component .no_of_contributors = count
318
278
_component .save ()
0 commit comments