mirror of
https://github.com/heyarne/berliner-winter.git
synced 2026-05-06 19:23:39 +02:00
Return only the best place for each article
This commit is contained in:
parent
677a839012
commit
7846dad0a8
1 changed files with 29 additions and 18 deletions
37
server.py
37
server.py
|
|
@ -29,28 +29,39 @@ def index():
|
||||||
@bottle.get("/articles/")
|
@bottle.get("/articles/")
|
||||||
def articles():
|
def articles():
|
||||||
conn = sqlite3.connect('violence.db')
|
conn = sqlite3.connect('violence.db')
|
||||||
c = conn.cursor()
|
cursor = conn.cursor()
|
||||||
c = c.execute("""
|
|
||||||
SELECT article.id, article.date, article.place, article.description, category.name,
|
l = cursor.execute("""
|
||||||
location.confidence, location.lat, location.lng, location.match, location.returned_place
|
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
|
FROM article
|
||||||
JOIN category ON article.id = category.article_id
|
JOIN category ON article.id = category.article_id
|
||||||
JOIN location ON article.id = location.article_id
|
|
||||||
""")
|
""")
|
||||||
|
|
||||||
articles = []
|
articles = []
|
||||||
for article in c.fetchall():
|
for article in c.fetchall():
|
||||||
|
article_id = article[0]
|
||||||
|
if locations.get(article_id):
|
||||||
articles.append({
|
articles.append({
|
||||||
"id": article[0],
|
"id": article_id,
|
||||||
"date": article[1],
|
"date": article[1],
|
||||||
"place": article[2],
|
"place": article[2],
|
||||||
"desc": article[3],
|
"description": article[3],
|
||||||
"cat": article[4],
|
"category": article[4],
|
||||||
"conf": article[5],
|
"lat": locations[article_id][0],
|
||||||
"lat": article[6],
|
"lng": locations[article_id][1],
|
||||||
"lng": article[7],
|
"place": locations[article_id][2]
|
||||||
"match": article[8],
|
|
||||||
"returned_place": article[9]
|
|
||||||
})
|
})
|
||||||
conn.close()
|
conn.close()
|
||||||
return json.dumps(articles)
|
return json.dumps(articles)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue