-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
139 lines (109 loc) · 4.06 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
import asyncio
from baraky.notifications import TelegramNotificationsBot
from baraky.estate_features import PIDCommuteFeatureEnhancer
import logging
from baraky.storages import (
EstatesStorage,
MinioStorage,
EstatesHitQueue,
ReactionsStorage,
)
from baraky.estate_watcher import EstateWatcher
from baraky.models import EstateOverview, PIDCommuteFeature
from baraky.client import SrealityEstatesClient
import argparse
import baraky.io as io
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
logger = logging.getLogger("baraky")
logger.setLevel(logging.DEBUG)
logging.getLogger("httpx").setLevel(logging.WARNING)
def main():
args = setup_args()
args.func(args)
def setup_args():
parser = argparse.ArgumentParser(description="Baraky")
subparsers = parser.add_subparsers(help="Commands")
parser_watcher = subparsers.add_parser("watcher", help="Watch for new estates")
parser_watcher.add_argument(
"--query-path",
type=str,
help="Path to query json file",
required=True,
)
parser_watcher.set_defaults(func=watcher_command)
parser_sync = subparsers.add_parser("sync", help="Watch for new estates ONCE")
parser_sync.add_argument(
"--query-path",
type=str,
help="Path to query json file",
required=True,
)
parser_sync.set_defaults(func=sync_command)
parser_notifier = subparsers.add_parser("notifier", help="Notify about new estates")
parser_notifier.set_defaults(func=notifier_command)
return parser.parse_args()
def watcher_command(args):
watcher = setup_watcher(args)
asyncio.run(watcher.watch())
def sync_command(args):
watcher = setup_watcher(args)
asyncio.run(watcher.update())
def notifier_command(args):
reactions_minio_storage = MinioStorage("reactions")
reactions_storage = ReactionsStorage("estate/", reactions_minio_storage)
hits_minio_storage = MinioStorage("hitqueue")
queue = EstatesHitQueue("filtered/", hits_minio_storage)
bot = TelegramNotificationsBot(
queue,
reactions_storage,
)
bot.start()
def _filter_house_close_to_prague(
estate_overview: EstateOverview, commute_time: PIDCommuteFeature
) -> bool:
if estate_overview.type != "house":
return False
if commute_time.time_minutes is None or commute_time.transfers_count is None:
return False
is_close = commute_time.time_minutes <= 75
max_1_transfer = commute_time.transfers_count <= 4
return max_1_transfer and is_close and estate_overview.price <= 8_000_000
def _filter_flat_close_to_prague(
estate_overview: EstateOverview, commute_time: PIDCommuteFeature
) -> bool:
if estate_overview.type != "flat":
return False
if commute_time.time_minutes is None or commute_time.transfers_count is None:
return False
is_close = commute_time.time_minutes <= 55
max_transfer = commute_time.transfers_count <= 4
return max_transfer and is_close and estate_overview.price <= 6_500_000
def filter_fn(estate_overview: EstateOverview) -> bool:
commute_time = estate_overview.features.get("pid_commute_time")
if commute_time is None:
return False
is_close_house = _filter_house_close_to_prague(estate_overview, commute_time)
is_close_flat = _filter_flat_close_to_prague(estate_overview, commute_time)
return is_close_house or is_close_flat
def setup_watcher(args):
query_params = io.read_json_sync(args.query_path)
client = SrealityEstatesClient(query_params)
estates_minio_storage = MinioStorage("estates")
storage = EstatesStorage("estate/", estates_minio_storage)
hits_minio_storage = MinioStorage("hitqueue")
queue = EstatesHitQueue("filtered/", hits_minio_storage)
feature_calculators = {
"pid_commute_time": PIDCommuteFeatureEnhancer(),
}
return EstateWatcher(
client=client,
storage=storage,
output_queue=queue,
feature_calculators=feature_calculators,
filter_fn=filter_fn,
)
if __name__ == "__main__":
main()