-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
67 lines (57 loc) · 2.25 KB
/
server.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
import bottle
import sqlite3
import json
import os
# Fix path for uberspace
import sys
try:
os.chdir(os.path.dirname(sys.argv[0]))
except:
pass
@bottle.get("/")
def index():
return bottle.template('index')
@bottle.get("/articles/")
def articles():
conn = sqlite3.connect('violence.db')
cursor = conn.cursor()
l = cursor.execute("""
SELECT location.lat, location.lng, location.returned_place, location.article_id
FROM location
ORDER BY location.confidence DESC, location.id ASC
""")
locations = {}
for location in l.fetchall():
# check if we already have entries for the article_id
if not locations.get(location[3]):
locations[location[3]] = (location[0], location[1], location[2])
c = cursor.execute("""
SELECT article.id, article.date, article.place, article.description, category.name
FROM article
LEFT OUTER JOIN category ON article.id = category.article_id
""")
articles = {}
for article in c.fetchall():
article_id = article[0]
if locations.get(article_id):
if articles.get(article_id):
articles[article_id]["categories"].append(article[4])
else:
articles[article_id] = {
"id": article_id,
"date": article[1],
"place": article[2],
"description": article[3],
"categories": [article[4]] if article[4] else [],
"lat": locations[article_id][0],
"lng": locations[article_id][1],
"place": locations[article_id][2]
}
conn.close()
bottle.response.content_type = "application/json"
return json.dumps([article for article_id, article in articles.items()])
@bottle.get('/static/<filepath:path>')
def server_static(filepath):
return bottle.static_file(filepath, root='static/')
if __name__ == "__main__":
bottle.run(host="localhost", port=os.environ.get('OPEN_DATA_PORT', 12345), reloader=True, debug=True)