2017-06-17 18:49:30 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import pytoml as toml
|
|
|
|
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 logging
|
|
|
|
import sendmail
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2017-12-30 15:33:34 +00:00
|
|
|
|
2017-06-17 18:49:30 +00:00
|
|
|
|
|
|
|
class RetootBot(object):
|
2018-01-07 19:22:32 +00:00
|
|
|
def __init__(self, config, trigger):
|
2017-06-17 18:49:30 +00:00
|
|
|
self.config = config
|
2018-01-05 09:42:31 +00:00
|
|
|
self.trigger = trigger
|
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
|
|
|
|
2017-06-17 22:35:34 +00:00
|
|
|
def retoot(self, toots=()):
|
2017-06-17 18:49:30 +00:00
|
|
|
# toot external provided messages
|
|
|
|
for toot in toots:
|
|
|
|
self.m.toot(toot)
|
|
|
|
|
|
|
|
# boost mentions
|
|
|
|
retoots = []
|
2017-11-24 17:11:35 +00:00
|
|
|
for notification in self.m.notifications():
|
|
|
|
if (notification['type'] == 'mention'
|
|
|
|
and notification['status']['id'] not in self.seen_toots):
|
|
|
|
self.seen_toots.add(notification['status']['id'])
|
|
|
|
text_content = re.sub(r'<[^>]*>', '',
|
|
|
|
notification['status']['content'])
|
2018-01-05 09:42:31 +00:00
|
|
|
if not self.trigger.is_ok(text_content):
|
2017-11-24 17:11:35 +00:00
|
|
|
continue
|
2018-01-07 19:22:32 +00:00
|
|
|
logger.info('Boosting toot from %s: %s' % (
|
2017-11-24 17:11:35 +00:00
|
|
|
notification['status']['account']['acct'],
|
|
|
|
notification['status']['content']))
|
|
|
|
self.m.status_reblog(notification['status']['id'])
|
|
|
|
retoots.append('%s: %s' % (
|
|
|
|
notification['status']['account']['acct'],
|
|
|
|
re.sub(r'@\S*', '', text_content)))
|
2017-10-02 18:12:58 +00:00
|
|
|
# If the Mastodon instance returns interesting Errors, add them here:
|
2017-06-17 18:49:30 +00:00
|
|
|
|
|
|
|
# save state
|
2017-12-30 10:31:16 +00:00
|
|
|
with os.fdopen(os.open('seen_toots.pickle.part', os.O_WRONLY | os.O_EXCL | os.O_CREAT), 'wb') as f:
|
2017-06-17 18:49:30 +00:00
|
|
|
pickle.dump(self.seen_toots, f)
|
|
|
|
os.rename('seen_toots.pickle.part', 'seen_toots.pickle')
|
|
|
|
|
|
|
|
# return mentions for mirroring
|
|
|
|
return retoots
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
# read config in TOML format (https://github.com/toml-lang/toml#toml)
|
2017-12-30 09:32:20 +00:00
|
|
|
with open('config.toml') as configfile:
|
2017-06-25 17:15:38 +00:00
|
|
|
config = toml.load(configfile)
|
|
|
|
|
2018-01-07 19:22:32 +00:00
|
|
|
fh = logging.FileHandler(config['logging']['logpath'])
|
|
|
|
fh.setLevel(logging.DEBUG)
|
|
|
|
logger.addHandler(fh)
|
2017-12-30 15:33:34 +00:00
|
|
|
|
2018-01-07 19:22:32 +00:00
|
|
|
trigger = trigger.Trigger(config)
|
|
|
|
bot = RetootBot(config, trigger)
|
2018-01-04 10:02:42 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
bot.retoot()
|
|
|
|
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-01-07 19:22:32 +00:00
|
|
|
logger.error('Shutdown', exc_info=True)
|
|
|
|
try:
|
|
|
|
mailer = sendmail.Mailer(config)
|
|
|
|
mailer.send('', config['mail']['contact'],
|
|
|
|
'Ticketfrei Crash Report',
|
|
|
|
attachment=config['logging']['logpath'])
|
|
|
|
except:
|
|
|
|
logger.error('Mail sending failed', exc_info=True)
|