-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
194 lines (167 loc) · 5.42 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
186
187
188
189
190
191
192
193
194
import argparse
import os
import config
import helpers
import tempfile
import logging
import database as db
from typing import Dict
from engine import kiwi
from telegram import bot
from datetime import datetime, timedelta
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
r"--print",
dest="print",
action="store_true",
help="print output to the terminal (not in AWS)",
)
parser.add_argument(
r"--publish",
dest="publish",
action="store_true",
help="publish report to telegram",
)
parser.add_argument(
r"--dest",
dest="dest",
help="destination, can be 2 letter countries,"
' or 3 letter airports. seperated with ","',
)
parser.add_argument(r"--just-special", default=False, action="store_true")
parser.add_argument(
r"--from-date", dest="from_date", help="scan start date (DD-MM-YY)"
)
parser.add_argument(r"--to-date", dest="to_date", help="scan end date (DD-MM-YY)")
parser.add_argument(
r"--price", dest="price", help="max ticket price (nis)", default=500
)
parser.add_argument(r"--airline", dest="airline", help="airline name", default=None)
parser.add_argument(
r"--chat-id", dest="chat_id", help="telegram chat id to send the result to"
)
parser.add_argument(r"--bot", action="store_true")
parser.add_argument(
r"--config_from_file", help="load config from provided file", default=""
)
parser.add_argument(
r"--aws_config",
help="load config from aws s3",
default=False,
action="store_true",
)
parser.add_argument(
r"--aws_test_config",
help="use config_test.json from s3",
default=False,
action="store_true",
)
args = parser.parse_args()
return args
def create_env():
try:
os.mkdir(os.path.join(config.tmp_folder, "reports"))
except Exception as e:
logging.error(e)
def handle_destination(
fly_to, date_from, date_to, max_price, chat_id, single_dest=False, details=None
):
scan_timestamp = int(datetime.timestamp(datetime.now()))
kiwi.generate_weekend_flights(
date_from,
date_to,
fly_to=fly_to,
price_to=max_price,
scan_timestamp=scan_timestamp,
details=details,
)
kiwi.generate_holidays_flights(
date_from,
date_to,
fly_to=fly_to,
price_to=max_price,
scan_timestamp=scan_timestamp,
)
if single_dest:
report = bot.publish_default_report(
chat_id, date_from, date_to, scan_timestamp, one_per_city=False
)
else:
report = bot.publish_default_report(chat_id, date_from, date_to, scan_timestamp)
if config.print:
print(report)
def scan_monthly_flights(date_from, date_to, args):
# iterate over the default configuration
for chat_name, details in config.TELEGRAM_BOTS["default"]["chats"].items():
chat_id, destinations, max_price, _, _, _, _ = details
max_price = max_price if max_price > int(args.price) else int(args.price)
fly_to = (
",".join(config.SPECIAL_DESTINATIONS)
if destinations == "all"
else ",".join(destinations)
)
handle_destination(
fly_to,
date_from,
date_to,
max_price,
chat_id,
single_dest=(chat_name != "all"),
details=details,
)
def scan_special_dates():
scan_timestamp = int(datetime.timestamp(datetime.now()))
for special_date in config.SPECIAL_DATES["dates"]:
kiwi.generate_special_date(special_date, scan_timestamp)
if config.publish:
bot.publish_special_date_report(
config.SPECIAL_DATES["dates"],
config.SPECIAL_DATES["chat_id"],
scan_timestamp,
)
def main():
args = parse_args()
# load config from env variables
config.init_config()
if path := args.config_from_file:
config.load_config_from_file(path)
if args.aws_config:
with tempfile.NamedTemporaryFile(mode="wb") as t:
if args.aws_test_config:
config_content = helpers.download_config_from_s3(test_config=True)
else:
config_content = helpers.download_config_from_s3()
t.write(config_content)
t.flush()
config.load_config_from_file(t.file.name)
config.publish = args.publish
config.print = args.print
create_env()
if args.from_date and args.to_date:
date_from = datetime.strptime(args.from_date, "%d-%m-%y")
date_to = datetime.strptime(args.to_date, "%d-%m-%y")
else:
date_from = datetime.now()
date_to = date_from + timedelta(days=config.MONTHS_TO_SCAN * 30)
if args.dest:
fly_to = args.dest.upper()
chat_id = (
args.chat_id
if args.chat_id
else config.TELEGRAM_BOTS["default"]["chats"]["all"][0]
)
max_price = (
int(args.price)
if args.price
else config.TELEGRAM_BOTS["default"]["chats"]["all"][2]
)
handle_destination(
fly_to, date_from, date_to, max_price, chat_id, single_dest=True
)
else:
if not args.just_special:
scan_monthly_flights(date_from, date_to, args)
scan_special_dates()
if __name__ == "__main__":
main()