From 758ff1db461a315d2e4489e4d220b2379bfc9005 Mon Sep 17 00:00:00 2001 From: b3yond Date: Sun, 15 Apr 2018 11:26:48 +0200 Subject: [PATCH] implemented mastodon DMs, city, and backend.shutdown function --- README.md | 3 +++ active_bots/mastodonbot.py | 27 ++++++++++++++++++++------- backend.py | 15 ++++++++++----- db.py | 1 + report.py | 2 +- user.py | 4 ++++ 6 files changed, 39 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 45d3e15..71bb6c5 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,9 @@ There are several logfiles which you can look at: # for the uwsgi deployment: less /var/log/ticketfrei/uwsgi.log +# for the backend: +less /var/log/ticketfrei/backend.log + # for the systemd service: less /var/log/syslog diff --git a/active_bots/mastodonbot.py b/active_bots/mastodonbot.py index 1a313dc..065555a 100755 --- a/active_bots/mastodonbot.py +++ b/active_bots/mastodonbot.py @@ -34,16 +34,29 @@ class MastodonBot(Bot): text = re.sub( "(?<=^|(?<=[^a-zA-Z0-9-_.]))@([A-Za-z]+[A-Za-z0-9-_]+)", "", text) - mentions.append(Report(status['account']['acct'], - self, - text, - status['status']['id'], - status['status']['created_at'])) + if status['status']['visibility'] == 'public': + mentions.append(Report(status['account']['acct'], + self, + text, + status['status']['id'], + status['status']['created_at'])) + else: + mentions.append(Report(status['account']['acct'], + 'mastodonPrivate', + text, + status['status']['id'], + status['status']['created_at'])) return mentions def post(self, user, report): m = Mastodon(*user.get_masto_credentials()) if report.source == self: - m.status_reblog(report.id) + try: + m.status_reblog(report.id) + except Exception: + logger.error('Error boosting: ' + report.id, exc_info=True) else: - m.toot(report.text) + try: + m.toot(report.text) + except Exception: + logger.error('Error tooting: ' + user.get_city() + ': ' + report.id, exc_info=True) diff --git a/backend.py b/backend.py index 040aac4..b66fcfa 100755 --- a/backend.py +++ b/backend.py @@ -8,9 +8,17 @@ from sendmail import sendmail import time +def shutdown(): + try: + sendmail(config['web']['contact'], 'Ticketfrei Shutdown') + except Exception: + logger.error('Could not inform admin.', exc_info=True) + exit(1) + + if __name__ == '__main__': logger = logging.getLogger() - fh = logging.StreamHandler() + fh = logging.FileHandler('/var/log/ticketfrei/backend.log') fh.setLevel(logging.DEBUG) logger.addHandler(fh) @@ -32,7 +40,4 @@ if __name__ == '__main__': time.sleep(60) # twitter rate limit >.< except Exception: logger.error('Shutdown.', exc_info=True) - try: - sendmail(config['web']['contact'], 'Ticketfrei Shutdown') - except Exception: - logger.error('Could not inform admin.', exc_info=True) + shutdown() diff --git a/db.py b/db.py index 29fa78c..3ad50f9 100644 --- a/db.py +++ b/db.py @@ -31,6 +31,7 @@ class DB(object): CREATE TABLE IF NOT EXISTS user ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, passhash TEXT, + city TEXT, enabled INTEGER DEFAULT 1 ); CREATE TABLE IF NOT EXISTS email ( diff --git a/report.py b/report.py index 67befc8..aa34fed 100644 --- a/report.py +++ b/report.py @@ -13,7 +13,7 @@ class Report(object): Constructor of a ticketfrei report :param author: username of the author - :param source: mastodon, twitter, or email + :param source: mastodon, twitter, or email bot object :param text: the text of the report :param id: id in the network :param timestamp: time of the report diff --git a/user.py b/user.py index b107277..82a53bb 100644 --- a/user.py +++ b/user.py @@ -172,3 +172,7 @@ class User(object): db.execute("INSERT INTO mastodon_accounts(user_id, access_token, instance_id, active) " "VALUES(?, ?, ?, ?);", (self.uid, access_token, instance_id, 1)) db.commit() + + def get_city(self): + db.execute("SELECT city FROM user WHERE id == ?;", (self.uid, )) + return db.cur.fetchone()[0]