refactor into RetootBot

This commit is contained in:
Thomas L 2017-06-17 19:37:33 +02:00
parent e476c9b2da
commit d952396797

View file

@ -6,45 +6,66 @@ import pickle
import os import os
import time import time
# read config in TOML format (https://github.com/toml-lang/toml#toml)
with open('ticketfrei.cfg') as configfile:
config = toml.load(configfile)
client_id = os.path.join( class RetootBot():
'appkeys', def __init__(self, config):
config['app']['name'] + '@' + config['user']['server'] self.config = config
) self.register()
self.login()
if not os.path.isfile(client_id): # load state
mastodon.Mastodon.create_app( try:
config['app']['name'], with open('seen_toots.pickle', 'rb') as f:
api_base_url=config['user']['server'], self.seen_toots = pickle.load(f)
to_file=client_id except IOError:
) self.seen_toots = set()
m = mastodon.Mastodon( def register(self):
client_id=client_id, self.client_id = os.path.join(
api_base_url=config['user']['server'] 'appkeys',
) self.config['app']['name'] +
m.log_in(config['user']['email'], config['user']['password']) '@' + self.config['user']['server']
)
try: if not os.path.isfile(self.client_id):
with open('seen_toots.pickle', 'rb') as f: mastodon.Mastodon.create_app(
seen_toots = pickle.load(f) self.config['app']['name'],
except IOError: api_base_url=self.config['user']['server'],
seen_toots = set() to_file=self.client_id
)
while True: def login(self):
for notification in m.notifications(): self.m = mastodon.Mastodon(
if (notification['type'] == 'mention' client_id=self.client_id,
and notification['status']['id'] not in seen_toots): api_base_url=self.config['user']['server']
print('Boosting toot %d from %s: %s' % ( )
notification['status']['id'], self.m.log_in(
notification['status']['account']['acct'], self.config['user']['email'],
notification['status']['content'])) self.config['user']['password']
seen_toots.add(notification['status']['id']) )
m.status_reblog(notification['status']['id'])
with open('seen_toots.pickle.part', 'wb') as f: def retoot(self):
pickle.dump(seen_toots, f) for notification in self.m.notifications():
os.rename('seen_toots.pickle.part', 'seen_toots.pickle') if (notification['type'] == 'mention'
time.sleep(1) and notification['status']['id'] not in self.seen_toots):
print('Boosting toot %d from %s: %s' % (
notification['status']['id'],
notification['status']['account']['acct'],
notification['status']['content']))
self.m.status_reblog(notification['status']['id'])
self.seen_toots.add(notification['status']['id'])
# save state
with open('seen_toots.pickle.part', 'wb') as f:
pickle.dump(self.seen_toots, f)
os.rename('seen_toots.pickle.part', 'seen_toots.pickle')
if __name__ == '__main__':
# read config in TOML format (https://github.com/toml-lang/toml#toml)
with open('ticketfrei.cfg') as configfile:
bot = RetootBot(toml.load(configfile))
while True:
bot.retoot()
time.sleep(1)