globals are in a separate python file now #39 #45

master
b3yond 2018-10-06 10:44:07 +02:00
parent 6f5db8bb90
commit df007cd7e3
4 changed files with 23 additions and 13 deletions

View File

@ -5,6 +5,8 @@ import tweepy
import re import re
import requests import requests
import report import report
import tfglobals
from time import time
from bot import Bot from bot import Bot
@ -27,6 +29,8 @@ class TwitterBot(Bot):
:return: reports: (list of report.Report objects) :return: reports: (list of report.Report objects)
""" """
reports = [] reports = []
if tfglobals.last_twitter_request + 60 > time():
return reports
try: try:
api = self.get_api(user) api = self.get_api(user)
except IndexError: except IndexError:
@ -37,6 +41,7 @@ class TwitterBot(Bot):
mentions = api.direct_messages() mentions = api.direct_messages()
else: else:
mentions = api.mentions_timeline(since_id=last_dm[0]) mentions = api.mentions_timeline(since_id=last_dm[0])
tfglobals.last_twitter_request = time()
for status in mentions: for status in mentions:
text = re.sub( text = re.sub(
"(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)", "(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)",

View File

@ -7,7 +7,7 @@ import requests
from time import time from time import time
import report import report
from bot import Bot from bot import Bot
from backend import last_twitter_request import tfglobals
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -30,12 +30,14 @@ class TwitterBot(Bot):
:return: reports: (list of report.Report objects) :return: reports: (list of report.Report objects)
""" """
reports = [] reports = []
global last_twitter_request #global last_twitter_request
if last_twitter_request + 60 > time(): if tfglobals.last_twitter_request + 60 > time():
return reports return reports
try: try:
api = self.get_api(user) 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) #logger.error("Error Authenticating Twitter", exc_info=True)
return reports return reports
last_mention = user.get_seen_tweet() last_mention = user.get_seen_tweet()
@ -44,7 +46,7 @@ class TwitterBot(Bot):
mentions = api.mentions_timeline() mentions = api.mentions_timeline()
else: else:
mentions = api.mentions_timeline(since_id=last_mention) mentions = api.mentions_timeline(since_id=last_mention)
last_twitter_request = time() tfglobals.last_twitter_request = time()
for status in mentions: for status in mentions:
text = re.sub( text = re.sub(
"(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)", "(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)",

3
tfglobals.py Normal file
View File

@ -0,0 +1,3 @@
from time import time
last_twitter_request = time()

16
user.py
View File

@ -136,14 +136,6 @@ schlitz
instance = db.cur.fetchone() instance = db.cur.fetchone()
return instance[1], instance[2], row[0], instance[0] 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): def toot_is_seen(self, toot_uri):
db.execute("SELECT COUNT(*) FROM seen_toots WHERE user_id = ? AND toot_uri = ?;", db.execute("SELECT COUNT(*) FROM seen_toots WHERE user_id = ? AND toot_uri = ?;",
(self.uid, toot_uri)) (self.uid, toot_uri))
@ -276,6 +268,14 @@ schlitz
(self.uid, )) (self.uid, ))
return db.cur.fetchone() 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): def update_telegram_key(self, apikey):
db.execute("UPDATE telegram_accounts SET apikey = ? WHERE user_id = ?;", (apikey, self.uid)) db.execute("UPDATE telegram_accounts SET apikey = ? WHERE user_id = ?;", (apikey, self.uid))
db.commit() db.commit()