This repository has been archived by the owner on Aug 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.py
executable file
·348 lines (242 loc) · 10.9 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
from pulseweb import app
from pulseweb.db import query_db
from werkzeug.contrib.cache import SimpleCache
from flask import request
import time
from datetime import date
cache = SimpleCache()
import simplejson as json
# date_offset =
@app.route('/data/hyperstreams')
def hyperstreams():
streams = []
scope = {}
for s in query_db('SELECT min(y_pos), max(y_pos), suprathematique FROM clusters, positions_superstreams WHERE clusters.cluster_univ_id = positions_superstreams.cluster_univ_id GROUP BY clusters.suprathematique'):
scope[ s["suprathematique"] ] = {}
scope[ s["suprathematique"] ]["min_y"] = s["min(y_pos)"]
scope[ s["suprathematique"] ]["max_y"] = s["max(y_pos)"]
scope[ s["suprathematique"] ]["height"] = s["max(y_pos)"] + 1
for s in query_db('SELECT *, COUNT(distinct period) AS period_count FROM clusters GROUP BY suprathematique ORDER BY suprathematique'):
if s["period_count"] == 1 : continue
stream = {}
stream["id"] = s["suprathematique"]
stream["group"] = s["suprathematique"]
stream["title"] = s["suprathematique_label"]
stream["clusters"] = []
stream["links"] = []
stream["min_y"] = scope[stream["id"]]["min_y"]
stream["max_y"] = scope[stream["id"]]["max_y"]
stream["height"]= scope[stream["id"]]["height"]
clusters = {}
for t in query_db('SELECT clusters.cluster_univ_id, pos_x, y_pos FROM clusters, positions_superstreams WHERE clusters.suprathematique = %i AND clusters.cluster_univ_id = positions_superstreams.cluster_univ_id GROUP BY clusters.cluster_univ_id' % (s["suprathematique"])):
cluster = {}
cluster["id"] = t["cluster_univ_id"]
cluster["x"] = t["pos_x"]
cluster["y"] = t["y_pos"] - stream["min_y"]
clusters[cluster["id"]] = cluster
stream["clusters"].append(cluster)
for l in query_db('SELECT *, suprathematique FROM phylogeny, clusters WHERE clusters.suprathematique = %i AND clusters.stream_id = phylogeny.stream_id GROUP BY id0' % (s["suprathematique"]) ):
l_light = {}
l_light["previous"] = l["previous_cluster_univ_id"]
l_light["current"] = l["current_cluster_univ_id"]
l_light["start"] = {}
l_light["start"]["x"] = clusters[l_light["previous"]]["x"]
l_light["start"]["y"] = clusters[l_light["previous"]]["y"]
l_light["end"] = {}
l_light["end"]["x"] = clusters[l_light["current"]]["x"]
l_light["end"]["y"] = clusters[l_light["current"]]["y"]
stream["links"].append(l_light)
streams.append(stream)
return json.dumps(streams)
@app.route('/data/streams')
def streams():
streams = []
for s in query_db('SELECT *, COUNT(distinct period) AS period_count FROM clusters GROUP BY stream_id ORDER BY suprathematique'):
if s["period_count"] == 1 : continue
stream = {}
stream["id"] = s["stream_id"]
stream["group"] = s["suprathematique"]
stream["title"] = s["stream_label"]
stream["clusters"] = []
stream["links"] = []
# there is certainly a way to speed up this part
stream_min_max_y = query_db('SELECT min(y_pos), max(y_pos) FROM clusters, positions_metrolines_cam WHERE clusters.stream_id = %i AND clusters.cluster_univ_id = positions_metrolines_cam.cluster_univ_id GROUP BY clusters.stream_id' % (s["stream_id"]));
if(stream_min_max_y):
stream["min_y"] = stream_min_max_y[0]["min(y_pos)"]
stream["max_y"] = stream_min_max_y[0]["max(y_pos)"]
stream["height"]= stream["max_y"] - stream["min_y"] + 1
clusters = {}
for t in query_db('SELECT clusters.*, positions_metrolines_cam.* FROM clusters, positions_metrolines_cam WHERE clusters.stream_id = %i AND clusters.cluster_univ_id = positions_metrolines_cam.cluster_univ_id GROUP BY clusters.cluster_univ_id ORDER BY pos_y' % (s["stream_id"])):
cluster = {}
cluster["id"] = t["cluster_univ_id"]
cluster["x"] = t["pos_x"]
cluster["y"] = t["y_pos"] - stream["min_y"]
clusters[cluster["id"]] = cluster
stream["clusters"].append(cluster)
for l in query_db('SELECT * FROM phylogeny WHERE stream_id = %i' % (s["stream_id"])):
l_light = {}
l_light["previous"] = l["previous_cluster_univ_id"]
l_light["current"] = l["current_cluster_univ_id"]
l_light["start"] = {}
l_light["start"]["x"] = clusters[l_light["previous"]]["x"]
l_light["start"]["y"] = clusters[l_light["previous"]]["y"]
l_light["end"] = {}
l_light["end"]["x"] = clusters[l_light["current"]]["x"]
l_light["end"]["y"] = clusters[l_light["current"]]["y"]
stream["links"].append(l_light)
streams.append(stream)
return json.dumps(streams)
@app.route('/data/tubes')
def tubes():
return "tubes"
@app.route('/data/clusters')
def clusters():
clusters = []
resp = cache.get("clusters")
average = {}
for r in query_db('SELECT * FROM cluster_average'):
average[ r["cluster_univ_id"] ] = r["average"]
if resp is None:
for cluster in query_db('SELECT * FROM clusters WHERE isolated = 0 GROUP BY cluster_univ_id ORDER BY AVG(pos_y)'):
# , count(distinct period) as c
# Reduction des donnees transferees 1.4 mo -> 230 ko
cluster_light = {}
cluster_light["label"] = cluster["cluster_label"]
cluster_light["id"] = cluster["cluster_univ_id"]
cluster_light["x"] = cluster["pos_x"]
cluster_light["x_average"] = average[ cluster_light["id"] ]
cluster_light["x_norm"] = cluster["pos_x_t"]
cluster_light["y"] = cluster["pos_y"]
cluster_light["w"] = cluster["width"]
t = cluster["period"].split("_")
cluster_light["period_length"] = int(t[1]) - int(t[0])
cluster_light["start"] = int(t[0])
cluster_light["end"] = int(t[1])
cluster_light["stream_id"] = cluster["stream_id"]
cluster_light["group_id"] = cluster["suprathematique"]
clusters.append(cluster_light)
resp = clusters
cache.set('clusters', resp, timeout=5 * 60)
return json.dumps(resp)
@app.route('/data/cluster/<int:cluster_id>')
def cluster_info(cluster_id):
resp = []
for t in query_db('SELECT * FROM clusters WHERE cluster_univ_id = %i ORDER BY weight DESC' % int(cluster_id) ):
term = {}
term["id"] = t["id0"]
term["cluster_id"] = cluster_id
term["label"] = t["term"]
# term["term_id"] = t["term_id"]
term["count_articles"] = int( t["width"] * t["weight"] )
resp.append(term)
return json.dumps(resp)
@app.route('/data/cluster/<int:cluster_id>/geo')
def cluster_geo(cluster_id):
resp = []
countries = {}
for c in query_db('SELECT iso, name, capital_lat, capital_long FROM countries'):
countries[ c["iso"] ] = c
for a in query_db('SELECT * from cluster_country_weight WHERE cluster_univ_id = %i ORDER BY weight DESC' % int(cluster_id) ):
if(a["weight"] < 2):
continue
countries[ a["iso"] ]["weight"] = a["weight"]
resp.append(countries[ a["iso"] ])
return json.dumps(resp)
@app.route('/data/cluster/<int:cluster_id>/term/<term_id>')
def cluster_term_articles(cluster_id, term_id):
resp = []
for t in query_db('SELECT article_id FROM cluster_term_article, terms WHERE cluster_univ_id = %i AND cluster_term_article.term_id = terms.id AND terms.term = "%s" ' % (cluster_id, term_id) ):
resp.append(t["article_id"])
resp = sorted ( set(resp) )
return json.dumps(resp)
@app.route('/data/cluster/<int:cluster_id>/term/<term_id>/full')
def cluster_term_articles_full(cluster_id, term_id):
resp = []
a = []
for t in query_db('SELECT article_id FROM cluster_term_article, terms WHERE cluster_univ_id = %i AND cluster_term_article.term_id = terms.id AND terms.term = "%s" ' % (cluster_id, term_id) ):
a.append(str( t["article_id"] ) )
a = sorted ( set(a) )
for article in query_db('SELECT * FROM articles WHERE id IN (%s)' % (','.join(a)) ):
resp.append(row_to_article(article));
return json.dumps(resp)
@app.route('/data/cluster/<int:cluster_id>/country/<country_id>/full')
def cluster_country_full(cluster_id, country_id):
resp = []
# for article in query_db('SELECT articles.* from articles, region_weight, cluster_article WHERE region_weight.id = article_id AND countrycode = "%s" AND cluster_univ_id = %i AND articles.id = region_weight.id' % (country_id, cluster_id) ):
# resp.append(row_to_article(article));
t = []
for r in query_db('SELECT id from region_weight, cluster_article WHERE region_weight.id = article_id AND countrycode = "%s" AND cluster_univ_id = %i' % (country_id, cluster_id) ):
t.append(str( r["id"] ));
for article in query_db('SELECT * FROM articles WHERE id IN (%s)' % (','.join(t)) ):
resp.append(row_to_article(article));
return json.dumps(resp) #json.dumps(request.headers["accept"].partition(',')[0]) #json.dumps(resp)
@app.route('/data/clusters/positions/metrolines')
def clusters_metrolines():
clusters = []
resp = cache.get("clusters")
if resp is None:
for cluster in query_db('select clusters.*, positions_metrolines_cam.* from clusters, positions_metrolines_cam WHERE clusters.cluster_univ_id = positions_metrolines_cam.cluster_univ_id group by cluster_univ_id'):
# Reduction des donnees transferees 1.4 mo -> 230 ko
cluster_light = {}
cluster_light["label"] = cluster["cluster_label"]
cluster_light["id"] = cluster["cluster_univ_id"]
cluster_light["x"] = cluster["pos_x_t"]
cluster_light["y"] = cluster["y_pos"]
cluster_light["w"] = cluster["width"]
t = cluster["period"].split("_")
cluster_light["period_length"] = int(t[1]) - int(t[0])
cluster_light["start"] = int(t[0])
cluster_light["end"] = int(t[1])
cluster_light["stream_id"] = cluster["stream_id"]
clusters.append(cluster_light)
resp = clusters
cache.set('clusters', resp, timeout=5 * 60)
return json.dumps(resp)
@app.route('/data/clusters/links')
def clusters_links():
links = []
for l in query_db('select * from phylogeny order by stream_id'):
l_light = {}
l_light["previous"] = l["previous_cluster_univ_id"]
l_light["current"] = l["current_cluster_univ_id"]
links.append(l_light)
return json.dumps(links)
@app.route("/data/meta")
def meta():
# SELECT pos_y, SUM(width) FROM
# (SELECT * FROM clusters GROUP BY cluster_univ_id)
# GROUP BY pos_y
pass
def row_to_article(r):
o = {}
o["id"] = r["id"]
o["title"] = r["title"]
o["source"] = r["source"]
o["author"] = r["author"]
o["lang"] = r["lang"]
# o["date_offset"] = date.fromtimestamp( 946681200 ).strftime("%d/%m/%Y")
# 946681200
# 978306360
o["date"] = date.fromtimestamp( 946681200 + int(r["date"]) * 86400 ).strftime("%d/%m/%Y")
o["content"] = r["abstract"]
#q = 'SELECT terms_id FROM articles2terms WHERE wos_id = %i' % r["id"]
return o
@app.route("/data/article/<int:article_id>")
def get_article(article_id):
resp = {}
a = query_db('SELECT * FROM articles WHERE id=%i' % article_id)[0]
resp = row_to_article(a)
# 1314262254.484391
# 9783063600
return json.dumps(resp)
@app.route("/data/distribution/articles/by_month")
def articles_month():
q = "select count(*) as size, strftime('%Y-%m', data) as month from publicationDate group by strftime('%Y-%m', data)"
distribution = {}
for m in query_db(q):
d = m["month"].split("-")
# if type(distribution[ d[0] ]) is not dict:
if not distribution.has_key( d[0] ):
distribution[ d[0] ] = {}
distribution[ d[0] ][ d[1] ] = m["size"]
return json.dumps(distribution)