# HG changeset patch # User Malcolm Matalka # Date 1595087740 -7200 # Sat Jul 18 17:55:40 2020 +0200 # Node ID 83299548544242b1f151cefdc226f255727cd81a # Parent 39bbc86f82b7d84943f3a2bca8438c2f4f967adf 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. diff --git a/feed2maildir/converter.py b/feed2maildir/converter.py --- a/feed2maildir/converter.py +++ b/feed2maildir/converter.py @@ -98,7 +98,7 @@ 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 @@ 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])) diff --git a/feed2maildir/reader.py b/feed2maildir/reader.py --- a/feed2maildir/reader.py +++ b/feed2maildir/reader.py @@ -4,14 +4,15 @@ """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: diff --git a/scripts/feed2maildir b/scripts/feed2maildir --- a/scripts/feed2maildir +++ b/scripts/feed2maildir @@ -40,7 +40,9 @@ 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()