better error handling of FileExistsError, fixed regex for mastobot

This commit is contained in:
b3yond 2018-01-18 15:10:05 +01:00
parent 10de40549c
commit c8e67d1937

View file

@ -59,8 +59,10 @@ class RetootBot(object):
with os.fdopen(os.open('seen_toots.pickle.part', os.O_WRONLY | os.O_EXCL | os.O_CREAT), 'wb') as f: with os.fdopen(os.open('seen_toots.pickle.part', os.O_WRONLY | os.O_EXCL | os.O_CREAT), 'wb') as f:
pickle.dump(self.seen_toots, f) pickle.dump(self.seen_toots, f)
except FileExistsError: except FileExistsError:
with os.fdopen(os.open('seen_toots.pickle.part', os.O_WRONLY), 'wb') as f: os.unlink('seen_toots.pickle.part')
with os.fdopen(os.open('seen_toots.pickle.part', os.O_WRONLY | os.O_EXCL | os.O_CREAT), 'wb') as f:
pickle.dump(self.seen_toots, f) pickle.dump(self.seen_toots, f)
os.rename('seen_toots.pickle.part', 'seen_toots.pickle')
def crawl(self): def crawl(self):
""" """
@ -75,12 +77,12 @@ class RetootBot(object):
# save state # save state
self.seen_toots.add(status['status']['id']) self.seen_toots.add(status['status']['id'])
self.save_last() self.save_last()
os.rename('seen_toots.pickle.part', 'seen_toots.pickle')
# add mention to mentions # add mention to mentions
text = re.sub("(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)", "", status['status']['content']) text = re.sub(r'<[^>]*>', '', status['status']['content'])
text = re.sub("(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)", "", text)
mentions.append(report.Report(status['account']['acct'], mentions.append(report.Report(status['account']['acct'],
"mastodon", "mastodon",
re.sub(r'<[^>]*>', '', text), text,
status['status']['id'], status['status']['id'],
status['status']['created_at'])) status['status']['created_at']))
return mentions return mentions