From 32bc3b906bd35edacc763004d26dc6b6e97fe28c Mon Sep 17 00:00:00 2001 From: Jason Schwarzenberger Date: Thu, 19 Nov 2020 15:06:55 +1300 Subject: [PATCH] add update-story.py --- apiserver/database.py | 7 ++++++ apiserver/update-story.py | 45 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 apiserver/update-story.py diff --git a/apiserver/database.py b/apiserver/database.py index 6ab5aaa..be791ea 100644 --- a/apiserver/database.py +++ b/apiserver/database.py @@ -73,6 +73,13 @@ def get_stories_by_url(url): filter(Story.meta['url'].as_string() == url).\ order_by(Story.meta['date'].desc()) +def get_ref_by_sid(sid): + session = Session() + x = session.query(Reflist).\ + filter(Reflist.sid == sid).\ + first() + return dict(ref=x.ref, sid=x.sid, source=x.source, urlref=x.urlref) + def get_reflist(): session = Session() q = session.query(Reflist).order_by(Reflist.rid.desc()) diff --git a/apiserver/update-story.py b/apiserver/update-story.py new file mode 100644 index 0000000..68a8dfa --- /dev/null +++ b/apiserver/update-story.py @@ -0,0 +1,45 @@ +import logging +logging.basicConfig( + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', + level=logging.INFO) + +import sys +import json +import requests + +import database +import feed +import search + +database.init() +search.init() + +def _update_current_story(story, item): + logging.info('Updating story: {}'.format(str(story['ref']))) + + valid = feed.update_story(story, urlref=item['urlref']) + if valid: + database.put_story(story) + search.put_story(story) + else: + database.del_ref(item['ref']) + logging.info('Removed ref {}'.format(item['ref'])) + +if __name__ == '__main__': + if len(sys.argv) == 2: + sid = sys.argv[1] + else: + print('Usage: python delete-story.py [story id]') + exit(1) + + item = database.get_ref_by_sid(sid) + + if item: + story = database.get_story(item['sid']).data + if story: + print('Updating story:') + _update_current_story(story, item) + else: + print('Story not found. Exiting.') + else: + print('Story not found. Exiting.') \ No newline at end of file