-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrecolector.py
More file actions
269 lines (234 loc) · 10.7 KB
/
recolector.py
File metadata and controls
269 lines (234 loc) · 10.7 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
"""
Autores: Yusef Ahsini Ouariaghli, Pablo Díaz Masa Valencia
"""
import pandas as pd
import time
import requests
import datetime
from datetime import date
from github import Github
import smtplib
import imaplib
import email
from email.mime.text import MIMEText
from calendar import monthrange
################################################
#### FUNCIONES ####
################################################
def newsletter(mercados, prov, d, d2,):
d = d.loc[d["Ciudad de destino"] == prov]
d2 = d2.loc[d2["Ciudad de destino"] == prov]
plaztotal = d.groupby("Ciudad de destino")["Es directo"].count()*189
plazas = d.groupby("País origen")["Es directo"].count()*189
varPlazas = df_verano = round((d.groupby("País origen")["Es directo"].mean()/d2.groupby("País origen")["Es directo"].mean()-1)*100 ,2)
texto = f"El número de plazas estimadas para {prov} es de {plaztotal[prov]}"
precioMediototal = d.groupby("Ciudad de destino")["Precio"].mean()
precioMedio = d.groupby("País origen")["Precio"].mean()
varPrecio = df_verano = round((d.groupby("País origen")["Precio"].mean()/d2.groupby("País origen")["Precio"].mean()-1)*100 ,2)
texto += f" y precio medio se situó en {round(precioMediototal[prov],2)}€.\n\nPor paises emisores la variación ha sido la siguiente:\n\n"
for mercado in mercados:
texto += f"\t{mercado}: El número de plazas es {plazas[mercado]}, una variación del {varPlazas[mercado]}% , una . El precio medio se situo en un {round(precioMedio[mercado],2)}€, lo que significa una diferencia del {varPrecio[mercado]}% en comparación a la semana pasada.\n\n"
text = f"Buenas,\n\nSegún nuestros análisis, le informamos que durante la última semana el comportamiento ha sido el siguiente:\n\n{texto}Entra en https://bit.ly/skydemand para infomación más detallada.\n\nPara darse de baja enviar un email con el asunto: CANCELAR."
return text
def enviarEmails(mails,texto,prov):
text_type = "plain"
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login(Usuario, Contraseña)
for mail in mails:
msg = MIMEText(texto, text_type, 'utf-8')
msg['Subject'] = f"Info turística {prov}"
msg['From'] = 'skydemand.upv@gmail.com'
msg['To'] = mail
server.send_message(msg)
server.quit()
def emails():
host = 'imap.gmail.com'
username = "USUARIO"
password = "Contraseña"
mail = imaplib.IMAP4_SSL(host)
mail.login(username, password)
mail.select("inbox")
_, search_data = mail.search(None, 'ALL') # con ALL mira todos los correos, con UNSEEN sólo los correos sin abrir y con SEEN los abiertos
newslt_VLC = []
newslt_ALC = []
newslt_TCI = []
#newslt_PMI = []
for num in search_data[0].split():
email_data = {}
_, data = mail.fetch(num, '(RFC822)')
_, b = data[0]
email_message = email.message_from_bytes(b)
palabras_asunto = email_message['subject'].split() # estructura: ['Suscripcion', 'VLC', 'user@example.com']
# print(palabras_asunto)
if palabras_asunto[0] == 'Suscripcion':
if palabras_asunto[1] == 'Valencia':
newslt_VLC.append(palabras_asunto[2]) # se añade el correo
elif palabras_asunto[1] == 'Alicante' :
newslt_ALC.append(palabras_asunto[2])
elif palabras_asunto[1] == 'Tenerife':
newslt_TCI.append(palabras_asunto[2])
#elif palabras_asunto[1] == 'Mallorca':
#newslt_PMI.append(palabras_asunto[2]) En una semana.
return [newslt_VLC, newslt_ALC, newslt_TCI] #,newslt_PMI
def subirArchivo(nombreArchivo):
g = Github(login_or_token=('TOKEN GITHUB'))
repo = g.get_user().get_repo('SkyDemand')
all_files = []
contents = repo.get_contents('')
while contents:
file_content = contents.pop(0)
if file_content.type == 'dir':
contents.extend(repo.get_contents(file_content.path))
else:
file = file_content
all_files.append(str(file).replace('ContentFile(path="','').replace('")',''))
file = open(nombreArchivo, encoding="utf8")
content = file.read()
git_file = nombreArchivo
if git_file in all_files:
contents = repo.get_contents(git_file)
repo.update_file(contents.path, nombreArchivo, content, contents.sha, branch='main')
print(git_file + ' UPDATED')
else:
repo.create_file(git_file, nombreArchivo, content, branch='main')
print(git_file + ' CREATED')
def ciudad(d):
if d == "TFS":
return "Tenerife"
if d == "VLC":
return "Valencia"
if d == "PMI":
return "Mallorca"
else:
return "Alicante"
def vuelosEnMes(anio, mes, orgien, destino, lista):
"""
Devuelve una lista anidada con los vuelos que encuentra SkyScanner con el origen, mes y año introducido
con el siguiente [Fecha, Precio, Si es directo el vuelo (True or False), Aerolinea, Origen, Destino, Ciudad, Pais]
Para la variable origen y destino introduce el código IATA en mayusculas de los perspectivos aeropuertos.
No introducir una fecha pasada.
La API tiene un limite de 50 búsquedas por minuto, cuando se supera devuelve:
{'message': 'You have exceeded the rate limit per minute for your plan, BASIC, by the API provider'}
Lista es una lista donde se añadiran los datos
"""
j=0
if destino == "ALC":
j=1
elif destino == "VLC" and orgien == "ZRH":
j=1
elif destino == "TFS" and orgien == "ZRH":
j=1
elif destino == "TFS" and orgien == "VIE":
j=1
elif destino == "TFS" and orgien == "TLS":
j=1
elif destino == "PMI" and orgien == "STN":
j=1
elif destino == "PMI" and orgien =="VCE":
j=1
elif destino == "PMI" and orgien =="SNN":
j=1
elif destino == "PMI" and orgien =="ZRH":
j=1
elif destino == "PMI" and orgien =="PRG":
j=1
elif destino == "PMI" and orgien =="TLS":
j=1
elif destino == "PMI" and orgien =="VIE":
j=1
elif destino == "PMI" and orgien =="STR":
j=1
dia = datetime.datetime.now()
m = dia.month
d = dia.day
var =1
rang = monthrange(2021, mes)[1]
if m > mes:
return lista
elif m == mes:
rang = monthrange(2021, mes)[1]
var = (date(2021, mes, rang) - date(2021, mes, d)).days
for i in range(var,rang+1):
vuelo= []
url = f"https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/browsequotes/v1.0/ES/EUR/es-ES/{orgien}-sky/{destino}-sky/{anio}-{mes:02d}-{i:02d}"
headers = {
'x-rapidapi-key': "KEY",
'x-rapidapi-host': "HOST"
}
response = requests.request("GET", url, headers=headers)
d = response.json()
try:
if len(d["Quotes"]) != 0:
for p in range(0,len(d["Quotes"])):
#creamos una lista a la que vamos agregando los datos de las distintas busquedas
vuelo.append(f"2021-{mes:02d}-{i:02d}")
vuelo.append(mes)
vuelo.append(str(datetime.date.today()))
vuelo.append(d["Quotes"][p]["MinPrice"])
vuelo.append("€")
vuelo.append(d["Quotes"][p]["Direct"])
if len(d["Quotes"]) != len(d["Carriers"]): #Previene errores, aveces hay dos vuelos y una aerolínea
vuelo.append(d["Carriers"][0]["Name"])
else:
vuelo.append(d["Carriers"][p]["Name"])
vuelo.append(orgien)
vuelo.append(destino)
vuelo.append(d["Places"][j]["CityName"])
vuelo.append(d["Places"][j]["CountryName"])
vuelo.append(ciudad(destino))
lista.append(vuelo)
vuelo= []
except:
print(d) #si por algún error el programa imprime en consola el motivo
break
return lista
################################################
#### PROGRAMA ####
################################################
starttime = time.time() #iniciamos el tiempo para nuestro bucle temporal
while True:
today = date.today()
print(today)
lista = []
orig = ["VLC", "TFS", "ALC", "PMI"] #codigo IATA del aeropuerto de destino
aeropuertos = ["FRA", "DUS", "BER", "MUC","HHN","LEJ","HAM","NRN","STR", "BRU", "CRL", "VIE", "CPH", "HEL","TLS","CDG","ORY","BVA",
"MRS","NCE","NTE","LYS","LUX","AMS","EIN","LHR","LTN", "LGW", "EDI","GLA","LPL","BHX","STN","MAN",
"NCL","BHD","BRS","PRG","ARN","ZRH","GVA","DUB","ORK","SNN","FCO","MXP","VCE"] #principales aeropuertos europeos
for destino in orig: #recolcetamos los datos para cada provincia, tarda 10 horas con esta selección
print(f"Recolectando datos de {destino}")
for aeropueto in aeropuertos: #estudiamos todos los aeropuertos de la lista
print(aeropueto)
i = 6
while True:
vuelosEnMes(2021,i,aeropueto,destino, lista)
i +=1
time.sleep(60.0 - ((time.time() - starttime) % 60.0))
if i >= 9: break
print("Recolección de datos terminada")
#### Creamos un DataFrame con los datos de la lista ####
df = pd.DataFrame(lista, columns=["Fecha", "Mes" ,"Fecha de busqueda", "Precio","Moneda","Es directo",
"Aerolinea", "Origen", "Destino", "Ciudad origen", "País origen","Ciudad de destino"])
def diaDelMes(fecha):
fec = fecha.split("-")
return fec[2]
df["Dia"] = df["Fecha"].map(diaDelMes)
df.to_csv (f"{today}.csv",sep=';', index=False, header=True)
subirArchivo(f"{today}.csv")
dia = datetime.datetime.now() - datetime.timedelta(days=7)
if dia.today().weekday() == 5:
try:
df2 = pd.read_csv(f'2021-{dia.month:02d}-{dia.day:02d}.csv', delimiter=';')
provincias=["Valencia", "Alicante", "Tenerife"]
mercados=["Reino Unido", "Alemania", "Francia", "Bélgica","Países Bajos"]
i=0
print("Enviando emails")
mails = emails()
for prov in provincias:
info = newsletter(mercados,prov,df,df2)
enviarEmails(mails[i],info,prov)
i+=1
print("Programa finalizado con exito")
except:
enviarEmails(["EMAIL PARA AVISAR DEL ERROR"],"SE HA PRODUCIDO UN ERROR","ERROR")
print("Error con los emails")
time.sleep(86400.0 - ((time.time() - starttime) % 86400.0))