diff --git a/mailbot.py b/mailbot.py index 39c1eb9..fb6703d 100644 --- a/mailbot.py +++ b/mailbot.py @@ -87,7 +87,7 @@ class Mailbot(object): date = (date_tuple - datetime.datetime(1970, 1, 1)).total_seconds() if date > self.get_history(self.history_path): self.last_mail = date - self.save_last_mail() + self.save_last() msgs.append(self.make_report(msg)) return msgs @@ -109,7 +109,7 @@ class Mailbot(object): f.write(last_mail) return float(last_mail) - def save_last_mail(self): + def save_last(self): """ Saves the last retweeted tweet in last_mention. """ with open(self.history_path, "w") as f: f.write(str(self.last_mail)) @@ -121,9 +121,8 @@ class Mailbot(object): :param statuses: (list of report.Report objects) """ for status in statuses: - status = status.format() mailer = sendmail.Mailer(self.config) - mailer.send(status, self.mailinglist, "Warnung: Kontrolleure gesehen") + mailer.send(status.format(), self.mailinglist, "Warnung: Kontrolleure gesehen") def make_report(self, msg): """ @@ -143,7 +142,7 @@ class Mailbot(object): text = msg.get_payload() post = report.Report(author, "mail", text, None, date) self.last_mail = date - self.save_last_mail() + self.save_last() return post def flow(self, trigger, statuses): @@ -170,6 +169,7 @@ if __name__ == "__main__": config = toml.load(configfile) # set log file + logger = logging.getLogger() fh = logging.FileHandler(config['logging']['logpath']) fh.setLevel(logging.DEBUG) logger.addHandler(fh) @@ -189,7 +189,7 @@ if __name__ == "__main__": print("Good bye. Remember to restart the bot!") except: logger.error('Shutdown', exc_info=True) - m.save_last_mail() + m.save_last() try: mailer = sendmail.Mailer(config) mailer.send('', config['mail']['contact'], diff --git a/retootbot.py b/retootbot.py index 78744e6..8caaedb 100755 --- a/retootbot.py +++ b/retootbot.py @@ -53,6 +53,11 @@ class RetootBot(object): ) return m + def save_last(self): + """ save the last seen toot """ + 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) + def crawl(self): """ Crawl mentions from Mastodon. @@ -65,8 +70,7 @@ class RetootBot(object): if (status['type'] == 'mention' and status['status']['id'] not in self.seen_toots): # save state self.seen_toots.add(status['status']['id']) - 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) + self.save_last() os.rename('seen_toots.pickle.part', 'seen_toots.pickle') # add mention to mentions mentions.append(report.Report(status['account']['acct'], diff --git a/retweetbot.py b/retweetbot.py index 4a48da9..8dd3e10 100755 --- a/retweetbot.py +++ b/retweetbot.py @@ -80,7 +80,7 @@ class RetweetBot(object): f.write(last_mention) return int(last_mention) - def save_last_mention(self): + def save_last(self): """ Saves the last retweeted tweet in last_mention. """ with open(self.history_path, "w") as f: f.write(str(self.last_mention)) @@ -186,7 +186,7 @@ class RetweetBot(object): # save the id so it doesn't get crawled again if status.id > self.last_mention: self.last_mention = status.id - self.save_last_mention() + self.save_last() # Return Retweets for posting on other bots return all_tweets @@ -216,7 +216,7 @@ if __name__ == "__main__": print("Good bye. Remember to restart the bot!") except: logger.error('Shutdown', exc_info=True) - bot.save_last_mention() + bot.save_last() try: mailer = sendmail.Mailer(config) mailer.send('', config['mail']['contact'], diff --git a/ticketfrei.py b/ticketfrei.py index ad554ab..d6097a3 100755 --- a/ticketfrei.py +++ b/ticketfrei.py @@ -7,6 +7,7 @@ import sendmail from retootbot import RetootBot from retweetbot import RetweetBot +from mailbot import Mailbot from trigger import Trigger if __name__ == '__main__': @@ -14,26 +15,36 @@ if __name__ == '__main__': with open('config.toml') as configfile: config = toml.load(configfile) + # set log file logger = logging.getLogger() fh = logging.FileHandler(config['logging']['logpath']) fh.setLevel(logging.DEBUG) logger.addHandler(fh) trigger = Trigger(config) - mbot = RetootBot(config) - tbot = RetweetBot(config) + + bots = [RetootBot(config), RetweetBot(config), Mailbot(config)] try: statuses = [] while True: - statuses = mbot.flow(trigger, statuses) - statuses = tbot.flow(trigger, to_tweet=statuses) - time.sleep(60) + for bot in bots: + reports = bot.crawl() + for status in reports: + if not trigger.is_ok(status.text): + continue + for bot2 in bots: + if bot == bot2: + bot2.repost(status) + else: + bot2.post(status) + time.sleep(60) # twitter rate limit >.< except KeyboardInterrupt: print("Good bye. Remember to restart the bot!") except: logger.error('Shutdown', exc_info=True) - tbot.save_last_mention() + for bot in bots: + bot.save_last() try: mailer = sendmail.Mailer(config) mailer.send('', config['mail']['contact'],