small fixes and improvements
This commit is contained in:
@@ -4,12 +4,17 @@ from datetime import date
|
||||
from config import Config
|
||||
f=file('config.cfg')
|
||||
cfg=Config(f)
|
||||
server = imapclient.IMAPClient(cfg.host, use_uid=True, ssl=True)
|
||||
server.login(cfg.user, cfg.password)
|
||||
server.select_folder('INBOX')
|
||||
|
||||
try:
|
||||
server = imapclient.IMAPClient(cfg.host, use_uid=True, ssl=True)
|
||||
server.login(cfg.user, cfg.password)
|
||||
server.select_folder('INBOX')
|
||||
except Error:
|
||||
print "error initializing server"
|
||||
server=None
|
||||
|
||||
def fetch_mail(myid):
|
||||
if server is None:
|
||||
raise ValueError("Server is None")
|
||||
m=server.fetch([myid],['ENVELOPE','RFC822'])
|
||||
m=m[myid]
|
||||
m["id"]=myid
|
||||
@@ -19,7 +24,9 @@ def fetch_thread(tp):
|
||||
return tp
|
||||
|
||||
def fetch_threads():
|
||||
src=server.thread(criteria=[b'SUBJECT', b'service', b'SINCE', date(2017,05,01)])
|
||||
if server is None:
|
||||
raise ValueError("Server is None")
|
||||
src=server.thread(criteria=[b'SUBJECT', b'service', b'SINCE', date(2017,02,01)])
|
||||
#, b'BEFORE', date(2017,08,01)
|
||||
return src
|
||||
|
||||
@@ -44,12 +51,17 @@ def store_threads(thrds):
|
||||
if th == None:
|
||||
th=MailThread()
|
||||
th.firstmail=t[0]
|
||||
th.body=yaml.dump(t) # body zb (27422,27506), (27450,)
|
||||
th.islabeled=False
|
||||
th.opened=True
|
||||
th.istrained=False
|
||||
elif not th.body == yaml.dump(t): # Ansonsten body vergleichen
|
||||
th.body=yaml.dump(t) # body zb (27422,27506), (27450,)
|
||||
th.islabeled=False
|
||||
th.opened=True
|
||||
else:
|
||||
th.body=yaml.dump(t)
|
||||
th.istrained=False
|
||||
# else:
|
||||
# th.body=yaml.dump(t)
|
||||
db_session.add(th)
|
||||
db_session.commit()
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import yaml
|
||||
import email
|
||||
from fetch_mail import fetch_mail
|
||||
import bs4
|
||||
import re
|
||||
class FullMailSchema(Schema):
|
||||
id=fields.Integer()
|
||||
text=fields.String()
|
||||
@@ -56,6 +57,8 @@ class Mail(Base):
|
||||
return mm
|
||||
|
||||
def get_email(self):
|
||||
if self.body is None:
|
||||
raise ValueError("body not yet loaded")
|
||||
em=email.message_from_string(yaml.load(self.body))
|
||||
return em
|
||||
|
||||
@@ -91,11 +94,10 @@ class Mail(Base):
|
||||
|
||||
def dict_envelope(self):
|
||||
d={}
|
||||
i=0
|
||||
for p in yaml.load(self.subject):
|
||||
if p is not None:
|
||||
d["subject_"+str(i)]=p
|
||||
i=i+1
|
||||
if self.to_ is None:
|
||||
self.compile_envelope()
|
||||
if self.to_ is None:
|
||||
raise ValueError("Self.to_ of mail not yet compiled")
|
||||
i=0
|
||||
for p in yaml.load(self.to_):
|
||||
if p["host"] is not None:
|
||||
@@ -126,3 +128,19 @@ class Mail(Base):
|
||||
self.text= yaml.dump(b4.get_text())
|
||||
else:
|
||||
self.text =yaml.dump( pl)
|
||||
def print_text(self):
|
||||
txt=""
|
||||
fr=yaml.load(self.from_)
|
||||
txt= txt+ "Gesendet von: "+str(fr[0]["mail"])+"@"+str(fr[0]["host"])+" am "+ str(self.date) + "\n"
|
||||
t=yaml.load(self.text)
|
||||
if type(t) is unicode:
|
||||
#txt=txt
|
||||
txt=txt+t
|
||||
else:
|
||||
t=t.decode("ISO-8859-1")
|
||||
txt=txt+t
|
||||
txt=re.sub(r'\n\s*\n',r'\n',txt)
|
||||
txt=re.sub(r'<!--.*-->',r'',txt,flags=re.MULTILINE|re.DOTALL)
|
||||
txt=re.sub(r'\s*>+ .*\n',r'',txt)
|
||||
|
||||
return txt
|
||||
|
||||
@@ -73,7 +73,7 @@ class MailThread(Base):
|
||||
a=[]
|
||||
# print "maildicts: "+ str(self.mails())
|
||||
for m in self.mails():
|
||||
m.compile_envelope()
|
||||
# m.compile_envelope()
|
||||
a.append(m.dict_envelope())
|
||||
return a
|
||||
|
||||
@@ -85,6 +85,9 @@ class MailThread(Base):
|
||||
for i in range(0,len(dc)):
|
||||
for k, v in dc[i].iteritems():
|
||||
d["mail_"+str(i)+"_"+k]=v
|
||||
for k, v in dc[-1].iteritems():
|
||||
d["mail_last_"+k]=v
|
||||
|
||||
return d
|
||||
|
||||
def subject(self):
|
||||
@@ -103,30 +106,13 @@ class MailThread(Base):
|
||||
self.date=self.mails()[0].date
|
||||
|
||||
def print_text(self,filter="all"):
|
||||
a=u""
|
||||
def mail_txt(m):
|
||||
#txt ="Gesendet von: "+ str(m.from_mailbox)+"@"+str(m.from_host) +"\n"
|
||||
txt=""
|
||||
fr=yaml.load(m.from_)
|
||||
txt= txt+ "Gesendet von: "+str(fr[0]["mail"])+"@"+str(fr[0]["host"])+" am "+ str(m.date) + "\n"
|
||||
t=yaml.load(m.text)
|
||||
if type(t) is unicode:
|
||||
#txt=txt
|
||||
txt=txt+t
|
||||
else:
|
||||
t=t.decode("ISO-8859-1")
|
||||
txt=txt+t
|
||||
return txt
|
||||
|
||||
a=[]
|
||||
if filter=="all":
|
||||
mm=self.mails()
|
||||
for m in mm:
|
||||
a=a+mail_txt(m)+"\n****........................................***\n"
|
||||
a.append(m.print_text())
|
||||
elif filter=="first":
|
||||
a=mail_txt(m[0])
|
||||
a=re.sub(r'\n\s*\n',r'\n',a)
|
||||
a=re.sub(r'<!--.*-->',r'',a,flags=re.MULTILINE|re.DOTALL)
|
||||
a=re.sub(r'\s*>+ .*\n',r'',a)
|
||||
a.append(m[0].print_text())
|
||||
return a
|
||||
def text(self,filter="all"):
|
||||
a=u""
|
||||
@@ -143,7 +129,7 @@ class MailThread(Base):
|
||||
for m in mm:
|
||||
a=a+mail_txt(m)+"\n****........................................***\n"
|
||||
elif filter=="first":
|
||||
a=mail_txt(m[0])
|
||||
a=mail_txt(mm[0])
|
||||
a=re.sub(r'\n\s*\n',r'\n',a)
|
||||
a=re.sub(r'<!--.*-->',r'',a,flags=re.MULTILINE|re.DOTALL)
|
||||
a=re.sub(r'\s*>+ .*\n',r'',a)
|
||||
|
||||
Reference in New Issue
Block a user