2017-06-17 18:49:30 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import mastodon
|
|
|
|
import os
|
|
|
|
import pickle
|
|
|
|
import re
|
|
|
|
import time
|
2017-06-25 17:15:38 +00:00
|
|
|
import trigger
|
2018-01-07 19:22:32 +00:00
|
|
|
import sendmail
|
2018-01-18 10:41:08 +00:00
|
|
|
import report
|
2018-03-23 12:19:39 +00:00
|
|
|
import backend
|
2017-06-17 18:49:30 +00:00
|
|
|
|
|
|
|
class RetootBot(object):
|
2018-01-18 10:41:08 +00:00
|
|
|
def __init__(self, config):
|
2017-06-17 18:49:30 +00:00
|
|
|
self.config = config
|
2018-03-23 12:19:39 +00:00
|
|
|
self.logger = backend.get_logger(config)
|
2018-01-04 10:02:42 +00:00
|
|
|
self.client_id = self.register()
|
|
|
|
self.m = self.login()
|
2017-06-17 18:49:30 +00:00
|
|
|
|
|
|
|
# load state
|
|
|
|
try:
|
|
|
|
with open('seen_toots.pickle', 'rb') as f:
|
|
|
|
self.seen_toots = pickle.load(f)
|
|
|
|
except IOError:
|
|
|
|
self.seen_toots = set()
|
|
|
|
|
|
|
|
def register(self):
|
2018-01-04 10:02:42 +00:00
|
|
|
client_id = os.path.join(
|
2017-12-30 15:33:34 +00:00
|
|
|
'appkeys',
|
|
|
|
self.config['mapp']['name'] +
|
|
|
|
'@' + self.config['muser']['server']
|
|
|
|
)
|
2017-06-17 18:49:30 +00:00
|
|
|
|
2018-01-04 10:02:42 +00:00
|
|
|
if not os.path.isfile(client_id):
|
2017-06-17 18:49:30 +00:00
|
|
|
mastodon.Mastodon.create_app(
|
2017-12-30 15:33:34 +00:00
|
|
|
self.config['mapp']['name'],
|
|
|
|
api_base_url=self.config['muser']['server'],
|
2018-01-04 10:02:42 +00:00
|
|
|
to_file=client_id
|
2017-12-30 15:33:34 +00:00
|
|
|
)
|
2018-01-04 10:05:36 +00:00
|
|
|
return client_id
|
2017-06-17 18:49:30 +00:00
|
|
|
|
|
|
|
def login(self):
|
2018-01-04 10:02:42 +00:00
|
|
|
m = mastodon.Mastodon(
|
2017-12-30 15:33:34 +00:00
|
|
|
client_id=self.client_id,
|
|
|
|
api_base_url=self.config['muser']['server']
|
|
|
|
)
|
2018-01-04 10:02:42 +00:00
|
|
|
m.log_in(
|
2017-12-30 15:33:34 +00:00
|
|
|
self.config['muser']['email'],
|
|
|
|
self.config['muser']['password']
|
|
|
|
)
|
2018-01-04 10:02:42 +00:00
|
|
|
return m
|
2017-06-17 18:49:30 +00:00
|
|
|
|
2018-01-18 12:06:53 +00:00
|
|
|
def save_last(self):
|
|
|
|
""" save the last seen toot """
|
2018-01-18 12:40:07 +00:00
|
|
|
try:
|
|
|
|
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)
|
|
|
|
except FileExistsError:
|
2018-01-18 14:10:05 +00:00
|
|
|
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:
|
2018-01-18 12:40:07 +00:00
|
|
|
pickle.dump(self.seen_toots, f)
|
2018-01-18 14:10:05 +00:00
|
|
|
os.rename('seen_toots.pickle.part', 'seen_toots.pickle')
|
2018-01-18 12:06:53 +00:00
|
|
|
|
2018-01-18 10:41:08 +00:00
|
|
|
def crawl(self):
|
|
|
|
"""
|
|
|
|
Crawl mentions from Mastodon.
|
|
|
|
|
|
|
|
:return: list of statuses
|
|
|
|
"""
|
|
|
|
mentions = []
|
2018-01-18 23:17:09 +00:00
|
|
|
try:
|
|
|
|
all = self.m.notifications()
|
|
|
|
except: # mastodon.Mastodon.MastodonAPIError is unfortunately not in __init__.py
|
2018-03-23 10:18:00 +00:00
|
|
|
self.logger.error("Unknown Mastodon API Error.", exc_info=True)
|
2018-01-18 23:17:09 +00:00
|
|
|
return mentions
|
2018-01-18 10:41:08 +00:00
|
|
|
for status in all:
|
|
|
|
if (status['type'] == 'mention' and status['status']['id'] not in self.seen_toots):
|
|
|
|
# save state
|
|
|
|
self.seen_toots.add(status['status']['id'])
|
2018-01-18 12:06:53 +00:00
|
|
|
self.save_last()
|
2018-01-18 10:41:08 +00:00
|
|
|
# add mention to mentions
|
2018-01-18 14:10:05 +00:00
|
|
|
text = re.sub(r'<[^>]*>', '', status['status']['content'])
|
|
|
|
text = re.sub("(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)", "", text)
|
2018-01-18 10:41:08 +00:00
|
|
|
mentions.append(report.Report(status['account']['acct'],
|
|
|
|
"mastodon",
|
2018-01-18 14:10:05 +00:00
|
|
|
text,
|
2018-01-18 10:41:08 +00:00
|
|
|
status['status']['id'],
|
|
|
|
status['status']['created_at']))
|
|
|
|
return mentions
|
|
|
|
|
|
|
|
def repost(self, mention):
|
|
|
|
"""
|
|
|
|
Retoots a mention.
|
|
|
|
|
|
|
|
:param mention: (report.Report object)
|
|
|
|
"""
|
2018-03-23 10:18:00 +00:00
|
|
|
self.logger.info('Boosting toot from %s' % (
|
2018-01-18 10:41:08 +00:00
|
|
|
mention.format()))
|
|
|
|
self.m.status_reblog(mention.id)
|
|
|
|
|
|
|
|
|
|
|
|
def post(self, report):
|
|
|
|
"""
|
|
|
|
Toots a report from other sources.
|
|
|
|
|
|
|
|
:param report: (report.Report object)
|
|
|
|
"""
|
|
|
|
toot = report.format()
|
|
|
|
self.m.toot(toot)
|
|
|
|
|
|
|
|
def flow(self, trigger, reports=()):
|
2017-06-17 18:49:30 +00:00
|
|
|
# toot external provided messages
|
2018-01-18 10:41:08 +00:00
|
|
|
for report in reports:
|
|
|
|
self.post(report)
|
2017-06-17 18:49:30 +00:00
|
|
|
|
|
|
|
# boost mentions
|
|
|
|
retoots = []
|
2018-01-18 10:41:08 +00:00
|
|
|
for mention in self.crawl():
|
|
|
|
if not trigger.is_ok(mention.text):
|
|
|
|
continue
|
|
|
|
self.repost(mention)
|
|
|
|
retoots.append(mention)
|
2017-06-17 18:49:30 +00:00
|
|
|
|
|
|
|
# return mentions for mirroring
|
|
|
|
return retoots
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2018-03-23 12:19:39 +00:00
|
|
|
config = backend.get_config()
|
2017-06-25 17:15:38 +00:00
|
|
|
|
2018-01-07 19:22:32 +00:00
|
|
|
trigger = trigger.Trigger(config)
|
2018-01-18 10:41:08 +00:00
|
|
|
bot = RetootBot(config)
|
2018-01-04 10:02:42 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
while True:
|
2018-01-18 10:41:08 +00:00
|
|
|
bot.flow(trigger)
|
2018-01-04 10:02:42 +00:00
|
|
|
time.sleep(1)
|
|
|
|
except KeyboardInterrupt:
|
2018-01-04 11:20:59 +00:00
|
|
|
print("Good bye. Remember to restart the bot!")
|
2018-01-04 10:02:42 +00:00
|
|
|
except:
|
2018-03-23 10:18:00 +00:00
|
|
|
bot.logger.error('Shutdown', exc_info=True)
|
2018-01-07 19:22:32 +00:00
|
|
|
try:
|
|
|
|
mailer = sendmail.Mailer(config)
|
|
|
|
mailer.send('', config['mail']['contact'],
|
|
|
|
'Ticketfrei Crash Report',
|
|
|
|
attachment=config['logging']['logpath'])
|
|
|
|
except:
|
2018-03-23 10:18:00 +00:00
|
|
|
bot.logger.error('Mail sending failed', exc_info=True)
|