-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
219 lines (189 loc) · 8.72 KB
/
main.py
File metadata and controls
219 lines (189 loc) · 8.72 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
from datetime import datetime, timedelta
from logging import getLevelName, getLogger
from os import getenv
from src.config import RunConfig
from src.db.clean import clean_old_timeseries
from src.db.db import MySQL, PostgreSQL, Sqlite
from src.logs.log import log_setup
from src.notifications.telegram import Telegram
from src.oportunities.roundtrip import round_trip
from src.scrapers.iryo import IryoScraper, IryoScraperConfig
from src.scrapers.renfe import RenfeScraper, RenfeScraperConfig
from src.scrapers.ouigo import OuigoScraper, OuigoScraperConfig
def clean(runConfig: RunConfig):
historical_data_days = int(getenv("TRAVEL_HISTORICAL_DATA_DAYS", "30"))
clean_old_timeseries(runConfig, historical_data_days)
def run(runConfig: RunConfig):
init_time = datetime.now()
log.debug(f"loop started at {init_time.strftime('%H:%M:%S')}")
# Travel input parameters
travel_from = getenv("TRAVEL_FROM", "Madrid")
travel_to = getenv("TRAVEL_TO", "Zaragoza")
travel_days = int(getenv("TRAVEL_DAYS", "30"))
renfe_price_change_notification = to_bool(
getenv("TRAVEL_RENFE_PRICE_CHANGE_NOTIFICATION", "False"))
round_trip_enabled = to_bool(
getenv("ROUND_TRIP_ENABLED", "True"))
round_trip_notification_max_price = float(
getenv("ROUND_TRIP_NOTIFICATION_MAX_PRICE", "40"))
round_trip_origin_departure_times = getenv(
"ROUND_TRIP_ORIGIN_DEPARTURE_TIME", "06:30,07:05")
round_trip_destination_departure_times = getenv(
"ROUND_TRIP_DESTINATION_DEPARTURE_TIME", "15:45,17:45,18:26,20:45")
travel_start_date = getenv("TRAVEL_START_DATE", None)
if travel_start_date:
start_date = datetime.strptime(travel_start_date, "%d/%m/%Y")
if start_date < datetime.now():
start_date = datetime.now()
else:
start_date = datetime.now()
renfe = RenfeScraper()
iryo = IryoScraper()
ouigo = OuigoScraper()
processed = 0
while processed < travel_days:
inner_init_time = datetime.now()
log.debug(
f"inner loop started at {inner_init_time.strftime('%H:%M:%S')}")
currentDateFormatted = start_date.strftime("%d/%m/%Y")
log.info(f"processing {currentDateFormatted}")
# Trains From Origin -> To Destination
origin_station = travel_from
destination_station = travel_to
# Renfe
renfeScrapeConfig = RenfeScraperConfig(
runConfig, currentDateFormatted, origin_station, destination_station, renfe_price_change_notification)
try:
result = renfe.scrape(renfeScrapeConfig)
except Exception as e:
log.error(f"Error scraping {currentDateFormatted} from {origin_station} to {destination_station}")
log.error(e)
exit(1)
renfe.save(renfeScrapeConfig, result)
# IRYO
iryoScraperConfig = IryoScraperConfig(
runConfig, currentDateFormatted, origin_station, destination_station, renfe_price_change_notification)
try:
result = iryo.scrape(iryoScraperConfig)
except Exception as e:
log.error(f"Error scraping {currentDateFormatted} from {origin_station} to {destination_station}")
log.error(e)
exit(1)
iryo.save(iryoScraperConfig, result)
# Ouigo
ouigoScraperConfig = OuigoScraperConfig(
runConfig, currentDateFormatted, origin_station, destination_station, renfe_price_change_notification)
try:
result = ouigo.scrape(ouigoScraperConfig)
except Exception as e:
log.error(f"Error scraping {currentDateFormatted} from {origin_station} to {destination_station}")
log.error(e)
exit(1)
ouigo.save(ouigoScraperConfig, result)
if round_trip_enabled:
# Return: Trains From Destination -> To Origin
origin_station = travel_to
destination_station = travel_from
# Renfe
renfeScrapeConfig = RenfeScraperConfig(
runConfig, currentDateFormatted, origin_station, destination_station, renfe_price_change_notification)
try:
result = renfe.scrape(renfeScrapeConfig)
except Exception as e:
log.error(f"Error scraping {currentDateFormatted} from {origin_station} to {destination_station}")
log.error(e)
exit(1)
renfe.save(renfeScrapeConfig, result)
# IRYO
iryoScraperConfig = IryoScraperConfig(
runConfig, currentDateFormatted, origin_station, destination_station, renfe_price_change_notification)
try:
result = iryo.scrape(iryoScraperConfig)
except Exception as e:
log.error(f"Error scraping {currentDateFormatted} from {origin_station} to {destination_station}")
log.error(e)
exit(1)
iryo.save(iryoScraperConfig, result)
# Ouigo
ouigoScraperConfig = OuigoScraperConfig(
runConfig, currentDateFormatted, origin_station, destination_station, renfe_price_change_notification)
try:
result = ouigo.scrape(ouigoScraperConfig)
except Exception as e:
log.error(f"Error scraping {currentDateFormatted} from {origin_station} to {destination_station}")
log.error(e)
exit(1)
ouigo.save(ouigoScraperConfig, result)
# Check Round Trips oportunities
for round_trip_origin_departure_time in round_trip_origin_departure_times.split(","):
for round_trip_destination_departure_time in round_trip_destination_departure_times.split(","):
round_trip(runConfig, start_date, travel_from, round_trip_origin_departure_time, travel_to,
round_trip_destination_departure_time, round_trip_notification_max_price)
processed += 1
start_date += timedelta(days=1)
log.info(f"{processed}/{travel_days} days processed")
inner_end_time = datetime.now()
log.debug(
f"inner loop finished at {inner_end_time.strftime('%H:%M:%S')}")
log.info(
f"inner loop toke: {abs((inner_end_time-inner_init_time).seconds)} seconds")
end_time = datetime.now()
log.debug(f"loop finished at {end_time.strftime('%H:%M:%S')}")
log.info(f"loop toke: {abs((end_time-init_time).seconds)/60} minutes")
def to_bool(value):
"""
Converts 'something' to boolean. Raises exception for invalid formats
Possible True values: 1, True, "1", "TRue", "yes", "y", "t"
Possible False values: 0, False, None, [], {}, "", "0", "faLse", "no", "n", "f", 0.0, ...
"""
if str(value).lower() in ("yes", "y", "true", "t", "1"):
return True
if str(value).lower() in ("no", "n", "false", "f", "0", "0.0", "", "none", "[]", "{}"):
return False
raise Exception(f"Invalid value for boolean conversion: {str(value)}")
if __name__ == "__main__":
# Init log
log_level_cfg = getenv("TRAVEL_LOG_LEVEL", "info")
log = log_setup(getLogger(__file__),
getLevelName(log_level_cfg.upper()))
# Init DB
db = None
db_file_path = getenv(
"TRAVEL_DB_PATH")
if db_file_path:
log.info(f"db mode is: sqlite. Path: {db_file_path}")
db = Sqlite(db_file_path)
else:
db_engine = getenv("TRAVEL_DB_ENGINE")
if not db_engine:
raise Exception("db engine is not specified")
# Check db engine is mysql or postgres
if db_engine not in ["mysql", "postgres"]:
raise Exception(f"db engine is not supported: {db_engine}")
db_host = getenv("TRAVEL_DB_HOST")
db_port = getenv("TRAVEL_DB_PORT")
db_user = getenv("TRAVEL_DB_USER")
db_pass = getenv("TRAVEL_DB_PASSWORD")
db_name = getenv("TRAVEL_DB_NAME")
if db_engine == "mysql":
log.info(f"db mode is: mysql/mariadb")
db = MySQL(db_user, db_pass, db_host, db_port, db_name)
elif db_engine == "postgres":
log.info(f"db mode is: postgres")
db = PostgreSQL(db_user, db_pass, db_host, db_port, db_name)
# Init Telegram Notification service
notify_token = getenv("TRAVEL_NOTIFICATION_TOKEN")
notify_chat_id = int(getenv("TRAVEL_NOTIFICATION_CHAT_ID"))
notification = Telegram(notify_token, notify_chat_id)
# Create configuration struct
runConfig = RunConfig(log, db, notification)
# Execute once
run(runConfig)
clean(runConfig)
debug = getenv("TRAVEL_DEBUG", "no")
log.info(f"debug mode is: {debug}")
if debug == "no":
# Execute forever if no debug mode
while True:
run(runConfig)
clean(runConfig)