make interop with RetweetBot easier

This commit is contained in:
Thomas L 2017-06-17 20:27:53 +02:00
parent a3e6c36926
commit 364746f976
2 changed files with 35 additions and 13 deletions

View file

@ -1,7 +1,17 @@
[app] ##
# Twitter bot configuration
#
# TODO
##
# Mastodon bot configuration
#
[mapp]
app_name = 'yourcity_ticketfrei' app_name = 'yourcity_ticketfrei'
[user] [muser]
email = 'youremail@server.tld' email = 'youremail@server.tld'
password = 'yourpassword' password = 'yourpassword'
server = 'yourmastodoninstance' server = 'yourmastodoninstance'

View file

@ -2,12 +2,13 @@
import pytoml as toml import pytoml as toml
import mastodon import mastodon
import pickle
import os import os
import pickle
import re
import time import time
class RetootBot(): class RetootBot(object):
def __init__(self, config): def __init__(self, config):
self.config = config self.config = config
self.register() self.register()
@ -23,28 +24,34 @@ class RetootBot():
def register(self): def register(self):
self.client_id = os.path.join( self.client_id = os.path.join(
'appkeys', 'appkeys',
self.config['app']['name'] + self.config['mapp']['name'] +
'@' + self.config['user']['server'] '@' + self.config['muser']['server']
) )
if not os.path.isfile(self.client_id): if not os.path.isfile(self.client_id):
mastodon.Mastodon.create_app( mastodon.Mastodon.create_app(
self.config['app']['name'], self.config['mapp']['name'],
api_base_url=self.config['user']['server'], api_base_url=self.config['muser']['server'],
to_file=self.client_id to_file=self.client_id
) )
def login(self): def login(self):
self.m = mastodon.Mastodon( self.m = mastodon.Mastodon(
client_id=self.client_id, client_id=self.client_id,
api_base_url=self.config['user']['server'] api_base_url=self.config['muser']['server']
) )
self.m.log_in( self.m.log_in(
self.config['user']['email'], self.config['muser']['email'],
self.config['user']['password'] self.config['muser']['password']
) )
def retoot(self): def retoot(self, toots=[]):
# toot external provided messages
for toot in toots:
self.m.toot(toot)
# boost mentions
retoots = []
for notification in self.m.notifications(): for notification in self.m.notifications():
if (notification['type'] == 'mention' if (notification['type'] == 'mention'
and notification['status']['id'] not in self.seen_toots): and notification['status']['id'] not in self.seen_toots):
@ -53,13 +60,18 @@ class RetootBot():
notification['status']['account']['acct'], notification['status']['account']['acct'],
notification['status']['content'])) notification['status']['content']))
self.m.status_reblog(notification['status']['id']) self.m.status_reblog(notification['status']['id'])
retoots.append(re.sub('<[^>]*>', '',
notification['status']['content']))
self.seen_toots.add(notification['status']['id']) self.seen_toots.add(notification['status']['id'])
# save state # save state
with open('seen_toots.pickle.part', 'wb') as f: with open('seen_toots.pickle.part', 'xb') as f:
pickle.dump(self.seen_toots, f) pickle.dump(self.seen_toots, f)
os.rename('seen_toots.pickle.part', 'seen_toots.pickle') os.rename('seen_toots.pickle.part', 'seen_toots.pickle')
# return mentions for mirroring
return retoots
if __name__ == '__main__': if __name__ == '__main__':
# read config in TOML format (https://github.com/toml-lang/toml#toml) # read config in TOML format (https://github.com/toml-lang/toml#toml)