Merge pull request #51 from ticketfrei/master
fixing the last few issues
This commit is contained in:
commit
5c84de65ef
|
@ -34,7 +34,7 @@ class Mailbot(Bot):
|
||||||
unsubscribe_text = "\n_______\nYou don't want to receive those messages? Unsubscribe with this link: "
|
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/" \
|
body = report.text + unsubscribe_text + config['web']['host'] + "/city/mail/unsubscribe/" \
|
||||||
+ db.mail_subscription_token(rec, user.get_city())
|
+ db.mail_subscription_token(rec, user.get_city())
|
||||||
if report.author != rec:
|
if rec not in report.author:
|
||||||
try:
|
try:
|
||||||
city = user.get_city()
|
city = user.get_city()
|
||||||
sendmail(rec, "Ticketfrei " + city + " Report",
|
sendmail(rec, "Ticketfrei " + city + " Report",
|
||||||
|
@ -54,16 +54,19 @@ def make_report(msg, user):
|
||||||
date = get_date_from_header(msg['Date'])
|
date = get_date_from_header(msg['Date'])
|
||||||
|
|
||||||
author = msg['From'] # get mail author from email header
|
author = msg['From'] # get mail author from email header
|
||||||
# :todo take only the part in between the < >
|
|
||||||
|
|
||||||
if msg.is_multipart():
|
if msg.is_multipart():
|
||||||
text = []
|
text = []
|
||||||
for part in msg.get_payload():
|
for part in msg.get_payload():
|
||||||
if part.get_content_type() == "text":
|
if part.get_content_type() == "text":
|
||||||
text.append(part.get_payload())
|
text.append(part.get_payload())
|
||||||
|
elif part.get_content_type() == "application/pgp-signature":
|
||||||
|
pass # ignore PGP signatures
|
||||||
elif part.get_content_type() == "multipart/mixed":
|
elif part.get_content_type() == "multipart/mixed":
|
||||||
for p in part:
|
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())
|
text.append(part.get_payload())
|
||||||
else:
|
else:
|
||||||
logger.error("unknown MIMEtype: " +
|
logger.error("unknown MIMEtype: " +
|
||||||
|
|
|
@ -5,7 +5,6 @@ import tweepy
|
||||||
import re
|
import re
|
||||||
import requests
|
import requests
|
||||||
import report
|
import report
|
||||||
import tfglobals
|
|
||||||
from time import time
|
from time import time
|
||||||
from bot import Bot
|
from bot import Bot
|
||||||
|
|
||||||
|
@ -13,9 +12,10 @@ from bot import Bot
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class TwitterBot(Bot):
|
class TwitterDMListener(Bot):
|
||||||
|
|
||||||
def get_api(self, user):
|
def get_api(self, user):
|
||||||
keys = user.get_api_keys()
|
keys = user.get_twitter_credentials()
|
||||||
auth = tweepy.OAuthHandler(consumer_key=keys[0],
|
auth = tweepy.OAuthHandler(consumer_key=keys[0],
|
||||||
consumer_secret=keys[1])
|
consumer_secret=keys[1])
|
||||||
auth.set_access_token(keys[2], # access_token_key
|
auth.set_access_token(keys[2], # access_token_key
|
||||||
|
@ -29,19 +29,22 @@ 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():
|
try:
|
||||||
|
if user.get_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 IndexError:
|
except TypeError:
|
||||||
return reports # no twitter account for this user.
|
return reports # no twitter account for this user.
|
||||||
last_dm = user.get_seen_dm()
|
last_dm = user.get_seen_dm()
|
||||||
try:
|
try:
|
||||||
if last_dm is None:
|
if last_dm is None:
|
||||||
mentions = api.direct_messages()
|
mentions = api.direct_messages()
|
||||||
else:
|
else:
|
||||||
mentions = api.mentions_timeline(since_id=last_dm[0])
|
mentions = api.direct_messages(since_id=last_dm[0])
|
||||||
tfglobals.last_twitter_request = time()
|
user.set_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-_]+)",
|
||||||
|
@ -59,9 +62,13 @@ class TwitterBot(Bot):
|
||||||
# :todo implement rate limiting
|
# :todo implement rate limiting
|
||||||
except requests.exceptions.ConnectionError:
|
except requests.exceptions.ConnectionError:
|
||||||
logger.error("Twitter API Error: Bad Connection", exc_info=True)
|
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)
|
logger.error("Twitter API Error: General Error", exc_info=True)
|
||||||
return []
|
return reports
|
||||||
|
|
||||||
def post(self, user, report):
|
def post(self, user, report):
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -7,7 +7,6 @@ import requests
|
||||||
from time import time
|
from time import time
|
||||||
import report
|
import report
|
||||||
from bot import Bot
|
from bot import Bot
|
||||||
import tfglobals
|
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
@ -30,9 +29,11 @@ class TwitterBot(Bot):
|
||||||
:return: reports: (list of report.Report objects)
|
:return: reports: (list of report.Report objects)
|
||||||
"""
|
"""
|
||||||
reports = []
|
reports = []
|
||||||
#global last_twitter_request
|
try:
|
||||||
if tfglobals.last_twitter_request + 60 > time():
|
if user.get_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 TypeError:
|
except TypeError:
|
||||||
|
@ -46,13 +47,12 @@ 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)
|
||||||
tfglobals.last_twitter_request = time()
|
user.set_last_twitter_request(time())
|
||||||
for status in mentions:
|
for status in mentions:
|
||||||
|
if status._json['in_reply_to_status_id'] == None:
|
||||||
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-_]+)",
|
||||||
"", status.text)
|
"", status.text)
|
||||||
username = "@" + api.me().screen_name
|
|
||||||
if username in status.text:
|
|
||||||
reports.append(report.Report(status.author.screen_name,
|
reports.append(report.Report(status.author.screen_name,
|
||||||
self,
|
self,
|
||||||
text,
|
text,
|
||||||
|
|
|
@ -5,7 +5,6 @@ from config import config
|
||||||
from db import db
|
from db import db
|
||||||
import logging
|
import logging
|
||||||
from sendmail import sendmail
|
from sendmail import sendmail
|
||||||
from time import time
|
|
||||||
|
|
||||||
|
|
||||||
def shutdown():
|
def shutdown():
|
||||||
|
@ -16,8 +15,6 @@ 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')
|
||||||
|
|
18
db.py
18
db.py
|
@ -141,6 +141,12 @@ class DB(object):
|
||||||
mail_date REAL,
|
mail_date REAL,
|
||||||
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 cities (
|
CREATE TABLE IF NOT EXISTS cities (
|
||||||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
|
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
|
||||||
user_id INTEGER,
|
user_id INTEGER,
|
||||||
|
@ -246,17 +252,15 @@ u\d\d?"""
|
||||||
else:
|
else:
|
||||||
uid = json['uid']
|
uid = json['uid']
|
||||||
with open("/etc/aliases", "a+") as f:
|
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(?, ?);",
|
self.execute("INSERT INTO email (user_id, email) VALUES(?, ?);",
|
||||||
(uid, json['email']))
|
(uid, json['email']))
|
||||||
self.execute("""INSERT INTO telegram_accounts (user_id, apikey,
|
self.execute("""INSERT INTO telegram_accounts (user_id, apikey,
|
||||||
active) VALUES(?, ?, ?);""", (uid, "", 1))
|
active) VALUES(?, ?, ?);""", (uid, "", 1))
|
||||||
self.execute(
|
self.execute("INSERT INTO seen_telegrams (user_id, tg_id) VALUES (?, ?);", (uid, 0))
|
||||||
"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(
|
self.execute("INSERT INTO seen_tweets (user_id, tweet_id) VALUES (?, ?)", (uid, 0))
|
||||||
"INSERT INTO seen_mail (user_id, mail_date) VALUES (?, ?);", (uid, 0))
|
self.execute("INSERT INTO twitter_last_request (user_id, date) VALUES (?, ?)", (uid, 0))
|
||||||
self.execute("INSERT INTO seen_tweets (user_id, tweet_id) VALUES (?, ?)",
|
|
||||||
(uid, 0))
|
|
||||||
self.commit()
|
self.commit()
|
||||||
user = User(uid)
|
user = User(uid)
|
||||||
user.set_city(city)
|
user.set_city(city)
|
||||||
|
|
|
@ -45,7 +45,7 @@ def register_post():
|
||||||
sendmail(
|
sendmail(
|
||||||
email,
|
email,
|
||||||
"Confirm your account",
|
"Confirm your account",
|
||||||
"Complete your registration here: %s" % (link)
|
body="Complete your registration here: %s" % (link)
|
||||||
)
|
)
|
||||||
return dict(info='Confirmation mail sent.')
|
return dict(info='Confirmation mail sent.')
|
||||||
except Exception:
|
except Exception:
|
||||||
|
@ -105,7 +105,7 @@ def subscribe_mail(city):
|
||||||
# send mail with code to email
|
# send mail with code to email
|
||||||
sendmail(email, "Subscribe to Ticketfrei " + city + " Mail Notifications",
|
sendmail(email, "Subscribe to Ticketfrei " + city + " Mail Notifications",
|
||||||
body="To subscribe to the mail notifications for Ticketfrei " +
|
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.")
|
return city_page(city, info="Thanks! You will receive a confirmation mail.")
|
||||||
|
|
||||||
|
|
||||||
|
|
10
tfglobals.py
10
tfglobals.py
|
@ -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()
|
|
10
user.py
10
user.py
|
@ -93,6 +93,16 @@ schlitz
|
||||||
return False
|
return False
|
||||||
return True
|
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):
|
def get_telegram_credentials(self):
|
||||||
db.execute("""SELECT apikey
|
db.execute("""SELECT apikey
|
||||||
FROM telegram_accounts
|
FROM telegram_accounts
|
||||||
|
|
Loading…
Reference in a new issue