# HG changeset patch # User Dirk Olmes # Date 1552354882 -3600 # Node ID 304917762618abc15c964c7409fd98b2c0841134 # Parent 4ca1fac32dde65fe4ada49563bf4bb90393ed373 implementation of feed updates diff -r 4ca1fac32dde -r 304917762618 backend/arangodb/ArangoBackend.py --- a/backend/arangodb/ArangoBackend.py Tue Mar 12 02:39:18 2019 +0100 +++ b/backend/arangodb/ArangoBackend.py Tue Mar 12 02:41:22 2019 +0100 @@ -4,6 +4,7 @@ from backend.AbstractBackend import AbstractBackend from Feed import Feed from FeedEntry import FeedEntry +from FeedUpdater import FeedUpdater from Preferences import Preferences from pyArango.connection import Connection @@ -40,7 +41,7 @@ FILTER feed_entry_doc.feed == @feed_key""" if hideReadEntries: query = base_query + " AND feed_entry_doc.read == false " - query = query + " RETURN feed_entry_doc" + query = query + " SORT feed_entry_doc.updated RETURN feed_entry_doc" bind_vars = { 'feed_key': self.selectedFeed._key } results = self.database.AQLQuery(query, bind_vars=bind_vars) return [FeedEntry(doc) for doc in results] @@ -50,4 +51,4 @@ def updateAllFeeds(self): for feed in Feed.all_pending_update(self.database): - print('updating ' + feed.title) + FeedUpdater(self.database, self.preferences()).update(feed) diff -r 4ca1fac32dde -r 304917762618 backend/arangodb/Feed.py --- a/backend/arangodb/Feed.py Tue Mar 12 02:39:18 2019 +0100 +++ b/backend/arangodb/Feed.py Tue Mar 12 02:41:22 2019 +0100 @@ -1,15 +1,8 @@ # -*- coding: utf-8 -*- -from datetime import datetime +from datetime import datetime, timedelta class Feed(object): - def __init__(self, document): - super(Feed, self).__init__() - self.document = document - - def __getattr__(self, attribute): - return self.document[attribute] - @staticmethod def get_unread(database): query = """ @@ -30,3 +23,20 @@ """ results = database.AQLQuery(query) return [Feed(doc) for doc in results] + + def __init__(self, document): + super(Feed, self).__init__() + self.document = document + + def __getattr__(self, attribute): + return self.document[attribute] + + def increment_next_update_date(self): + updateInterval = self._updateIntervalInSeconds() + delta = timedelta(seconds=updateInterval) + next_update = datetime.now() + delta + self.document['next_update'] = next_update.strftime('%Y-%m-%dT%H:%M:%S.000') + self.document.patch() + + def _updateIntervalInSeconds(self): + return self.update_interval * 60 diff -r 4ca1fac32dde -r 304917762618 backend/arangodb/FeedEntry.py --- a/backend/arangodb/FeedEntry.py Tue Mar 12 02:39:18 2019 +0100 +++ b/backend/arangodb/FeedEntry.py Tue Mar 12 02:41:22 2019 +0100 @@ -2,18 +2,32 @@ from datetime import datetime class FeedEntry(object): + @staticmethod + def find_by_link(link, database): + query = """ + FOR feed_entry_doc IN feed_entry + FILTER feed_entry_doc.link == @link + RETURN feed_entry_doc + """ + bind_vars = { 'link': link } + results = database.AQLQuery(query, bind_vars) + if len(results) > 0: + return FeedEntry[results[0]] + else: + return None + def __init__(self, document): super(FeedEntry, self).__init__() self.document = document def __getattr__(self, attribute): - if attribute == 'updated': - return self._parse_updated() + if attribute == 'updated' or attribute == 'created': + return self._parse_date(attribute) return self.document[attribute] - def _parse_updated(self): - value = self.document['updated'] - return datetime(*value) + def _parse_date(self, attribute): + value = self.document[attribute] + return datetime.strptime(value, '%Y-%m-%dT%H:%M:%S.%f') def markRead(self): self.document['read'] = True diff -r 4ca1fac32dde -r 304917762618 backend/arangodb/FeedUpdater.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/backend/arangodb/FeedUpdater.py Tue Mar 12 02:41:22 2019 +0100 @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +from backend.AbstractFeedUpdater import AbstractFeedUpdater +from FeedEntry import FeedEntry + +class FeedUpdater(AbstractFeedUpdater): + def __init__(self, database, preferences): + super(FeedUpdater, self).__init__(preferences) + self.database = database + + def _incrementFeedUpdateDate(self): + self.feed.increment_next_update_date() + + def _processEntry(self, entry): + feed_entry = FeedEntry.find_by_link(entry.link, self.database) + if feed_entry is None: + self._createFeedEntry(entry) \ No newline at end of file