-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhavmanifest.py
216 lines (205 loc) · 10.3 KB
/
havmanifest.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import email
import re
import os
import sys
import imaplib
import smtplib
import ctypes
import getpass
import quopri
import telegram
import pdfkit
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import formatdate
from email import encoders
import pandas as pd
import numpy as np
from bs4 import BeautifulSoup
from email.header import decode_header
from time import sleep
mail = imaplib.IMAP4_SSL('',993) #add the imap server here
unm = '' #add the username here
pwd = '' #add the password here
mail.login(unm,pwd)
mail.select("INBOX")
mga_contacts=[''] #add email accounts to send the PDF to here. separate them with comas
mailtothis = '' #add single account to send EMAIL TO HERE
#mailtothis = '[email protected]'
server = smtplib.SMTP('') #add the smtp server here
server.starttls()
server.login(unm,pwd)
#bot = telegram.Bot(token="394211218%3AAAF7EfA7GHw6nccUJQIUhIGlegQTWXgkBMg")
chat_id = "14185356"
def loop():
mail.select('TEST')
n=0
seat=""
pnr=""
bags=""
(retcode ,messages)=mail.search(None,'(UNSEEN)')
if retcode == 'OK':
#bot.send_message(chat_id=chat_id,text="OK", parse_mode=telegram.ParseMode.MARKDOWN)
for num in messages[0].split():
n +=1
print(n)
typ, data = mail.fetch(num, '(RFC822)')
raw_email = data[0][1]
raw_email_string = raw_email.decode('utf-8')
email_message = email.message_from_string(raw_email_string)
email_from = email.utils.parseaddr(email_message['From'])[1]
subject = str(email.header.make_header(email.header.decode_header(email_message['Subject'])))
if "PNL AG" in subject :
print(subject)
splitted = subject.split()
print (splitted[3])
just_number = splitted[3].split()
if just_number.isdigit():
print(just_number)
if "PRL" in subject :
print("detectado")
print(subject)
pre,fnp = subject.split("PRL", 1)
fnp=fnp.split()
fname = fnp[0].replace('/','_')
for part in email_message.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
fileName = part.get_filename()
print(fileName)
if bool(fileName):
filePath = os.path.join('.',fileName)
if not os.path.isfile(filePath) :
fp = open(filePath, 'wb')
fp.write(part.get_payload(decode=True))
#print(type(fp))
fp.close()
list_apis = []
#print(textodeemail)
#testprl = testprl.replace('HAV','AUA')
#send_body ='\n'.join(textodeemail.split('\n')[3:-20])
with open(filePath) as f:
for line in f:
if line[0]=="1":
bagsearch = re.search("\.W/K/(.+?) ",line)
if bagsearch:
bags = bagsearch.groups()[0]
else:
bags = ""
pnrsearch = re.search("\.L/(.+?) ",line)
if pnrsearch:
pnr = pnrsearch.groups()[0]
#print (pnr)
else:
pnr=""
seatsearch = re.search("\.R/SEAT HK1 (.+?) ",line)
if seatsearch:
seat = seatsearch.groups()[0]
#print (seat)
else:
seatline = "%s" % next(f)
seatagain = re.search("\.R/SEAT HK1 (.+?) ",seatline)
seat=seatagain.groups()[0]
if line.startswith(".R/DOCS"):
param, value = line.split("DOCS HK1",1)
value = re.split(r'/', value)
names = "%s" % next(f)
if names.startswith(".RN/"):
namess = names.split(".RN/",1)
elif names.startswith("1"):
namess = names.split("1",1)
namessplit = re.split(r'/',namess[1])
#print(value)
list_apis.append([pnr,namessplit[1].strip(),namessplit[0],seat,bags,value[4],value[3],value[5]])
elif line.startswith(".R/PSPT"):
continue
#param, value = line.split("PSPT HK1",1)
#value = re.split(r'/',value)
#list_apis.append([value[4],value[3],value[1],value[0],value[2]])
data_to_send = pd.DataFrame(list_apis,columns=['PNR','FIRTS NAME','LAST NAME','SEAT','BAGS','NATIONALITY','DOCS','DOB'])
data_to_send.index += 1
data_to_send.replace(np.nan, '', regex=True)
#data_to_send.to_excel(r'APIS %s.xlsx' % fname, index=None, header=True)
data_to_send.to_html('test.html')
with open('test.html', 'r') as original: data = original.read()
with open('test.html', 'w') as modified: modified.write ("<html><head><link rel='stylesheet' type='text/css' href='table.css' media='screen' /><body><h2> Lista de Pasajeros</h2>"+data+"</body></html>")
PdfFilename=r'paxlist.pdf'
pdfkit.from_file('test.html', PdfFilename)
documentofinal=r'Manifiesto de Pasajeros.pdf' #% (vuelo,fechavuelo)
#break
print (data_to_send.shape[0])
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(unm,pwd)
msg=MIMEMultipart()
msg['From'] = "[email protected]"
msg['To'] = "[email protected]"
msg['Date'] = formatdate(localtime = True)
msg['Subject'] = "Apis Final %s " % fname
msg.attach(MIMEText("Saludos, \n Anexo APIS Finales vuelo %s" % fname))
part2 = MIMEBase('application', "octet-stream")
part2.set_payload(open(PdfFilename, "rb").read())
encoders.encode_base64(part2)
part2.add_header('Content-Disposition', 'attachment; filename="Manifiesto %s.pdf"' % fname)
msg.attach(part2)
msgit = "\r\n".join([
"From: [email protected]",
"To: [email protected]",
"Subject: APIS Test",
"",
"test",
])
#server.sendmail('[email protected]', '[email protected]', msg.as_string())
server.quit()
break
#for part in email_message.walk():
#body = part.get_payload()
#print(body)
#print(part)
#replaced = body.replace('HAV AUA MGA','AUA MGA')
#cleantext = BeautifulSoup(body).text
#body = "\n".join(filter(None,cleantext.split("\n")[3:]))
#textodeemail = "%s" % body[0]
#print(textodeemail)
#textodeemail = textodeemail.replace(' HAV AUA MGA',' AUA MGA')
#textodeemail = textodeemail.replace('HAV','AUA')
#send_body ='\n'.join(textodeemail.split('\n')[3:-20])
#print(send_body)
#break
#print (send_body)
#server = smtplib.SMTP('smtp.gmail.com:587')
#server.ehlo()
#server.starttls()
#server.login(unm,pwd)
#msgit = "\r\n".join([
# "From: [email protected]",
# "To: [email protected]",
# "Subject: APIS Test",
# "",
# "%s" % send_body,
# ])
#server.sendmail('[email protected]', mailtothis, msgit)
#server.quit()
#break
#texto = """*%s*\n%s""" % (subject,body)
#print(body),subject,email_from)
#bot.send_message(chat_id=chat_id,text=texto,parse_mode=telegram.ParseMode.MARKDOWN)
for respone_part in data:
if isinstance (respone_part, tuple):
original = email.message_from_string(respone_part[1].decode())
#print original['From']
data = original['Subject']
#print quopri.decodestring(data)
typ, data = mail.store(num,'+FLAGS','\\Seen')
continue
#print(n)
if __name__ == '__main__':
try:
while True:
loop()
finally:
print ("Thanks")