From 5efc6ef2d31728fb5589047068422303b8bb984f Mon Sep 17 00:00:00 2001 From: Jason Schwarzenberger Date: Tue, 10 Nov 2020 14:09:56 +1300 Subject: [PATCH] add related stories (in api only) --- apiserver/database.py | 9 +++++++-- apiserver/feeds/news.py | 2 ++ apiserver/server.py | 4 +++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/apiserver/database.py b/apiserver/database.py index 5f0e659..f6026a8 100644 --- a/apiserver/database.py +++ b/apiserver/database.py @@ -1,6 +1,4 @@ -import json from datetime import datetime, timedelta - from sqlalchemy import create_engine, Column, String, ForeignKey, Integer from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker @@ -67,6 +65,13 @@ def get_story_by_ref(ref): session = Session() return session.query(Story).filter(Story.ref==ref).first() +def get_stories_by_url(url): + session = Session() + return session.query(Story).\ + filter(Story.title != None).\ + filter(Story.meta['url'].as_string() == url).\ + order_by(Story.meta['date'].desc()) + def get_reflist(): session = Session() q = session.query(Reflist).order_by(Reflist.rid.desc()) diff --git a/apiserver/feeds/news.py b/apiserver/feeds/news.py index 0e3d510..600a9be 100644 --- a/apiserver/feeds/news.py +++ b/apiserver/feeds/news.py @@ -163,6 +163,8 @@ def get_sitemap_date(a): return a.find('lastmod').text if a.find('news:publication_date'): return a.find('news:publication_date').text + if a.find('ns2:publication_date'): + return a.find('ns2:publication_date').text return '' class Sitemap(_Base): diff --git a/apiserver/server.py b/apiserver/server.py index 2d295cf..f522534 100644 --- a/apiserver/server.py +++ b/apiserver/server.py @@ -99,7 +99,9 @@ def submit(): def story(sid): story = database.get_story(sid) if story: - res = Response(json.dumps({"story": story.data})) + related = database.get_stories_by_url(story.meta['url']) + related = [r.meta for r in related] + res = Response(json.dumps({"story": story.data, "related": related})) res.headers['content-type'] = 'application/json' return res else: