This repository has been archived by the owner on Mar 25, 2019. It is now read-only.
forked from alexanderlissenko/snd-anteckningar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·185 lines (164 loc) · 7.01 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env python
# coding: UTF-8
from email.parser import HeaderParser
from subprocess import call
import email
import imaplib
import smtplib
import os
import re
import string
import glob
import shutil
import base64
from time import gmtime, strftime
senders = dict()
#Creates a dir for every course in sender.txt
def init_script():
f = open('sender.txt')
lines = f.readlines()
f.close()
for emailAddress in lines :
splitted = re.split(r'\s+', emailAddress.rstrip('\t\n'))
if not splitted[0][:1] == '#' :
senders[splitted[0]] = (splitted[1], splitted[2])
if not os.path.exists("www/uppladdat/{0}".format(splitted[1])):
os.makedirs("www/uppladdat/{0}".format(splitted[1]))
#Fetches the email attachment, and complise multiple .jpg's to a .pdf
def fetch_and_store():
detach_dir = '.'
m = imaplib.IMAP4_SSL("imap.gmail.com")
m.login('dtekanteckningar@gmail.com',getPassword())
m.select("inbox")
resp, items = m.search(None, "(UNSEEN)")
items = items[0].split()
for emailid in items:
resp, data = m.fetch(emailid, "(RFC822)")
email_body = data[0][1]
resp2, header = m.fetch(emailid, '(BODY[HEADER.FIELDS (SUBJECT)])')
subject = header[0][1]
parser = HeaderParser()
msg = parser.parsestr(subject)
subjectline = "".join(re.findall(r'[0-9a-zA-Z\-]', msg["Subject"]))
mail = email.message_from_string(email_body)
from email.utils import parseaddr
fromaddr = parseaddr(mail['from'])[1]
name = parseaddr(mail['from'])[0]
temp = m.store(emailid,'+FLAGS', '\\Seen')
m.expunge()
sender = checkSender(fromaddr)
if not parseSubLine(subjectline) or not sender : #Ifall mailet har fel rubrik, går vi in här
if not parseSubLine(subjectline) :
print "Warning: Mail has wrong subject"
sendEmail(name, fromaddr, subjectline, "2") #Skickar ett mail till avsändaren om att dess mail haft fel format på rubriken
elif not sender :
print "Warning: Address not in senders file"
sendEmail(name, fromaddr, subjectline, "1") #Skickar ett mail till avsändaren om adressen inte finns i "sender.txt"-listan
else:
if not os.path.exists(subjectline):
os.makedirs(subjectline)
if mail.get_content_maintype() != 'multipart':
continue
filenamelist = []
for part in mail.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
filename = "".join(re.findall(r'[.0-9a-zA-Z\-]',part.get_filename()))
att_path = os.path.join(detach_dir + "/" + subjectline, filename)
filenamelist.append(filename)
if not os.path.isfile(att_path) :
fp = open(att_path, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
course = sender[0]
name = sender[1]
dest = "www/uppladdat/" + course + "/{0}.pdf".format(setFileName(sender,subjectline))
convertStoredToPdf(subjectline, dest)
def convertStoredToPdf(dirName, destName) :
pdfArray = [os.path.basename(path) for path in glob.glob(dirName + "/*.pdf")]
if len(pdfArray) == 1 :
path = dirName + "/" + pdfArray[0]
shutil.move(path,destName)
else :
fileList = glob.glob(dirName + "/*.jpg")
fileList.sort()
call(["convert"] + fileList + [destName])
shutil.rmtree(dirName)
# Creates a HTML-Page
def createHTML():
f = open("www/index.html", "w")
f.write('<html><head><title>Anteckningar Dtek</title><meta charset="UTF-8"><link href="mall.css" rel="stylesheet" media="all"></head><body>')
f.write('<div class="flex-container flex-container-style fixed-height">')
f.write( listFileHTML() )
f.write('</div>')
f.write('<div class="disc"><a href="http://snd.dtek.se"><img src="http://snd.dtek.se/wp-content/uploads/2013/02/sndlogga.png"></a>')
f.write('<div class="discText"> Disclaimer: På denna sidan samlas anteckningar gjorda av Teknologer på D-sektionen. Vi i SND kan ej garantera att anteckningarna stämmer.</div>')
f.write('<div class="discText"> Uppdaterad: ' + strftime("%Y-%m-%d %H:%M:%S", gmtime()) + '</div></div>')
f.write('</body></html>') #Bygger html-sidan
def listFileHTML():
string = ""
for dir in sortDir(os.listdir("www/uppladdat")):
if os.path.isdir("www/uppladdat/{0}".format(dir)):
string += str('<div class="flex-item"><h1>{0}</h1><ul id="{0}">'.format(dir))
for fil in sortDir(os.listdir("www/uppladdat/{0}".format(dir))) :
if fil.endswith(".pdf") :
string += str('<li><a href="uppladdat/{0}/{1}">{2}</a></li>'.format(fil[:6],fil,fil[7:]))
string += str('</ul></div>')
return string
def checkSender(mail) :
if mail in senders:
return senders[mail]
else:
return False
def sortDir(dir) :
return sorted(dir, key=lambda x: x.split('-')[1:4])
def parseSubLine(string):
splitted = string.split("-")
if not (len(splitted) == 2 or len(splitted) == 3):
return False
if len(splitted[0]) > 2 or len(splitted[1]) > 2 :
return False
if re.match("[0-9]",splitted[0]) and re.match("[0-9]",splitted[1]) :
return True
return False
def setFileName(sender,subjectline) :
course = sender[0]
name = sender[1]
splitted = subjectline.split("-")
year = strftime("%Y", gmtime())
date = "{0}-{1}".format(splitted[0],splitted[1])
info = ""
if len(splitted) > 2:
info = "-{0}".format(splitted[2])
return "{0}-{1}-{2}-{3}{4}".format(course,year,date,name,info)
# Skickar ett mail om
def sendEmail(name,toaddrs,subjectline,error):
fromaddr = 'dtekanteckningar@gmail.com'
if error == "1" :
errorMsg = "Detta beror på att denna mail ej är registrerad som antecknare. Hör av dig till snd@dtek.se så fixar dem det."
elif error == "2" :
errorMsg = "Rubriken: {0} på ditt mail har fel format. Formatet skall vara MM-DD exempel 03-12 för 12 Mars. Skicka ett nytt mail med rätt rubrik :)".format(subjectline)
msg = 'Hej {0}\n\n Uppladdningen av dina antecknignar gick fel.\n{1}\n\n MVH SND'.format(name,errorMsg)
sub = "Uppladdning av dina anteckingar gick fel"
username = 'dtekanteckningar'
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,getPassword())
message = 'Subject: %s\n\n%s' % (sub, msg)
server.sendmail(fromaddr, toaddrs, message)
server.quit()
#Hämtar lösenordet till gmail-kontot
def getPassword():
password = ""
f = open('password.txt', 'r')
try:
password = f.read()
finally:
f.close()
return base64.b64decode(password)
#### Här Börjar Programet ####
init_script()
fetch_and_store()
createHTML()