# HG changeset patch # User Dirk Olmes # Date 1281403949 -7200 # Node ID 525a52169f6021444358f21d6e6440abda3d3740 # Parent c3f2b11e7420b31e9f80552908441f93cac82ea3 Make FeedEntry table object available in Mapping to use it to expire old, read feed entries. diff -r c3f2b11e7420 -r 525a52169f60 Mapping.py --- 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), diff -r c3f2b11e7420 -r 525a52169f60 feedupdate-main.py --- 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()