refactor into RetootBot
This commit is contained in:
parent
e476c9b2da
commit
d952396797
|
@ -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):
|
|
||||||
mastodon.Mastodon.create_app(
|
|
||||||
config['app']['name'],
|
|
||||||
api_base_url=config['user']['server'],
|
|
||||||
to_file=client_id
|
|
||||||
)
|
|
||||||
|
|
||||||
m = mastodon.Mastodon(
|
|
||||||
client_id=client_id,
|
|
||||||
api_base_url=config['user']['server']
|
|
||||||
)
|
|
||||||
m.log_in(config['user']['email'], config['user']['password'])
|
|
||||||
|
|
||||||
|
# load state
|
||||||
try:
|
try:
|
||||||
with open('seen_toots.pickle', 'rb') as f:
|
with open('seen_toots.pickle', 'rb') as f:
|
||||||
seen_toots = pickle.load(f)
|
self.seen_toots = pickle.load(f)
|
||||||
except IOError:
|
except IOError:
|
||||||
seen_toots = set()
|
self.seen_toots = set()
|
||||||
|
|
||||||
while True:
|
def register(self):
|
||||||
for notification in m.notifications():
|
self.client_id = os.path.join(
|
||||||
|
'appkeys',
|
||||||
|
self.config['app']['name'] +
|
||||||
|
'@' + self.config['user']['server']
|
||||||
|
)
|
||||||
|
|
||||||
|
if not os.path.isfile(self.client_id):
|
||||||
|
mastodon.Mastodon.create_app(
|
||||||
|
self.config['app']['name'],
|
||||||
|
api_base_url=self.config['user']['server'],
|
||||||
|
to_file=self.client_id
|
||||||
|
)
|
||||||
|
|
||||||
|
def login(self):
|
||||||
|
self.m = mastodon.Mastodon(
|
||||||
|
client_id=self.client_id,
|
||||||
|
api_base_url=self.config['user']['server']
|
||||||
|
)
|
||||||
|
self.m.log_in(
|
||||||
|
self.config['user']['email'],
|
||||||
|
self.config['user']['password']
|
||||||
|
)
|
||||||
|
|
||||||
|
def retoot(self):
|
||||||
|
for notification in self.m.notifications():
|
||||||
if (notification['type'] == 'mention'
|
if (notification['type'] == 'mention'
|
||||||
and notification['status']['id'] not in seen_toots):
|
and notification['status']['id'] not in self.seen_toots):
|
||||||
print('Boosting toot %d from %s: %s' % (
|
print('Boosting toot %d from %s: %s' % (
|
||||||
notification['status']['id'],
|
notification['status']['id'],
|
||||||
notification['status']['account']['acct'],
|
notification['status']['account']['acct'],
|
||||||
notification['status']['content']))
|
notification['status']['content']))
|
||||||
seen_toots.add(notification['status']['id'])
|
self.m.status_reblog(notification['status']['id'])
|
||||||
m.status_reblog(notification['status']['id'])
|
self.seen_toots.add(notification['status']['id'])
|
||||||
|
|
||||||
|
# save state
|
||||||
with open('seen_toots.pickle.part', 'wb') as f:
|
with open('seen_toots.pickle.part', 'wb') as f:
|
||||||
pickle.dump(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')
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
time.sleep(1)
|
||||||
|
|
Loading…
Reference in a new issue