REFACTOR Write database out after all posts have been successfully written out
1 files changed, 9 insertions(+), 13 deletions(-)

M feed2maildir/converter.py
M feed2maildir/converter.py +9 -13
@@ 92,16 92,19 @@ Content-Type: text/plain
         """Do a full run"""
         if self.feeds:
             self.check_maildir(self.maildir)
-            self.news = self.find_new(self.feeds, self.dbdata)
+            (self.news, newtimes) = self.find_new(self.feeds, self.dbdata)
             for newfeed, posts in self.news.items():
                 for newpost in posts:
                     self.write(self.compose(newfeed, newpost))
 
+            with open(self.db, 'w') as f:
+                f.write(json.dumps(newtimes))
+
     def load(self, feeds):
         """Load a list of feeds in feedparser-dict form"""
         self.feeds = feeds
 
-    def find_new(self, feeds, db, writedb=True, dbfile=None):
+    def find_new(self, feeds, db, dbfile=None):
         """Find the new posts by comparing them to the db, by default
         refreshing the db"""
         new = {}

          
@@ 136,19 139,10 @@ Content-Type: text/plain
                             new[feedname].append(post)
                         except: # it is the first one, make a new list
                             new[feedname] = [post, ]
-            if writedb:
-                newtimes[feedname] = feedup.strftime('%Y-%m-%d %H:%M:%S %Z')
 
-        if writedb:
-            if not dbfile: # use own dbfile as default
-                dbfile = self.db
-            try: # to write the new database
-                with open(dbfile, 'w') as f:
-                    f.write(json.dumps(newtimes))
-            except:
-                self.output('WARNING: failed to write the new database')
+            newtimes[feedname] = feedup.strftime('%Y-%m-%d %H:%M:%S %Z')
 
-        return new
+        return (new, newtimes)
 
     def post_update_time(self, post):
         """Try to get the post time"""

          
@@ 212,6 206,8 @@ Content-Type: text/plain
                 else:
                     f.write(message)
         except:
+            os.remove(name)
+            raise
             self.output('WARNING: failed to write message to file')
 
     def mktime(self, arg):