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 (url , user = None , password = None ):
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 is_absolute_dependency (dep ):
41
+ return re .match ('^(?:[a-z]+:)?//' , dep ) is not None
42
+
43
+ def get_owner_and_repo_from_github_url (url ):
44
+ split_url = url .split ('?' )[0 ].split ('/' )
45
+ return split_url [4 ], split_url [5 ]
25
46
26
47
### store the dependency urls and snippet urls
27
48
def update_visualizations (component , commit_hash , test = False ):
28
- github_url_list = component .github_url .split ('?' )[0 ].split ('/' )
29
- owner = github_url_list [4 ]
30
- repo_name = github_url_list [5 ]
49
+ owner , repo_name = get_owner_and_repo_from_github_url (component .github_url )
31
50
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 :
51
+ url = create_jsdelivr_link (owner , repo_name , '/package.json' , commit_hash )
52
+ response = send_GET_request (url )
53
+ package = json .load (response )
54
+ sniper_data = package ["sniper" ]
55
+ except KeyError :
56
+ print ('No sniper info in ' , repo_name )
35
57
return
36
- try :
37
- buildJS = sniper_data ["buildJS" ]
38
- except :
39
- buildJS = []
40
- try :
41
- js = sniper_data ["js" ]
42
- except :
43
- js = []
44
- js_dependencies = buildJS + js
45
- try :
46
- buildCSS = sniper_data ["buildCSS" ]
47
- except :
48
- buildCSS = []
49
- try :
50
- css = sniper_data ["css" ]
51
- except :
52
- css = []
53
- 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 )
104
- try :
105
- sniperData = SniperData .objects .get (component = component )
106
- except :
107
- sniperData = SniperData .objects .create (component = component )
108
- try :
109
- no_browserify = sniper_data ['noBrowserify' ]
110
- sniperData .no_browserify = no_browserify
111
- except :
112
- pass
113
- try :
114
- if no_browserify :
115
- sniperData .wzrd_url = '#'
116
- else :
117
- sniperData .wzrd_url = "https://wzrd.in/bundle/" + component .name
118
- except :
119
- sniperData .wzrd_url = "https://wzrd.in/bundle/" + component .name
120
- try :
121
- snippets_dir_name = sniper_data ['snippets' ][0 ]
122
- sniperData .snippets_dir_name = snippets_dir_name
123
- except :
124
- pass
58
+ buildJS = sniper_data .get ('buildJS' , [])
59
+ js = sniper_data .get ('js' , [])
60
+ buildCSS = sniper_data .get ('buildCSS' , [])
61
+ css = sniper_data .get ('css' , [])
62
+ # Move absolute links from js to buildJS and same for css
63
+ buildJS = buildJS + filter (lambda l : is_absolute_dependency (l ), js )
64
+ js = filter (lambda l : not is_absolute_dependency (l ), js )
65
+ buildCSS = buildCSS + filter (lambda l : is_absolute_dependency (l ), css )
66
+ css = filter (lambda l : not is_absolute_dependency (l ), css )
67
+ # Save to db
68
+ for dep in buildJS :
69
+ JSDependency .objects .create (component = component , js_url = dep , sniper_data_value = dep )
70
+ for dep in buildCSS :
71
+ CSSDependency .objects .create (component = component , css_url = dep , sniper_data_value = dep )
72
+
73
+ sniperData , created = SniperData .objects .get_or_create (component = component )
74
+ if 'noBrowserify' in sniper_data :
75
+ sniperData .no_browserify = sniper_data ['noBrowserify' ]
76
+ elif len (js ) == 0 and len (css ) == 0 :
77
+ sniperData .no_browserify = True
78
+
79
+ sniperData .wzrd_url = '#' if sniperData .no_browserify else 'https://wzrd.in/bundle/' + component .name
80
+ if 'snippets' in sniper_data :
81
+ sniperData .snippets_dir_name = sniper_data ['snippets' ][0 ]
125
82
sniperData .save ()
126
83
127
84
### For Snippets URLs
128
85
try :
86
+ url = str ('https://api.github.com/repos/' + owner + '/' + repo_name + '/contents/' + sniperData .snippets_dir_name + '?ref=master' )
129
87
if not test :
130
- print ("https://api.github.com/repos/" + str (owner ) + "/" + str (repo_name ) + "/contents/" + sniperData .snippets_dir_name + "?ref=master&client_id="
131
- + GITHUB_CLIENT_ID + "&client_secret=" + GITHUB_CLIENT_SECRET )
132
- snippets_data = urllib .urlopen ("https://api.github.com/repos/" + str (owner ) + "/" + str (repo_name ) + "/contents/" + sniperData .snippets_dir_name + "?ref=master&client_id="
133
- + GITHUB_CLIENT_ID + "&client_secret=" + GITHUB_CLIENT_SECRET )
134
- snippets = json .loads (snippets_data .read ())
135
- for snippet in snippets :
136
- if not snippet ['name' ].endswith ('.js' ):
137
- continue
138
- url = "https://cdn.rawgit.com/" + str (owner ) + '/' + str (repo_name ) + "/" + commit_hash + "/" + sniperData .snippets_dir_name + "/" + snippet ['name' ]
139
- try :
140
- name = snippet ['name' ].split ('.' )[0 ]
141
- req_snippet = Snippet .objects .get (name = name , sniperData = sniperData )
142
- req_snippet .url = url
143
- req_snippet .save ()
144
- except :
145
- name = snippet ['name' ].split ('.' )[0 ]
146
- Snippet .objects .create (name = name , sniperData = sniperData , url = url )
147
- except :
148
- pass
88
+ print (url )
89
+ snippets_data = send_GET_request (url , GITHUB_CLIENT_ID , GITHUB_CLIENT_SECRET )
90
+ snippets = json .load (snippets_data )
91
+ filtered_snippets = filter (lambda s : s ['name' ].endswith ('.js' ), snippets )
92
+ except Exception as e :
93
+ print ('ERROR: Something went wrong getting snippets data!' )
94
+ print (e )
149
95
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
96
97
+ for snip in filtered_snippets :
98
+ try :
99
+ url = create_jsdelivr_link (owner , repo_name , str ('/' + sniperData .snippets_dir_name + '/' + snip ['name' ]), commit_hash )
100
+ name = snip .get ('name' , '' ).split ('.' )[0 ]
101
+ Snippet .objects .update_or_create (name = name , url = url , sniperData = sniperData )
102
+ except Exception as e :
103
+ print ('ERROR: Something went wrong creating a new Snippet' )
104
+ print (e )
178
105
179
106
class Command (BaseCommand ):
180
107
# during --help
@@ -228,7 +155,7 @@ def handle(self, *args, **options):
228
155
try :
229
156
github_url = component_data ['links' ]['repository' ]
230
157
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
158
+ _component .github_url = 'https://api.github.com/repos/' + str (url_list [3 ]) + '/' + str (url_list [4 ])
232
159
except :
233
160
pass
234
161
try :
@@ -244,8 +171,15 @@ def handle(self, *args, **options):
244
171
if _component .github_url :
245
172
print (_component .github_url )
246
173
try :
247
- github_data = get_github_data (_component .github_url )
248
- except :
174
+ response = send_GET_request (_component .github_url , GITHUB_CLIENT_ID , GITHUB_CLIENT_SECRET )
175
+ github_data = json .load (response )
176
+ except urllib2 .HTTPError as e :
177
+ print ('Error getting github data!' )
178
+ print (e )
179
+ continue
180
+ except Exception as e :
181
+ print ('Unexpected error accessing Github!' )
182
+ print (e )
249
183
continue
250
184
_component .stars = github_data ['stargazers_count' ]
251
185
_component .forks = github_data ['forks' ]
@@ -277,17 +211,25 @@ def handle(self, *args, **options):
277
211
# else:
278
212
_component .github_update_time = aware_date
279
213
try :
280
- latest_commit_hash = get_commit_hash (github_data ['commits_url' ])
214
+ response = send_GET_request (github_data ['commits_url' ].split ('{' )[0 ], GITHUB_CLIENT_ID , GITHUB_CLIENT_SECRET )
215
+ latest_commit = json .load (response )[0 ]
216
+ latest_commit_hash = latest_commit ['sha' ]
217
+ _component .latest_commit_hash = latest_commit_hash
281
218
except :
282
- continue
283
- _component .latest_commit_hash = latest_commit_hash
284
- update_visualizations (_component , latest_commit_hash )
219
+ print ('Error getting commit hash!' )
220
+ pass
221
+ try :
222
+ update_visualizations (_component , latest_commit_hash )
223
+ except Exception as e :
224
+ print ('Error updating visualisations!' )
225
+ print (e )
285
226
# except:
286
227
# pass
287
228
_component .save ()
288
- print (str (github_data ['contributors_url' ]) + '?client_id=' + GITHUB_CLIENT_ID + '&client_secret=' + GITHUB_CLIENT_SECRET )
229
+ print (str (github_data ['contributors_url' ]))
289
230
try :
290
- contributors_data = get_contributors_data (str (github_data ['contributors_url' ]) + '?client_id=' + GITHUB_CLIENT_ID + '&client_secret=' + GITHUB_CLIENT_SECRET )
231
+ response = send_GET_request (str (github_data ['contributors_url' ]), GITHUB_CLIENT_ID , GITHUB_CLIENT_SECRET )
232
+ contributors_data = json .load (response )
291
233
except :
292
234
continue
293
235
commits = 0
@@ -307,12 +249,11 @@ def handle(self, *args, **options):
307
249
commits += _contribution .contributions
308
250
count += 1
309
251
except :
310
- print 'Error'
252
+ print ( 'Error' )
311
253
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
254
+ response = send_GET_request (github_data ['downloads_url' ], GITHUB_CLIENT_ID , GITHUB_CLIENT_SECRET )
255
+ downloads_array = json .load (response )
256
+ _component .downloads = len (downloads_array )
316
257
_component .commits = commits
317
258
_component .no_of_contributors = count
318
259
_component .save ()
0 commit comments