From 3f02891223d470aa7da7772fdc9ea3811d6c2164 Mon Sep 17 00:00:00 2001 From: Thomas L Date: Sat, 17 Jun 2017 18:15:13 +0200 Subject: [PATCH] implement basic auto-reblogging --- .gitignore | 4 ++++ README | 2 +- appkeys/.gitignore | 2 ++ ticketfrei.cfg.example | 7 ++++++ ticketfrei.py | 50 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 appkeys/.gitignore create mode 100644 ticketfrei.cfg.example create mode 100644 ticketfrei.py diff --git a/.gitignore b/.gitignore index b1675d4..8e3698c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ +*.swp +ticketfrei.cfg +seen_toots.pickle +seen_toots.pickle.part pip-selfcheck.json bin/ include/ diff --git a/README b/README index 60ef49d..5c8e656 100644 --- a/README +++ b/README @@ -9,5 +9,5 @@ $ . bin/activate ``` Install dependencies ```shell -$ pip3 install Mastodon.py +$ pip3 install Mastodon.py pytoml ``` diff --git a/appkeys/.gitignore b/appkeys/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/appkeys/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/ticketfrei.cfg.example b/ticketfrei.cfg.example new file mode 100644 index 0000000..f4f907d --- /dev/null +++ b/ticketfrei.cfg.example @@ -0,0 +1,7 @@ +[app] +app_name = 'yourcity_ticketfrei' + +[user] +email = 'youremail@server.tld' +password = 'yourpassword' +server = 'yourmastodoninstance' diff --git a/ticketfrei.py b/ticketfrei.py new file mode 100644 index 0000000..70a58ef --- /dev/null +++ b/ticketfrei.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 + +import pytoml as toml +import mastodon +import pickle +import os +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( + 'appkeys', + config['app']['name'] + '@' + config['user']['server'] + ) + +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']) + +try: + with open('seen_toots.pickle', 'rb') as f: + seen_toots = pickle.load(f) +except IOError: + seen_toots = set() + +while True: + for notification in m.notifications(): + if (notification['type'] == 'mention' + and notification['status']['id'] not in seen_toots): + print('Boosting toot %d from %s: %s' % ( + notification['status']['id'], + notification['status']['account']['acct'], + notification['status']['content'])) + seen_toots.add(notification['status']['id']) + m.status_reblog(notification['status']['id']) + with open('seen_toots.pickle.part', 'wb') as f: + pickle.dump(seen_toots, f) + os.rename('seen_toots.pickle.part', 'seen_toots.pickle') + time.sleep(1)