forked from ticketfrei/ticketfrei
fixing #39 - saving last request in global var, not db.
This commit is contained in:
parent
9b3efd7bd2
commit
304d83ffad
|
@ -7,6 +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
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
@ -29,11 +30,9 @@ class TwitterBot(Bot):
|
||||||
:return: reports: (list of report.Report objects)
|
:return: reports: (list of report.Report objects)
|
||||||
"""
|
"""
|
||||||
reports = []
|
reports = []
|
||||||
try:
|
global last_twitter_request
|
||||||
if user.get_last_twitter_request() + 60 > time():
|
if last_twitter_request + 60 > time():
|
||||||
return reports
|
return reports
|
||||||
except TypeError:
|
|
||||||
user.set_last_twitter_request(time())
|
|
||||||
try:
|
try:
|
||||||
api = self.get_api(user)
|
api = self.get_api(user)
|
||||||
except Exception:
|
except Exception:
|
||||||
|
@ -45,7 +44,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)
|
||||||
user.set_last_twitter_request(time())
|
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-_]+)",
|
||||||
|
|
|
@ -5,7 +5,7 @@ from config import config
|
||||||
from db import db
|
from db import db
|
||||||
import logging
|
import logging
|
||||||
from sendmail import sendmail
|
from sendmail import sendmail
|
||||||
import time
|
from time import time
|
||||||
|
|
||||||
|
|
||||||
def shutdown():
|
def shutdown():
|
||||||
|
@ -16,12 +16,15 @@ def shutdown():
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
last_twitter_request = time()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
logger = logging.getLogger()
|
logger = logging.getLogger()
|
||||||
fh = logging.FileHandler('/var/log/ticketfrei/backend.log')
|
fh = logging.FileHandler('/var/log/ticketfrei/backend.log')
|
||||||
fh.setLevel(logging.DEBUG)
|
fh.setLevel(logging.DEBUG)
|
||||||
logger.addHandler(fh)
|
logger.addHandler(fh)
|
||||||
|
|
||||||
|
|
||||||
bots = []
|
bots = []
|
||||||
for ActiveBot in active_bots.__dict__.values():
|
for ActiveBot in active_bots.__dict__.values():
|
||||||
if isinstance(ActiveBot, type) and issubclass(ActiveBot, Bot):
|
if isinstance(ActiveBot, type) and issubclass(ActiveBot, Bot):
|
||||||
|
|
6
db.py
6
db.py
|
@ -93,12 +93,6 @@ class DB(object):
|
||||||
active INTEGER,
|
active INTEGER,
|
||||||
FOREIGN KEY(user_id) REFERENCES user(id)
|
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 telegram_accounts (
|
CREATE TABLE IF NOT EXISTS telegram_accounts (
|
||||||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
|
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
|
||||||
user_id INTEGER,
|
user_id INTEGER,
|
||||||
|
|
|
@ -231,8 +231,8 @@ def twitter_callback(user):
|
||||||
@post('/login/mastodon')
|
@post('/login/mastodon')
|
||||||
def login_mastodon(user):
|
def login_mastodon(user):
|
||||||
"""
|
"""
|
||||||
Starts the mastodon OAuth authentication process.
|
Mastodon OAuth authentication process.
|
||||||
:return: redirect to twitter.
|
:return: redirect to city page.
|
||||||
"""
|
"""
|
||||||
# get app tokens
|
# get app tokens
|
||||||
instance_url = request.forms.get('instance_url')
|
instance_url = request.forms.get('instance_url')
|
||||||
|
|
10
user.py
10
user.py
|
@ -144,16 +144,6 @@ schlitz
|
||||||
keys.append(row[1])
|
keys.append(row[1])
|
||||||
return keys
|
return keys
|
||||||
|
|
||||||
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 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))
|
||||||
|
|
Loading…
Reference in a new issue