Use the name of in the config file as the name of the feed.

Fix bug where if feed fails to load the last update time is lost.
3 files changed, 48 insertions(+), 39 deletions(-)

M feed2maildir/converter.py
M feed2maildir/reader.py
M scripts/feed2maildir
M feed2maildir/converter.py +42 -36
@@ 98,7 98,7 @@ Content-Type: text/plain
                     self.write(self.compose(newfeed, newpost))
 
             with open(self.db, 'w') as f:
-                f.write(json.dumps(newtimes))
+                f.write(json.dumps(newtimes, indent=2))
 
     def load(self, feeds):
         """Load a list of feeds in feedparser-dict form"""

          
@@ 109,44 109,50 @@ Content-Type: text/plain
         refreshing the db"""
         new = {}
         newtimes = {}
-        for feed in feeds:
-            feedname = feed.feed.title
-            try: # to get the update time from the feed itself
-                feedup = self.mktime(feed.feed.updated)
-            except: # there is no info, then find it in the posts
-                feedup = self.find_update_time(feed)
-            try: # to localize the timezone
-                feedup = feedup.astimezone(dateutil.tz.tzutc())
-            except: # it is naive, force UTC
-                feedup = feedup.replace(tzinfo=dateutil.tz.tzutc())
-            try: # to find the old update time in the db
-                oldtime = self.mktime(db[feedname]).astimezone(
-                            dateutil.tz.tzutc())
-            except: # it is not there
-                oldtime = None
-            if oldtime and not oldtime.tzinfo: # force UTC
-                oldtime = oldtime.replace(tzinfo=dateutil.tz.tzutc())
+        for feedname, feed in feeds.items():
+            if feed is not None:
+                try: # to get the update time from the feed itself
+                    feedup = self.mktime(feed.feed.updated)
+                except: # there is no info, then find it in the posts
+                    feedup = self.find_update_time(feed)
+                try: # to localize the timezone
+                    feedup = feedup.astimezone(dateutil.tz.tzutc())
+                except: # it is naive, force UTC
+                    feedup = feedup.replace(tzinfo=dateutil.tz.tzutc())
+                try: # to find the old update time in the db
+                    oldtime = self.mktime(db[feedname]).astimezone(
+                                dateutil.tz.tzutc())
+                except: # it is not there
+                    oldtime = None
+                if oldtime and not oldtime.tzinfo: # force UTC
+                    oldtime = oldtime.replace(tzinfo=dateutil.tz.tzutc())
 
-            if not oldtime or oldtime < feedup:
-                for post in feed.entries:
-                    feedtime = self.post_update_time(post)
-                    try: # to localize the timezone
-                        feedtime = feedtime.astimezone(dateutil.tz.tzutc())
-                    except: # it is naive
-                        feedtime = feedtime.replace(tzinfo=dateutil.tz.tzutc())
-                    if not oldtime or oldtime < feedtime:
-                        feedup = max(feedup, feedtime)
-                        self.output('Feedname: {} : Oldtime : {} : Feedtime: {} : Link : {}'.format(
-                            feedname,
-                            oldtime,
-                            feedtime,
-                            post.link))
-                        new.setdefault(feedname, []).append(post)
+                if not oldtime or oldtime < feedup:
+                    for post in feed.entries:
+                        feedtime = self.post_update_time(post)
+                        try: # to localize the timezone
+                            feedtime = feedtime.astimezone(dateutil.tz.tzutc())
+                        except: # it is naive
+                            feedtime = feedtime.replace(tzinfo=dateutil.tz.tzutc())
+                        if not oldtime or oldtime < feedtime:
+                            feedup = max(feedup, feedtime)
+                            self.output('Feedname: {} : Oldtime : {} : Feedtime: {} : Link : {}'.format(
+                                feedname,
+                                oldtime,
+                                feedtime,
+                                post.link))
+                            new.setdefault(feedname, []).append(post)
 
-            if oldtime is not None:
-                newtimes[feedname] = max(oldtime, feedup).strftime('%Y-%m-%d %H:%M:%S %Z')
+                if oldtime is not None:
+                    newtimes[feedname] = max(oldtime, feedup).strftime('%Y-%m-%d %H:%M:%S %Z')
+                else:
+                    newtimes[feedname] = feedup.strftime('%Y-%m-%d %H:%M:%S %Z')
+
             else:
-                newtimes[feedname] = feedup.strftime('%Y-%m-%d %H:%M:%S %Z')
+                if feedname in db:
+                    newtimes[feedname] = self.mktime(
+                        db[feedname]).astimezone(
+                            dateutil.tz.tzutc()).strftime('%Y-%m-%d %H:%M:%S %Z')
 
             self.output('Feedname : {} : Feedup : {}'.format(feedname, newtimes[feedname]))
 

          
M feed2maildir/reader.py +3 -2
@@ 4,14 4,15 @@ class Reader:
     """Get updates on the feeds supplied"""
 
     def __init__(self, feeds, silent=False):
-        self.feeds = []
+        self.feeds = {}
         self.silent = silent
         for feed in feeds:
             f = feedparser.parse(feeds[feed])
             if f.bozo:
                 self.output('WARNING: could not parse feed {}'.format(feed))
+                self.feeds[feed] = None
             else:
-                self.feeds.append(f)
+                self.feeds[feed] = f
 
     def output(self, arg):
         if not self.silent:

          
M scripts/feed2maildir +3 -1
@@ 40,7 40,9 @@ def main():
     else:
         maildir = config['maildir']
 
-    converter = Converter(db=db, maildir=maildir, strip=args['s'],
+    converter = Converter(db=db,
+                          maildir=maildir,
+                          strip=args['s'],
                           links=args['l'])
     converter.load(reader.feeds)
     converter.run()