changeset 68:525a52169f60

Make FeedEntry table object available in Mapping to use it to expire old, read feed entries.
author Dirk Olmes <dirk@xanthippe.ping.de>
date Tue, 10 Aug 2010 03:32:29 +0200
parents c3f2b11e7420
children d20e99d46d78
files Mapping.py feedupdate-main.py
diffstat 2 files changed, 22 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/Mapping.py	Mon Aug 02 02:43:41 2010 +0200
+++ b/Mapping.py	Tue Aug 10 03:32:29 2010 +0200
@@ -15,6 +15,7 @@
 from sqlalchemy.orm import relation
 
 mappingDefined = False
+feedEntryTable = None
 
 def createMapping(engine):
     """ Make sure the mapping is defined only once. This is not really needed for the feed updater
@@ -26,6 +27,7 @@
 
 def _createMapping(engine):
     metadata = MetaData(engine)
+    metadata.bind = engine
     
     feedTable = Table("feed", metadata,
         Column("pk", Integer, primary_key=True),
@@ -38,6 +40,7 @@
         Column("auto_load_entry_link", Boolean, nullable=False)
     )
     
+    global feedEntryTable
     feedEntryTable = Table("feed_entry", metadata,
         Column("pk", Integer, primary_key=True),
         Column("create_timestamp", DateTime, nullable=False),
--- a/feedupdate-main.py	Mon Aug 02 02:43:41 2010 +0200
+++ b/feedupdate-main.py	Tue Aug 10 03:32:29 2010 +0200
@@ -1,9 +1,12 @@
 #!/usr/bin/env python
 
 import Database
+from datetime import datetime, timedelta
 from Feed import Feed
 import FeedUpdater
 import logging
+import Mapping
+from sqlalchemy.sql import and_
 import util
 
 logger = logging.getLogger("feedupdater")
@@ -15,6 +18,20 @@
         for entry in feed.entries:
             print entry.title
 
+def expireFeedEntries(session):
+    expireDate = _calculateExpireDate()
+    logger.info("expiring entries older than " + str(expireDate))
+    feedEntry = Mapping.feedEntryTable
+    deleteStatement = feedEntry.delete().where(
+        and_(feedEntry.c.create_timestamp < expireDate, feedEntry.c.read == 1)
+    )
+    deleteStatement.execute()
+
+def _calculateExpireDate():
+    now = datetime.now()
+    delta = timedelta(days=30)
+    return now - delta
+
 if __name__ == "__main__":
     util.configureLogging()
     session = Database.createSession()
@@ -23,6 +40,7 @@
     #util.forceUpdateAllFeeds(session)
     #listFeeds(session)
     #OpmlImport.createFeedsFromOPML(session, "feed-data/feeds.opml.xml")
-    FeedUpdater.updateAllFeeds(session)
+#    FeedUpdater.updateAllFeeds(session)
+    expireFeedEntries(session)
     
     session.close()