-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimizers.py
155 lines (121 loc) · 4 KB
/
optimizers.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
import logging
import pandas as pd
from typing import List
from pathlib import Path
from datetime import date
from ryanair.ryanair import Ryanair
from ryanair.utils.config import parse_toml, parse_proxies
from http.server import SimpleHTTPRequestHandler, HTTPServer
logger = logging.getLogger("ryanair")
class Handler(SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=kwargs.pop('directory'), **kwargs)
def log_message(self, format, *args):
pass
def make_clickable(val):
return f'<a href="{val}">link</a>'
def run_server(port: int, dir: Path = Path(".")):
server_address = ('', port)
handler = lambda *args, **kwargs: Handler(directory=dir, *args, **kwargs)
httpd = HTTPServer(server_address, handler)
httpd.serve_forever()
def optimizer_1w(
origin: str,
from_date: date,
to_date: date,
dests: List[str],
config_path: Path,
proxy_path: Path,
use_usd: bool,
no_proxy: bool
) -> pd.DataFrame:
logger.info(f"Using config path: {config_path.absolute()}")
logger.info(f"Using proxies path: {proxy_path.absolute()}")
config = parse_toml(config_path)
ryanair = Ryanair(
rid=config['cookies']['rid'],
ridsig=config["cookies"]["rid.sig"],
origin=origin,
USD=use_usd
)
if no_proxy:
proxies = ({},)
else:
proxies = parse_proxies(proxy_path)
ryanair.sm.extend_proxies_pool(proxies)
ryanair.sm.pool_size = config['network']['pool_size']
ryanair.sm.timeout = config['network']['timeout']
fares = ryanair.search_one_way_fares(
from_date=from_date,
to_date=to_date,
destinations=dests
)
df = pd.DataFrame(fares)
if not df.empty:
df = df.sort_values(
by="outbound_fare", ascending=True
).reset_index(drop=True)
df['link'] = df.apply(
lambda row: ryanair.get_one_way_link(
from_date=row['outbound_dep_time'].date(),
destination=row['destination']
),
axis=1
)
df['link'] = df['link'].apply(make_clickable)
return df
def optimizer_rt(
origin: str,
from_date: date,
to_date: date,
min_nights: int,
max_nights: int,
dests: List[str],
config_path: Path,
proxy_path: Path,
use_usd: bool,
no_proxy: bool
) -> pd.DataFrame:
logger.info(f"Using config path: {config_path.absolute()}")
logger.info(f"Using proxies path: {proxy_path.absolute()}")
config = parse_toml(config_path)
ryanair = Ryanair(
rid=config['cookies']['rid'],
ridsig=config["cookies"]["rid.sig"],
origin=origin,
USD=use_usd
)
if no_proxy:
proxies = ({},)
else:
proxies = parse_proxies(proxy_path)
ryanair.sm.extend_proxies_pool(proxies)
ryanair.sm.pool_size = config['network']['pool_size']
ryanair.sm.timeout = config['network']['timeout']
fares = ryanair.search_round_trip_fares(
min_nights=min_nights,
max_nights=max_nights,
from_date=from_date,
to_date=to_date,
destinations=dests
)
df = pd.DataFrame(fares)
if not df.empty:
df['round_trip_fare'] = (df["outbound_fare"] + df["return_fare"]).round(
decimals=2
)
df = df.sort_values(
by="round_trip_fare", ascending=True
).reset_index(drop=True)
columns = df.columns.to_list()
df = df[columns[:-2] + [df.columns[-1], df.columns[-2]]]
df['link'] = df.apply(
lambda row: ryanair.get_round_trip_link(
from_date=row['outbound_dep_time'].date(),
to_date=row['return_dep_time'].date(),
destination=row['destination']
),
axis=1
)
df['link'] = df['link'].apply(make_clickable)
return df