From df007cd7e34a1e5f791425fae584df4aa5c2dd40 Mon Sep 17 00:00:00 2001 From: b3yond Date: Sat, 6 Oct 2018 10:44:07 +0200 Subject: [PATCH] globals are in a separate python file now #39 #45 --- active_bots/twitterDMs.py | 5 +++++ active_bots/twitterbot.py | 12 +++++++----- tfglobals.py | 3 +++ user.py | 16 ++++++++-------- 4 files changed, 23 insertions(+), 13 deletions(-) create mode 100644 tfglobals.py diff --git a/active_bots/twitterDMs.py b/active_bots/twitterDMs.py index 36b2562..a9bd5e4 100644 --- a/active_bots/twitterDMs.py +++ b/active_bots/twitterDMs.py @@ -5,6 +5,8 @@ import tweepy import re import requests import report +import tfglobals +from time import time from bot import Bot @@ -27,6 +29,8 @@ class TwitterBot(Bot): :return: reports: (list of report.Report objects) """ reports = [] + if tfglobals.last_twitter_request + 60 > time(): + return reports try: api = self.get_api(user) except IndexError: @@ -37,6 +41,7 @@ class TwitterBot(Bot): mentions = api.direct_messages() else: mentions = api.mentions_timeline(since_id=last_dm[0]) + tfglobals.last_twitter_request = time() for status in mentions: text = re.sub( "(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)", diff --git a/active_bots/twitterbot.py b/active_bots/twitterbot.py index 2bda8fb..24ea012 100755 --- a/active_bots/twitterbot.py +++ b/active_bots/twitterbot.py @@ -7,7 +7,7 @@ import requests from time import time import report from bot import Bot -from backend import last_twitter_request +import tfglobals logger = logging.getLogger(__name__) @@ -30,12 +30,14 @@ class TwitterBot(Bot): :return: reports: (list of report.Report objects) """ reports = [] - global last_twitter_request - if last_twitter_request + 60 > time(): + #global last_twitter_request + if tfglobals.last_twitter_request + 60 > time(): return reports try: api = self.get_api(user) - except Exception: + except TypeError: + # When there is no twitter account for this bot, we want to + # seamlessly continue. #logger.error("Error Authenticating Twitter", exc_info=True) return reports last_mention = user.get_seen_tweet() @@ -44,7 +46,7 @@ class TwitterBot(Bot): mentions = api.mentions_timeline() else: mentions = api.mentions_timeline(since_id=last_mention) - last_twitter_request = time() + tfglobals.last_twitter_request = time() for status in mentions: text = re.sub( "(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)", diff --git a/tfglobals.py b/tfglobals.py new file mode 100644 index 0000000..ded3bb1 --- /dev/null +++ b/tfglobals.py @@ -0,0 +1,3 @@ +from time import time + +last_twitter_request = time() diff --git a/user.py b/user.py index a2e8821..9074b5a 100644 --- a/user.py +++ b/user.py @@ -136,14 +136,6 @@ schlitz instance = db.cur.fetchone() return instance[1], instance[2], row[0], instance[0] - def get_twitter_credentials(self): - keys = [config['twitter']['consumer_key'], - config['twitter']['consumer_secret']] - row = self.get_twitter_token() - keys.append(row[0]) - keys.append(row[1]) - return keys - def toot_is_seen(self, toot_uri): db.execute("SELECT COUNT(*) FROM seen_toots WHERE user_id = ? AND toot_uri = ?;", (self.uid, toot_uri)) @@ -276,6 +268,14 @@ schlitz (self.uid, )) return db.cur.fetchone() + def get_twitter_credentials(self): + keys = [config['twitter']['consumer_key'], + config['twitter']['consumer_secret']] + row = self.get_twitter_token() + keys.append(row[0]) + keys.append(row[1]) + return keys + def update_telegram_key(self, apikey): db.execute("UPDATE telegram_accounts SET apikey = ? WHERE user_id = ?;", (apikey, self.uid)) db.commit()