started mail rewrite

master
b3yond 2018-03-29 00:13:00 +02:00
parent 85fcd06e26
commit d8b2b811da
3 changed files with 10 additions and 22 deletions

View File

@ -9,12 +9,13 @@ import email
import imaplib import imaplib
import report import report
from user import User from user import User
from bot import Bot
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class Mailbot(object): class Mailbot(Bot):
""" """
Bot which sends Mails if mentioned via twitter/mastodon, and tells Bot which sends Mails if mentioned via twitter/mastodon, and tells
other bots that it received mails. other bots that it received mails.
@ -26,15 +27,8 @@ class Mailbot(object):
bots. bots.
""" """
self.user = User(uid)
try: try:
self.last_mail = self.user.get_seen_mail() self.mailinglist = self.user.get_mailinglist()
except TypeError:
self.last_mail = 0
try:
self.mailinglist = self.user.get_mail()
except TypeError: except TypeError:
self.mailinglist = None self.mailinglist = None
@ -67,11 +61,12 @@ class Mailbot(object):
""" """
pass pass
def crawl(self): def crawl(self, user):
""" """
crawl for new mails. crawl for new mails.
:return: msgs: (list of report.Report objects) :return: msgs: (list of report.Report objects)
""" """
mailinglist = user.get_mailinglist()
try: try:
rv, data = self.mailbox.select("Inbox") rv, data = self.mailbox.select("Inbox")
except imaplib.IMAP4.abort: except imaplib.IMAP4.abort:
@ -90,7 +85,7 @@ class Mailbot(object):
return msgs return msgs
msg = email.message_from_bytes(data[0][1]) msg = email.message_from_bytes(data[0][1])
if not self.user.get_mail() in msg['From']: if not self.user.get_mailinglist() in msg['From']:
# get a comparable date out of the email # get a comparable date out of the email
date_tuple = email.utils.parsedate_tz(msg['Date']) date_tuple = email.utils.parsedate_tz(msg['Date'])
date_tuple = datetime.datetime.fromtimestamp( date_tuple = datetime.datetime.fromtimestamp(

10
db.py
View File

@ -97,7 +97,7 @@ class DB(object):
tweet_id TEXT, tweet_id TEXT,
FOREIGN KEY(user_id) REFERENCES user(id) FOREIGN KEY(user_id) REFERENCES user(id)
); );
CREATE TABLE IF NOT EXISTS mail ( CREATE TABLE IF NOT EXISTS mailinglist (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
user_id INTEGER, user_id INTEGER,
email TEXT, email TEXT,
@ -106,14 +106,6 @@ class DB(object):
FOREIGN KEY(twitter_accounts_id) FOREIGN KEY(twitter_accounts_id)
REFERENCES twitter_accounts(id) REFERENCES twitter_accounts(id)
); );
CREATE TABLE IF NOT EXISTS seen_mails (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
user_id INTEGER,
mail_id INTEGER,
mail_date INTEGER,
FOREIGN KEY(user_id) REFERENCES user(id)
FOREIGN KEY(mail_id) REFERENCES mail(id)
);
''') ''')
def user_token(self, email, password): def user_token(self, email, password):

View File

@ -108,8 +108,9 @@ class User(object):
db.execute("UPDATE seen_tweets SET tweet_id = ? WHERE user_id = ?;", db.execute("UPDATE seen_tweets SET tweet_id = ? WHERE user_id = ?;",
(tweet_id, self.uid)) (tweet_id, self.uid))
def get_mail(self): def get_mailinglist(self):
db.execute("SELECT email FROM mail WHERE user_id = ?;", (self.uid, )) db.execute("SELECT email FROM mailinglist WHERE user_id = ? AND active = 1;", (self.uid, ))
return db.cur.fetchone()[0]
def get_seen_mail(self): def get_seen_mail(self):
db.execute("SELECT mail_date FROM seen_mails WHERE user_id = ?;", (self.uid, )) db.execute("SELECT mail_date FROM seen_mails WHERE user_id = ?;", (self.uid, ))