8
8
GITHUB_CLIENT_SECRET = ''
9
9
from datetime import datetime
10
10
import pytz
11
+ import ast
12
+
13
+ # Get sniper data
14
+ '''
15
+ https://rawgit.com/cytoscape/cytoscape.js/master/package.json
16
+ "sniper" key, has "js" and "css". Search for "first"
17
+ '''
18
+
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' ]
25
+
26
+ ### store the dependency urls and snippet urls
27
+ def update_visualizations (component , commit_hash ):
28
+ github_url_list = component .github_url .split ('?' )[0 ].split ('/' )
29
+ owner = github_url_list [4 ]
30
+ repo_name = github_url_list [5 ]
31
+ 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 :
35
+ 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 ('https://' ):
56
+ dependency , created = JSDependency .objects .get_or_create (component = component , js_url = dependency )
57
+ elif dependency .startswith ('/' ):
58
+ dependency = "https://cdn.rawgit.com/" + str (owner ) + '/' + str (repo_name ) + "/" + commit_hash + dependency
59
+ dependency , created = JSDependency .objects .get_or_create (component = component , js_url = dependency )
60
+ else :
61
+ dependency = "https://cdn.rawgit.com/" + str (owner ) + '/' + str (repo_name ) + "/" + commit_hash + "/" + dependency
62
+ dependency , created = JSDependency .objects .get_or_create (component = component , js_url = dependency )
63
+ for dependency in css_dependencies :
64
+ if dependency .startswith ('https://' ):
65
+ dependency , created = CSSDependency .objects .get_or_create (component = component , css_url = dependency )
66
+ elif dependency .startswith ('/' ):
67
+ dependency = "https://cdn.rawgit.com/" + str (owner ) + '/' + str (repo_name ) + "/" + commit_hash + dependency
68
+ dependency , created = CSSDependency .objects .get_or_create (component = component , css_url = dependency )
69
+ else :
70
+ dependency = "https://cdn.rawgit.com/" + str (owner ) + '/' + str (repo_name ) + "/" + commit_hash + "/" + dependency
71
+ dependency , created = CSSDependency .objects .get_or_create (component = component , css_url = dependency )
72
+ try :
73
+ sniperData = SniperData .objects .get (component = component )
74
+ except :
75
+ sniperData = SniperData .objects .create (component = component )
76
+ try :
77
+ no_browserify = sniper_data ['noBrowserify' ]
78
+ sniperData .no_browserify = no_browserify
79
+ except :
80
+ pass
81
+ try :
82
+ if no_browserify :
83
+ sniperData .wzrd_url = '#'
84
+ else :
85
+ sniperData .wzrd_url = "https://wzrd.in/bundle/" + component .name
86
+ except :
87
+ sniperData .wzrd_url = "https://wzrd.in/bundle/" + component .name
88
+ try :
89
+ snippets_dir_name = sniper_data ['snippets' ][0 ]
90
+ sniperData .snippets_dir_name = snippets_dir_name
91
+ except :
92
+ pass
93
+ sniperData .save ()
94
+
95
+ ### For Snippets URLs
96
+ try :
97
+ print ("https://api.github.com/repos/" + str (owner ) + "/" + str (repo_name ) + "/contents/" + sniperData .snippets_dir_name + "?ref=master&client_id="
98
+ + GITHUB_CLIENT_ID + "&client_secret=" + GITHUB_CLIENT_SECRET )
99
+ snippets_data = urllib .urlopen ("https://api.github.com/repos/" + str (owner ) + "/" + str (repo_name ) + "/contents/" + sniperData .snippets_dir_name + "?ref=master&client_id="
100
+ + GITHUB_CLIENT_ID + "&client_secret=" + GITHUB_CLIENT_SECRET )
101
+ snippets = json .loads (snippets_data .read ())
102
+ for snippet in snippets :
103
+ if not snippet ['name' ].endswith ('.js' ):
104
+ continue
105
+ url = "https://cdn.rawgit.com/" + str (owner ) + '/' + str (repo_name ) + "/" + commit_hash + "/" + sniperData .snippets_dir_name + "/" + snippet ['name' ]
106
+ try :
107
+ req_snippet = Snippet .objects .get (name = snippet ['name' ], sniperData = sniperData )
108
+ req_snippet .url = url
109
+ req_snippet .save ()
110
+ except :
111
+ Snippet .objects .create (name = snippet ['name' ], sniperData = sniperData , url = url )
112
+ except :
113
+ pass
11
114
12
115
def get_github_data (github_url ):
13
116
response = urllib .urlopen (github_url )
@@ -18,6 +121,7 @@ def get_npm_data():
18
121
# response = urllib2.urlopen()
19
122
hdr = {'User-Agent' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11' }
20
123
req = urllib2 .Request ("http://registry.npmjs.com/-/v1/search?text=keywords:biojs,bionode&size=500" , headers = hdr )
124
+ # req = urllib2.Request("http://registry.npmjs.com/-/v1/search?text=biojs-vis-msa&size=1", headers=hdr)
21
125
response = urllib2 .urlopen (req )
22
126
data = json .load (response )
23
127
return data
@@ -28,8 +132,7 @@ def get_contributors_data(contributors_url):
28
132
return data
29
133
30
134
def get_downloads (downloads_url ):
31
- import ast
32
- print downloads_url
135
+ print (downloads_url )
33
136
response = urllib .urlopen (downloads_url )
34
137
downloads = 0
35
138
data = ast .literal_eval (response .read ())
@@ -48,10 +151,10 @@ def handle(self, *args, **options):
48
151
component_data = component ['package' ]
49
152
try :
50
153
_component = Component .objects .get (name = component_data ['name' ])
51
- print 'exists'
154
+ print ( 'exists' )
52
155
except :
53
156
_component = Component .objects .create (name = component_data ['name' ])
54
- print _component .name
157
+ print ( _component .name )
55
158
try :
56
159
_component .version = component_data ['version' ]
57
160
except :
@@ -104,7 +207,7 @@ def handle(self, *args, **options):
104
207
_component .save ()
105
208
106
209
if _component .github_url :
107
- print _component .github_url
210
+ print ( _component .github_url )
108
211
try :
109
212
github_data = get_github_data (_component .github_url )
110
213
except :
@@ -126,8 +229,25 @@ def handle(self, *args, **options):
126
229
_component .created_time = aware_date
127
230
except :
128
231
pass
232
+ # try:
233
+ str_date = github_data ['updated_at' ]
234
+ req_date = datetime .strptime (str_date , "%Y-%m-%dT%H:%M:%SZ" ) #This object is timezone unaware
235
+ aware_date = pytz .utc .localize (req_date ) #This object is now timezone aware
236
+ # if _component.github_update_time:
237
+ # if aware_date > _component.github_update_time:
238
+ # _component.github_updated_time = aware_date
239
+ # latest_commit_hash = get_commit_hash(github_data['commits_url'])
240
+ # _component.latest_commit_hash = latest_commit_hash
241
+ # update_visualizations(_component, latest_commit_hash)
242
+ # else:
243
+ _component .github_update_time = aware_date
244
+ latest_commit_hash = get_commit_hash (github_data ['commits_url' ])
245
+ _component .latest_commit_hash = latest_commit_hash
246
+ update_visualizations (_component , latest_commit_hash )
247
+ # except:
248
+ # pass
129
249
_component .save ()
130
- print str (github_data ['contributors_url' ]) + '?client_id=' + GITHUB_CLIENT_ID + '&client_secret=' + GITHUB_CLIENT_SECRET
250
+ print ( str (github_data ['contributors_url' ]) + '?client_id=' + GITHUB_CLIENT_ID + '&client_secret=' + GITHUB_CLIENT_SECRET )
131
251
try :
132
252
contributors_data = get_contributors_data (str (github_data ['contributors_url' ]) + '?client_id=' + GITHUB_CLIENT_ID + '&client_secret=' + GITHUB_CLIENT_SECRET )
133
253
except :
0 commit comments