Browse Source

trying to avoid db corruption when script breaks

master
toyg 11 years ago
parent
commit
e377857fe9
  1. 13
      twistscraper.py

13
twistscraper.py

@ -164,9 +164,21 @@ class TwisterScraper: @@ -164,9 +164,21 @@ class TwisterScraper:
print("Connection error retrieving user {0}: {1}".format(u, str(e)))
def saveDb(self):
try:
with open(self.dbFile, 'wb') as dbFile:
pickle.dump(self.db, dbFile)
except (KeyboardInterrupt, Exception):
print("Closing db before quitting...")
if dbFile:
# close the hung descriptor and re-try the dumping
try:
dbFile.close()
except Exception:
pass
with open(self.dbFile, 'wb') as dbFile:
pickle.dump(self.db, dbFile)
def get_posts_since(self, username, dateObj, maxNum=1000):
since_epoch = time.mktime(dateObj.timetuple())
all_posts = self.twister.getposts(1000, [{'username': username}])
@ -217,3 +229,4 @@ if __name__ == '__main__': @@ -217,3 +229,4 @@ if __name__ == '__main__':
ts = TwisterScraper(expanduser('~/.twister/_localusersdb'), 'localhost')
ts.scrape_users()
print("Total users in db: {0}".format(len(ts.db.users)))

Loading…
Cancel
Save