@@ -27,7 +27,7 @@ def get_npm_data():
27
27
data = json .load (response )
28
28
return data
29
29
30
- def send_GET_request (user , password , url ):
30
+ def send_GET_request (url , user = None , password = None ):
31
31
request = urllib2 .Request (url )
32
32
if (user is not None and password is not None ):
33
33
base64string = base64 .encodestring ('%s:%s' % (user , password )).replace ('\n ' , '' )
@@ -37,94 +37,70 @@ def send_GET_request(user, password, url):
37
37
def create_jsdelivr_link (owner , repo , file_path , commit = None ):
38
38
return str ('https://cdn.jsdelivr.net/gh/' + owner + '/' + repo + ('@' + commit if commit else '' ) + file_path )
39
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 )
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 ]
47
46
48
47
### store the dependency urls and snippet urls
49
48
def update_visualizations (component , commit_hash , test = False ):
50
- github_url_list = component .github_url .split ('?' )[0 ].split ('/' )
51
- owner = github_url_list [4 ]
52
- repo_name = github_url_list [5 ]
49
+ owner , repo_name = get_owner_and_repo_from_github_url (component .github_url )
53
50
try :
54
51
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 )
52
+ response = send_GET_request (url )
57
53
package = json .load (response )
58
54
sniper_data = package ["sniper" ]
59
55
except KeyError :
60
56
print ('No sniper info in ' , repo_name )
61
57
return
62
- # buildJS and js are thrown together and then separated by string matching?
63
- try :
64
- buildJS = sniper_data ["buildJS" ]
65
- except :
66
- buildJS = []
67
- try :
68
- js = sniper_data ["js" ]
69
- except :
70
- js = []
71
- js_dependencies = buildJS + js
72
- try :
73
- buildCSS = sniper_data ["buildCSS" ]
74
- except :
75
- buildCSS = []
76
- try :
77
- css = sniper_data ["css" ]
78
- except :
79
- css = []
80
- css_dependencies = buildCSS + css
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 )
83
- try :
84
- sniperData = SniperData .objects .get (component = component )
85
- except :
86
- sniperData = SniperData .objects .create (component = component )
87
- try :
88
- no_browserify = sniper_data ['noBrowserify' ]
89
- sniperData .no_browserify = no_browserify
90
- except :
91
- pass
92
- try :
93
- if no_browserify :
94
- sniperData .wzrd_url = '#'
95
- else :
96
- sniperData .wzrd_url = "https://wzrd.in/bundle/" + component .name
97
- except :
98
- sniperData .wzrd_url = "https://wzrd.in/bundle/" + component .name
99
- try :
100
- snippets_dir_name = sniper_data ['snippets' ][0 ]
101
- sniperData .snippets_dir_name = snippets_dir_name
102
- except :
103
- 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
+
77
+ sniperData .wzrd_url = '#' if sniperData .no_browserify else 'https://wzrd.in/bundle/' + component .name
78
+ if 'snippets' in sniper_data :
79
+ sniperData .snippets_dir_name = sniper_data ['snippets' ][0 ]
104
80
sniperData .save ()
105
81
106
82
### For Snippets URLs
107
83
try :
84
+ url = str ('https://api.github.com/repos/' + owner + '/' + repo_name + '/contents/' + sniperData .snippets_dir_name + '?ref=master' )
108
85
if not test :
109
- print ("https://api.github.com/repos/" + str (owner ) + "/" + str (repo_name ) + "/contents/" + sniperData .snippets_dir_name + "?ref=master&client_id="
110
- + GITHUB_CLIENT_ID + "&client_secret=" + GITHUB_CLIENT_SECRET )
111
- snippets_data = urllib .urlopen ("https://api.github.com/repos/" + str (owner ) + "/" + str (repo_name ) + "/contents/" + sniperData .snippets_dir_name + "?ref=master&client_id="
112
- + GITHUB_CLIENT_ID + "&client_secret=" + GITHUB_CLIENT_SECRET )
113
- snippets = json .loads (snippets_data .read ())
114
- for snippet in snippets :
115
- if not snippet ['name' ].endswith ('.js' ):
116
- continue
117
- url = "https://cdn.rawgit.com/" + str (owner ) + '/' + str (repo_name ) + "/" + commit_hash + "/" + sniperData .snippets_dir_name + "/" + snippet ['name' ]
118
- try :
119
- name = snippet ['name' ].split ('.' )[0 ]
120
- req_snippet = Snippet .objects .get (name = name , sniperData = sniperData )
121
- req_snippet .url = url
122
- req_snippet .save ()
123
- except :
124
- name = snippet ['name' ].split ('.' )[0 ]
125
- Snippet .objects .create (name = name , sniperData = sniperData , url = url )
126
- except :
127
- pass
86
+ print (url )
87
+ snippets_data = send_GET_request (url , GITHUB_CLIENT_ID , GITHUB_CLIENT_SECRET )
88
+ snippets = json .load (snippets_data )
89
+ filtered_snippets = filter (lambda s : s ['name' ].endswith ('.js' ), snippets )
90
+ except Exception as e :
91
+ print ('ERROR: Something went wrong getting snipper data!' )
92
+ print (e )
93
+
94
+
95
+ for snip in filtered_snippets :
96
+ try :
97
+ url = create_jsdelivr_link (owner , repo_name , str ('/' + sniperData .snippets_dir_name + '/' + snip ['name' ]), commit_hash )
98
+ name = snip .get ('name' , '' ).split ('.' )[0 ]
99
+ Snippet .objects .create (name = name , url = url , sniperData = sniperData )
100
+ print ('here!' )
101
+ except Exception as e :
102
+ print ('ERROR: Something went wrong creating a new Snippet' )
103
+ print (e )
128
104
129
105
class Command (BaseCommand ):
130
106
# during --help
@@ -194,7 +170,7 @@ def handle(self, *args, **options):
194
170
if _component .github_url :
195
171
print (_component .github_url )
196
172
try :
197
- response = send_GET_request (GITHUB_CLIENT_ID , GITHUB_CLIENT_SECRET , _component . github_url )
173
+ response = send_GET_request (_component . github_url , GITHUB_CLIENT_ID , GITHUB_CLIENT_SECRET )
198
174
github_data = json .load (response )
199
175
except urllib2 .HTTPError as e :
200
176
print ('Error getting github data!' )
@@ -234,20 +210,24 @@ def handle(self, *args, **options):
234
210
# else:
235
211
_component .github_update_time = aware_date
236
212
try :
237
- response = send_GET_request (GITHUB_CLIENT_ID , GITHUB_CLIENT_SECRET , github_data ['commits_url' ].split ('{' )[0 ])
213
+ response = send_GET_request (github_data ['commits_url' ].split ('{' )[0 ], GITHUB_CLIENT_ID , GITHUB_CLIENT_SECRET )
238
214
latest_commit = json .load (response )[0 ]
239
215
latest_commit_hash = latest_commit ['sha' ]
240
216
_component .latest_commit_hash = latest_commit_hash
241
- update_visualizations (_component , latest_commit_hash )
242
217
except :
243
218
print ('Error getting commit hash!' )
244
219
pass
220
+ try :
221
+ update_visualizations (_component , latest_commit_hash )
222
+ except Exception as e :
223
+ print ('Error updating visualisations!' )
224
+ print (e )
245
225
# except:
246
226
# pass
247
227
_component .save ()
248
228
print (str (github_data ['contributors_url' ]))
249
229
try :
250
- response = send_GET_request (GITHUB_CLIENT_ID , GITHUB_CLIENT_SECRET , str (github_data ['contributors_url' ]))
230
+ response = send_GET_request (str (github_data ['contributors_url' ]), GITHUB_CLIENT_ID , GITHUB_CLIENT_SECRET )
251
231
contributors_data = json .load (response )
252
232
except :
253
233
continue
@@ -270,7 +250,7 @@ def handle(self, *args, **options):
270
250
except :
271
251
print ('Error' )
272
252
continue
273
- response = send_GET_request (GITHUB_CLIENT_ID , GITHUB_CLIENT_SECRET , github_data ['downloads_url' ])
253
+ response = send_GET_request (github_data ['downloads_url' ], GITHUB_CLIENT_ID , GITHUB_CLIENT_SECRET )
274
254
downloads_array = json .load (response )
275
255
_component .downloads = len (downloads_array )
276
256
_component .commits = commits
0 commit comments