changed ticketfrei flow logic, integrated mailbot!!! #11

This commit is contained in:
b3yond 2018-01-18 13:06:53 +01:00
parent cde5494de3
commit d6a0c6d377
4 changed files with 32 additions and 17 deletions

View File

@ -87,7 +87,7 @@ class Mailbot(object):
date = (date_tuple - datetime.datetime(1970, 1, 1)).total_seconds() date = (date_tuple - datetime.datetime(1970, 1, 1)).total_seconds()
if date > self.get_history(self.history_path): if date > self.get_history(self.history_path):
self.last_mail = date self.last_mail = date
self.save_last_mail() self.save_last()
msgs.append(self.make_report(msg)) msgs.append(self.make_report(msg))
return msgs return msgs
@ -109,7 +109,7 @@ class Mailbot(object):
f.write(last_mail) f.write(last_mail)
return float(last_mail) return float(last_mail)
def save_last_mail(self): def save_last(self):
""" Saves the last retweeted tweet in last_mention. """ """ Saves the last retweeted tweet in last_mention. """
with open(self.history_path, "w") as f: with open(self.history_path, "w") as f:
f.write(str(self.last_mail)) f.write(str(self.last_mail))
@ -121,9 +121,8 @@ class Mailbot(object):
:param statuses: (list of report.Report objects) :param statuses: (list of report.Report objects)
""" """
for status in statuses: for status in statuses:
status = status.format()
mailer = sendmail.Mailer(self.config) 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): def make_report(self, msg):
""" """
@ -143,7 +142,7 @@ class Mailbot(object):
text = msg.get_payload() text = msg.get_payload()
post = report.Report(author, "mail", text, None, date) post = report.Report(author, "mail", text, None, date)
self.last_mail = date self.last_mail = date
self.save_last_mail() self.save_last()
return post return post
def flow(self, trigger, statuses): def flow(self, trigger, statuses):
@ -170,6 +169,7 @@ if __name__ == "__main__":
config = toml.load(configfile) config = toml.load(configfile)
# set log file # set log file
logger = logging.getLogger()
fh = logging.FileHandler(config['logging']['logpath']) fh = logging.FileHandler(config['logging']['logpath'])
fh.setLevel(logging.DEBUG) fh.setLevel(logging.DEBUG)
logger.addHandler(fh) logger.addHandler(fh)
@ -189,7 +189,7 @@ if __name__ == "__main__":
print("Good bye. Remember to restart the bot!") print("Good bye. Remember to restart the bot!")
except: except:
logger.error('Shutdown', exc_info=True) logger.error('Shutdown', exc_info=True)
m.save_last_mail() m.save_last()
try: try:
mailer = sendmail.Mailer(config) mailer = sendmail.Mailer(config)
mailer.send('', config['mail']['contact'], mailer.send('', config['mail']['contact'],

View File

@ -53,6 +53,11 @@ class RetootBot(object):
) )
return m 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): def crawl(self):
""" """
Crawl mentions from Mastodon. Crawl mentions from Mastodon.
@ -65,8 +70,7 @@ class RetootBot(object):
if (status['type'] == 'mention' and status['status']['id'] not in self.seen_toots): if (status['type'] == 'mention' and status['status']['id'] not in self.seen_toots):
# save state # save state
self.seen_toots.add(status['status']['id']) 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: self.save_last()
pickle.dump(self.seen_toots, f)
os.rename('seen_toots.pickle.part', 'seen_toots.pickle') os.rename('seen_toots.pickle.part', 'seen_toots.pickle')
# add mention to mentions # add mention to mentions
mentions.append(report.Report(status['account']['acct'], mentions.append(report.Report(status['account']['acct'],

View File

@ -80,7 +80,7 @@ class RetweetBot(object):
f.write(last_mention) f.write(last_mention)
return int(last_mention) return int(last_mention)
def save_last_mention(self): def save_last(self):
""" Saves the last retweeted tweet in last_mention. """ """ Saves the last retweeted tweet in last_mention. """
with open(self.history_path, "w") as f: with open(self.history_path, "w") as f:
f.write(str(self.last_mention)) f.write(str(self.last_mention))
@ -186,7 +186,7 @@ class RetweetBot(object):
# save the id so it doesn't get crawled again # save the id so it doesn't get crawled again
if status.id > self.last_mention: if status.id > self.last_mention:
self.last_mention = status.id self.last_mention = status.id
self.save_last_mention() self.save_last()
# Return Retweets for posting on other bots # Return Retweets for posting on other bots
return all_tweets return all_tweets
@ -216,7 +216,7 @@ if __name__ == "__main__":
print("Good bye. Remember to restart the bot!") print("Good bye. Remember to restart the bot!")
except: except:
logger.error('Shutdown', exc_info=True) logger.error('Shutdown', exc_info=True)
bot.save_last_mention() bot.save_last()
try: try:
mailer = sendmail.Mailer(config) mailer = sendmail.Mailer(config)
mailer.send('', config['mail']['contact'], mailer.send('', config['mail']['contact'],

View File

@ -7,6 +7,7 @@ import sendmail
from retootbot import RetootBot from retootbot import RetootBot
from retweetbot import RetweetBot from retweetbot import RetweetBot
from mailbot import Mailbot
from trigger import Trigger from trigger import Trigger
if __name__ == '__main__': if __name__ == '__main__':
@ -14,26 +15,36 @@ if __name__ == '__main__':
with open('config.toml') as configfile: with open('config.toml') as configfile:
config = toml.load(configfile) config = toml.load(configfile)
# set log file
logger = logging.getLogger() logger = logging.getLogger()
fh = logging.FileHandler(config['logging']['logpath']) fh = logging.FileHandler(config['logging']['logpath'])
fh.setLevel(logging.DEBUG) fh.setLevel(logging.DEBUG)
logger.addHandler(fh) logger.addHandler(fh)
trigger = Trigger(config) trigger = Trigger(config)
mbot = RetootBot(config)
tbot = RetweetBot(config) bots = [RetootBot(config), RetweetBot(config), Mailbot(config)]
try: try:
statuses = [] statuses = []
while True: while True:
statuses = mbot.flow(trigger, statuses) for bot in bots:
statuses = tbot.flow(trigger, to_tweet=statuses) reports = bot.crawl()
time.sleep(60) 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: except KeyboardInterrupt:
print("Good bye. Remember to restart the bot!") print("Good bye. Remember to restart the bot!")
except: except:
logger.error('Shutdown', exc_info=True) logger.error('Shutdown', exc_info=True)
tbot.save_last_mention() for bot in bots:
bot.save_last()
try: try:
mailer = sendmail.Mailer(config) mailer = sendmail.Mailer(config)
mailer.send('', config['mail']['contact'], mailer.send('', config['mail']['contact'],