Fixed testing

This commit is contained in:
Анатолий Богомолов 2024-01-05 16:08:19 +10:00
parent fe16d4eee9
commit 694371f46f
2 changed files with 11 additions and 5 deletions

View File

@ -1,4 +1,6 @@
from dataclasses import dataclass from dataclasses import dataclass
from typing import Literal
from crawler.utils.classes import DataclassBase from crawler.utils.classes import DataclassBase
@dataclass(init=False) @dataclass(init=False)
@ -7,7 +9,7 @@ class City(DataclassBase):
city: str city: str
region_id: int | None region_id: int | None
region_name: str | None region_name: str | None
is_byapt: 1 | 0 is_byapt: Literal[1, 0]
@dataclass(init=False) @dataclass(init=False)
class Proxy(DataclassBase): class Proxy(DataclassBase):

12
main.py
View File

@ -17,6 +17,7 @@ async def get_crawler_data():
crawler = CrawlerAPI() crawler = CrawlerAPI()
cities = await crawler.get_cities() cities = await crawler.get_cities()
proxies = await crawler.get_proxies() proxies = await crawler.get_proxies()
await crawler.close()
return cities, proxies return cities, proxies
def filter_cities(config, cities: list[City]) -> list[City]: def filter_cities(config, cities: list[City]) -> list[City]:
@ -29,16 +30,19 @@ def filter_cities(config, cities: list[City]) -> list[City]:
return cities return cities
def parse_city(city: City):
CityParser(city, proxies).parse()
@logger.catch @logger.catch
def main(cities: list[City], proxies: list[Proxy]): def main(cities: list[City]):
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read("config.ini") config.read("config.ini")
cities = filter_cities(config, cities) cities = filter_cities(config, cities)
quantity = config['parser']['cities_quantity'] or len(cities) quantity = config['parser']['cities_quantity'] or len(cities)
with multiprocessing.Pool(processes=quantity) as pool: #type: ignore with multiprocessing.Pool(processes=int(quantity)) as pool: #type: ignore
results = pool.map(lambda city: CityParser(city, proxies).parse(), cities) results = pool.map(parse_city, cities)
with open("results.json", "w") as f: with open("results.json", "w") as f:
json.dump(results, f, cls=DataclassJSONEncoder, ensure_ascii=False, indent=4) json.dump(results, f, cls=DataclassJSONEncoder, ensure_ascii=False, indent=4)
@ -47,4 +51,4 @@ if __name__ == "__main__":
logger.add(sys.stderr, level="DEBUG", backtrace=True, enqueue=True) #type: ignore logger.add(sys.stderr, level="DEBUG", backtrace=True, enqueue=True) #type: ignore
cities, proxies = asyncio.run(get_crawler_data()) cities, proxies = asyncio.run(get_crawler_data())
main(cities, proxies) main(cities)