-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
276 lines (221 loc) · 7.78 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
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
270
271
272
273
274
275
276
from __future__ import annotations
import asyncio
import json
import logging
import os
from dataclasses import dataclass
import aiohttp
MAX_RETRIES = 20
logging.basicConfig(level=logging.DEBUG if os.environ.get("DEBUG") else logging.INFO)
class RemoteEntity:
def __init__(self, session) -> None:
self.session = session
async def fetch(self, url) -> list[dict]:
try_no = 0
while try_no < MAX_RETRIES:
try:
logging.debug("Sending request to %s (try: %s)", url, try_no)
async with self.session.get(url, raise_for_status=True) as response:
logging.debug("%s finished with status %s", url, response.status)
return await response.json()
except (aiohttp.ClientResponseError, aiohttp.ClientConnectorError, aiohttp.ServerDisconnectedError) as e:
if (
isinstance(e, aiohttp.ClientConnectorError)
or isinstance(e, aiohttp.ServerDisconnectedError)
or e.status == 429
or e.status == 500
):
logging.info(
"Sleeping before retry for %s (try: %s)...", url, try_no
)
await asyncio.sleep(0.1 * 2**try_no)
elif isinstance(e, aiohttp.ClientResponseError):
logging.error("%s failed with code %s", url, e.status)
raise e
else:
logging.error("%s failed with generic error %s", url, e)
raise e
try_no += 1
raise RuntimeError(f"Max retries reached for {url}: {try_no}")
@dataclass
class School(RemoteEntity):
id: int
name: str
neighborhood_id: int
def __init__(self, session, id, name, city_id, district_id, neighborhood_id):
super().__init__(session)
self.id = id
self.name = name
self.city_id = city_id
self.district_id = district_id
self.neighborhood_id = neighborhood_id
def to_dict(self):
return {
"id": self.id,
"name": self.name,
"city_id": self.city_id,
"district_id": self.district_id,
"neighborhood_id": self.neighborhood_id,
}
def __str__(self):
return f"{self.id} - {self.name} - {self.city_id} - {self.district_id} - {self.neighborhood_id}"
@dataclass
class Neighborhood(RemoteEntity):
id: int
name: str
city_id: int
district_id: int
schools: list[School]
@property
def schools_url(self):
return f"https://api-sonuc.oyveotesi.org/api/v1/cities/{self.city_id}/districts/{self.district_id}/neighborhoods/{self.id}/schools"
def __init__(self, session, id, name, city_id, district_id):
super().__init__(session)
self.id = id
self.name = name
self.city_id = city_id
self.district_id = district_id
self.schools = []
async def download(self) -> Neighborhood:
self.schools = [
School(
self.session,
id=school["id"],
name=school["name"],
city_id=self.city_id,
district_id=self.district_id,
neighborhood_id=self.id,
)
for school in await self.fetch(self.schools_url)
]
return self
def to_dict(self):
return {
"id": self.id,
"name": self.name,
"city_id": self.city_id,
"district_id": self.district_id,
"schools": [school.to_dict() for school in self.schools],
}
def __str__(self):
return f"{self.id} - {self.name}"
@dataclass
class District(RemoteEntity):
id: int
name: str
city_id: int
neighborhoods: list[Neighborhood]
@property
def neighborhoods_url(self):
return f"https://api-sonuc.oyveotesi.org/api/v1/cities/{self.city_id}/districts/{self.id}/neighborhoods"
def __init__(self, session, id, name, city_id):
super().__init__(session)
self.id = id
self.name = name
self.city_id = city_id
self.neighborhoods = []
async def download(self) -> District:
self.neighborhoods = await asyncio.gather(
*(
Neighborhood(
self.session,
id=neighborhood["id"],
name=neighborhood["name"],
city_id=self.city_id,
district_id=self.id,
).download()
for neighborhood in await self.fetch(self.neighborhoods_url)
)
)
return self
def to_dict(self):
return {
"id": self.id,
"name": self.name,
"city_id": self.city_id,
"neighborhoods": [
neighborhood.to_dict() for neighborhood in self.neighborhoods
],
}
def __str__(self):
return f"{self.id} - {self.name} - {len(self.neighborhoods)}"
@dataclass
class City(RemoteEntity):
id: int
name: str
plate: int
districts: list[District]
@property
def districts_url(self):
return f"https://api-sonuc.oyveotesi.org/api/v1/cities/{self.id}/districts"
def __init__(self, session, id, name, plate):
super().__init__(session)
self.id = id
self.name = name
self.plate = plate
self.districts = []
async def download(self) -> City:
self.districts = await asyncio.gather(
*(
District(
self.session,
id=district["id"],
name=district["name"],
city_id=self.id,
).download()
for district in await self.fetch(self.districts_url)
)
)
return self
def to_dict(self):
return {
"id": self.id,
"name": self.name,
"plate": self.plate,
"districts": [district.to_dict() for district in self.districts],
}
def __str__(self):
return f"{self.id} - {self.name} - {self.plate} - {len(self.districts)}"
async def gather_all(session):
cities = await get_cities(session)
print_cities(cities)
print("Gathered all cities")
def print_cities(cities):
cities_dict = [city.to_dict() for city in cities]
with open("tree.json", "w", encoding="utf-8") as f:
json.dump(cities_dict, f, ensure_ascii=False, indent=4)
async def get_cities(session):
with open("cities.json", "r", encoding="utf-8") as f:
cities_json = json.load(f)
cities = await asyncio.gather(
*(
City(session, id=city["id"], name=city["name"], plate=plate).download()
for plate, city in cities_json.items()
)
)
return cities
async def main():
with open("cities.json", "r", encoding="utf-8") as f:
cities_json = json.load(f)
city_plate = input("Enter city plate: ")
if not city_plate.isdigit():
print("Error: city plate must be a number")
exit(1)
if city_plate not in cities_json:
print("Error: city not found")
exit(1)
city_data = cities_json[city_plate]
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0"
}
async with aiohttp.ClientSession(headers=headers) as session:
city = await City(
session, id=city_data["id"], name=city_data["name"], plate=int(city_plate)
).download()
filename = f"{city.name}.json"
with open(filename, "w", encoding="utf-8") as f:
json.dump(city.to_dict(), f, ensure_ascii=False, indent=4)
print(f"Wrote to {filename}")
print(f"Gathered city {city.name}")
if __name__ == "__main__":
asyncio.run(main())