ticketfrei/retootbot.py

117 lines
3.7 KiB
Python
Raw Normal View History

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-07-20 20:30:43 +00:00
import datetime
2017-06-25 17:15:38 +00:00
import trigger
2017-06-17 18:49:30 +00:00
class RetootBot(object):
2017-07-20 20:30:43 +00:00
def __init__(self, config, filter, logpath=None):
2017-06-17 18:49:30 +00:00
self.config = config
2017-06-25 15:50:03 +00:00
self.filter = filter
2017-06-17 18:49:30 +00:00
self.register()
self.login()
# load state
try:
with open('seen_toots.pickle', 'rb') as f:
self.seen_toots = pickle.load(f)
except IOError:
self.seen_toots = set()
2017-07-20 20:30:43 +00:00
if logpath:
self.logpath = logpath
else:
2017-07-20 20:44:44 +00:00
self.logpath = os.path.join("logs", str(datetime.datetime.now()))
2017-07-20 20:30:43 +00:00
def log(self, message, tb=False):
"""
Writing an error message to a logfile in logs/ and prints it.
:param message(string): Log message to be displayed
:param tb: String of the Traceback
"""
2017-07-20 20:44:44 +00:00
time = str(datetime.datetime.now())
2017-07-20 20:30:43 +00:00
if tb:
message = message + " The traceback is located at " + os.path.join("logs" + time)
with open(os.path.join("logs", time), 'w+') as f:
f.write(tb)
line = "[" + time + "] "+ message + "\n"
2017-07-20 20:30:43 +00:00
with open(self.logpath, 'a') as f:
f.write(line)
print line,
2017-07-20 20:30:43 +00:00
2017-06-17 18:49:30 +00:00
def register(self):
self.client_id = os.path.join(
'appkeys',
self.config['mapp']['name'] +
'@' + self.config['muser']['server']
)
if not os.path.isfile(self.client_id):
mastodon.Mastodon.create_app(
self.config['mapp']['name'],
api_base_url=self.config['muser']['server'],
to_file=self.client_id
)
def login(self):
self.m = mastodon.Mastodon(
client_id=self.client_id,
api_base_url=self.config['muser']['server']
)
self.m.log_in(
self.config['muser']['email'],
self.config['muser']['password']
)
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 = []
for notification in self.m.notifications():
if (notification['type'] == 'mention'
2017-06-25 17:15:38 +00:00
and notification['status']['id'] not in self.seen_toots):
self.seen_toots.add(notification['status']['id'])
2017-07-11 21:50:55 +00:00
text_content = re.sub(r'<[^>]*>', '',
2017-06-25 17:15:38 +00:00
notification['status']['content'])
if not self.filter.is_ok(text_content):
continue
2017-07-20 20:30:43 +00:00
self.log('Boosting toot %d from %s: %s' % (
2017-06-17 18:49:30 +00:00
notification['status']['id'],
notification['status']['account']['acct'],
notification['status']['content']))
self.m.status_reblog(notification['status']['id'])
2017-07-11 21:50:55 +00:00
retoots.append('%s: %s' % (
notification['status']['account']['acct'],
re.sub(r'@\S*', '', text_content)))
2017-06-17 18:49:30 +00:00
# save state
2017-07-11 19:42:32 +00:00
with os.fdopen(os.open('seen_toots.pickle.part', os.O_WRONLY | os.O_EXCL | os.O_CREAT), 'w') 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)
with open('ticketfrei.cfg') as configfile:
2017-06-25 17:15:38 +00:00
config = toml.load(configfile)
filter = trigger.Trigger(config)
bot = RetootBot(config, filter)
2017-06-17 18:49:30 +00:00
while True:
bot.retoot()
time.sleep(1)