started to build a mailbot implementing bot.py
This commit is contained in:
parent
ef0ce8f9f1
commit
ec399db2eb
60
active_bots/mailbot.py
Normal file
60
active_bots/mailbot.py
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import sendmail
|
||||||
|
import datetime
|
||||||
|
import email
|
||||||
|
import report
|
||||||
|
from bot import Bot
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class Mailbot(Bot):
|
||||||
|
|
||||||
|
# returns a list of Report objects
|
||||||
|
def crawl(self, user):
|
||||||
|
reports = []
|
||||||
|
mails = [] # todo: look if new reports are in mailbox
|
||||||
|
for msg in mails:
|
||||||
|
if msg.date > user.get_seen_mail():
|
||||||
|
reports.append(make_report(msg, user))
|
||||||
|
return reports
|
||||||
|
|
||||||
|
# post/boost Report object
|
||||||
|
def post(self, user, report):
|
||||||
|
recipients = user.get_mailinglist()
|
||||||
|
for rec in recipients:
|
||||||
|
rec = rec[0]
|
||||||
|
unsubscribe_link = "" # todo: generate unsubscribe link
|
||||||
|
body = report.text + unsubscribe_link
|
||||||
|
if report.author != rec:
|
||||||
|
try:
|
||||||
|
sendmail.sendmail(rec, "Ticketfrei " + user.get_city() +
|
||||||
|
" Report", body=body)
|
||||||
|
except Exception:
|
||||||
|
logger.error("Sending Mail failed.", exc_info=True)
|
||||||
|
|
||||||
|
|
||||||
|
def make_report(msg, user):
|
||||||
|
"""
|
||||||
|
generates a report out of a mail
|
||||||
|
|
||||||
|
:param msg: email.parser.Message object
|
||||||
|
:return: post: report.Report object
|
||||||
|
"""
|
||||||
|
# get a comparable date out of the email
|
||||||
|
date_tuple = email.utils.parsedate_tz(msg['Date'])
|
||||||
|
date_tuple = datetime.datetime.fromtimestamp(
|
||||||
|
email.utils.mktime_tz(date_tuple)
|
||||||
|
)
|
||||||
|
date = (date_tuple - datetime.datetime(1970, 1, 1)).total_seconds()
|
||||||
|
|
||||||
|
author = msg.get("From") # get mail author from email header
|
||||||
|
# :todo take only the part before the @
|
||||||
|
|
||||||
|
text = msg.get_payload()
|
||||||
|
post = report.Report(author, "mail", text, None, date)
|
||||||
|
user.save_seen_mail(date)
|
||||||
|
return post
|
|
@ -19,7 +19,7 @@ class Report(object):
|
||||||
:param timestamp: time of the report
|
:param timestamp: time of the report
|
||||||
"""
|
"""
|
||||||
self.author = author
|
self.author = author
|
||||||
self.type = source
|
self.source = source
|
||||||
self.text = text
|
self.text = text
|
||||||
self.timestamp = timestamp
|
self.timestamp = timestamp
|
||||||
self.id = id
|
self.id = id
|
||||||
|
|
2
user.py
2
user.py
|
@ -141,7 +141,7 @@ schlitz
|
||||||
|
|
||||||
def get_mailinglist(self):
|
def get_mailinglist(self):
|
||||||
db.execute("SELECT email FROM mailinglist WHERE user_id = ? AND active = 1;", (self.uid, ))
|
db.execute("SELECT email FROM mailinglist WHERE user_id = ? AND active = 1;", (self.uid, ))
|
||||||
return db.cur.fetchone()[0]
|
return db.cur.fetchall()
|
||||||
|
|
||||||
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, ))
|
||||||
|
|
Loading…
Reference in a new issue