diff --git a/active_bots/mailbot.py b/active_bots/mailbot.py index 373960f..7c8fcb5 100644 --- a/active_bots/mailbot.py +++ b/active_bots/mailbot.py @@ -34,7 +34,7 @@ class Mailbot(Bot): unsubscribe_text = "\n_______\nYou don't want to receive those messages? Unsubscribe with this link: " body = report.text + unsubscribe_text + config['web']['host'] + "/city/mail/unsubscribe/" \ + db.mail_subscription_token(rec, user.get_city()) - if report.author != rec: + if rec not in report.author: try: city = user.get_city() sendmail(rec, "Ticketfrei " + city + " Report", @@ -54,16 +54,19 @@ def make_report(msg, user): date = get_date_from_header(msg['Date']) author = msg['From'] # get mail author from email header - # :todo take only the part in between the < > if msg.is_multipart(): text = [] for part in msg.get_payload(): if part.get_content_type() == "text": text.append(part.get_payload()) + elif part.get_content_type() == "application/pgp-signature": + pass # ignore PGP signatures elif part.get_content_type() == "multipart/mixed": for p in part: - if p.get_content_type() == "text": + if isinstance(p, str): + text.append(p) + elif p.get_content_type() == "text": text.append(part.get_payload()) else: logger.error("unknown MIMEtype: " + diff --git a/active_bots/twitterDMs.py b/active_bots/twitterDMs.py index a9bd5e4..8282fc5 100644 --- a/active_bots/twitterDMs.py +++ b/active_bots/twitterDMs.py @@ -5,7 +5,6 @@ import tweepy import re import requests import report -import tfglobals from time import time from bot import Bot @@ -13,9 +12,10 @@ from bot import Bot logger = logging.getLogger(__name__) -class TwitterBot(Bot): +class TwitterDMListener(Bot): + def get_api(self, user): - keys = user.get_api_keys() + keys = user.get_twitter_credentials() auth = tweepy.OAuthHandler(consumer_key=keys[0], consumer_secret=keys[1]) auth.set_access_token(keys[2], # access_token_key @@ -29,19 +29,22 @@ class TwitterBot(Bot): :return: reports: (list of report.Report objects) """ reports = [] - if tfglobals.last_twitter_request + 60 > time(): - return reports + try: + if user.get_last_twitter_request() + 60 > time(): + return reports + except TypeError: + user.set_last_twitter_request(time()) try: api = self.get_api(user) - except IndexError: + except TypeError: return reports # no twitter account for this user. last_dm = user.get_seen_dm() try: if last_dm is None: mentions = api.direct_messages() else: - mentions = api.mentions_timeline(since_id=last_dm[0]) - tfglobals.last_twitter_request = time() + mentions = api.direct_messages(since_id=last_dm[0]) + user.set_last_twitter_request(time()) for status in mentions: text = re.sub( "(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)", @@ -59,9 +62,13 @@ class TwitterBot(Bot): # :todo implement rate limiting except requests.exceptions.ConnectionError: logger.error("Twitter API Error: Bad Connection", exc_info=True) - except tweepy.TweepError: + except tweepy.TweepError as terror: + # Waiting for https://github.com/tweepy/tweepy/pull/1109 to get + # merged, so direct messages work again + if terror.api_code == 34: + return reports logger.error("Twitter API Error: General Error", exc_info=True) - return [] + return reports def post(self, user, report): pass diff --git a/active_bots/twitterbot.py b/active_bots/twitterbot.py index 24ea012..0efb14f 100755 --- a/active_bots/twitterbot.py +++ b/active_bots/twitterbot.py @@ -7,7 +7,6 @@ import requests from time import time import report from bot import Bot -import tfglobals logger = logging.getLogger(__name__) @@ -30,9 +29,11 @@ class TwitterBot(Bot): :return: reports: (list of report.Report objects) """ reports = [] - #global last_twitter_request - if tfglobals.last_twitter_request + 60 > time(): - return reports + try: + if user.get_last_twitter_request() + 60 > time(): + return reports + except TypeError: + user.set_last_twitter_request(time()) try: api = self.get_api(user) except TypeError: @@ -46,13 +47,12 @@ class TwitterBot(Bot): mentions = api.mentions_timeline() else: mentions = api.mentions_timeline(since_id=last_mention) - tfglobals.last_twitter_request = time() + user.set_last_twitter_request(time()) for status in mentions: - text = re.sub( - "(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)", - "", status.text) - username = "@" + api.me().screen_name - if username in status.text: + if status._json['in_reply_to_status_id'] == None: + text = re.sub( + "(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)", + "", status.text) reports.append(report.Report(status.author.screen_name, self, text, diff --git a/backend.py b/backend.py index 13dcfe7..fd18aee 100755 --- a/backend.py +++ b/backend.py @@ -5,7 +5,6 @@ from config import config from db import db import logging from sendmail import sendmail -from time import time def shutdown(): @@ -16,8 +15,6 @@ def shutdown(): exit(1) -last_twitter_request = time() - if __name__ == '__main__': logger = logging.getLogger() fh = logging.FileHandler('/var/log/ticketfrei/backend.log') diff --git a/db.py b/db.py index cd6b99b..8212560 100644 --- a/db.py +++ b/db.py @@ -141,6 +141,12 @@ class DB(object): mail_date REAL, FOREIGN KEY(user_id) REFERENCES user(id) ); + CREATE TABLE IF NOT EXISTS twitter_last_request ( + id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, + user_id INTEGER, + date INTEGER, + FOREIGN KEY(user_id) REFERENCES user(id) + ); CREATE TABLE IF NOT EXISTS cities ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, user_id INTEGER, @@ -246,17 +252,15 @@ u\d\d?""" else: uid = json['uid'] with open("/etc/aliases", "a+") as f: - f.write(city + ": " + config["mail"]["mbox_user"]) + f.write(city + ": " + config["mail"]["mbox_user"] + "\n") self.execute("INSERT INTO email (user_id, email) VALUES(?, ?);", (uid, json['email'])) self.execute("""INSERT INTO telegram_accounts (user_id, apikey, active) VALUES(?, ?, ?);""", (uid, "", 1)) - self.execute( - "INSERT INTO seen_telegrams (user_id, tg_id) VALUES (?, ?);", (uid, 0)) - self.execute( - "INSERT INTO seen_mail (user_id, mail_date) VALUES (?, ?);", (uid, 0)) - self.execute("INSERT INTO seen_tweets (user_id, tweet_id) VALUES (?, ?)", - (uid, 0)) + self.execute("INSERT INTO seen_telegrams (user_id, tg_id) VALUES (?, ?);", (uid, 0)) + self.execute("INSERT INTO seen_mail (user_id, mail_date) VALUES (?, ?);", (uid, 0)) + self.execute("INSERT INTO seen_tweets (user_id, tweet_id) VALUES (?, ?)", (uid, 0)) + self.execute("INSERT INTO twitter_last_request (user_id, date) VALUES (?, ?)", (uid, 0)) self.commit() user = User(uid) user.set_city(city) diff --git a/frontend.py b/frontend.py index 8ae5273..6acf3f4 100755 --- a/frontend.py +++ b/frontend.py @@ -45,7 +45,7 @@ def register_post(): sendmail( email, "Confirm your account", - "Complete your registration here: %s" % (link) + body="Complete your registration here: %s" % (link) ) return dict(info='Confirmation mail sent.') except Exception: @@ -105,7 +105,7 @@ def subscribe_mail(city): # send mail with code to email sendmail(email, "Subscribe to Ticketfrei " + city + " Mail Notifications", body="To subscribe to the mail notifications for Ticketfrei " + - city + ", click on this link: " + confirm_link) + city + ", click on this link: " + confirm_link, city=city) return city_page(city, info="Thanks! You will receive a confirmation mail.") diff --git a/tfglobals.py b/tfglobals.py deleted file mode 100644 index 32282b5..0000000 --- a/tfglobals.py +++ /dev/null @@ -1,10 +0,0 @@ -from time import time - -""" -This file is for shared global variables. They only stay during runtime. - -For reference: -https://stackoverflow.com/questions/15959534/visibility-of-global-variables-in-imported-modules -""" - -last_twitter_request = time() diff --git a/user.py b/user.py index 9074b5a..dad9b1c 100644 --- a/user.py +++ b/user.py @@ -93,6 +93,16 @@ schlitz return False return True + def get_last_twitter_request(self): + db.execute("SELECT date FROM twitter_last_request WHERE user_id = ?;", + (self.uid,)) + return db.cur.fetchone()[0] + + def set_last_twitter_request(self, date): + db.execute("UPDATE twitter_last_request SET date = ? WHERE user_id = ?;", + (date, self.uid)) + db.commit() + def get_telegram_credentials(self): db.execute("""SELECT apikey FROM telegram_accounts