From b323ab3344f656471d319a0c06dd531bbaf687ae Mon Sep 17 00:00:00 2001 From: Yaroslav Lisovskyi Date: Sat, 23 Mar 2024 00:16:40 +0200 Subject: [PATCH] refactored parameters, added new city --- dimria/dimria_requests.py | 44 ++++++++++++++++++++++++++-------- dimria/models/AdvertDetails.py | 12 ++++++++++ dimria/requests_handle.py | 4 ++++ function_app.py | 15 +++++++----- 4 files changed, 59 insertions(+), 16 deletions(-) diff --git a/dimria/dimria_requests.py b/dimria/dimria_requests.py index 1f26333..837ce93 100644 --- a/dimria/dimria_requests.py +++ b/dimria/dimria_requests.py @@ -8,20 +8,35 @@ API_KEY = os.getenv("DIMRIA_API_KEY") -state_id = 2 -city_ids = 2 -n_id = 6696 +search_items = [ + { + 'state_id': 2, + 'city_id': 2, + 'n_id': 6696 + }, + { + 'state_id': 10, + 'city_id': 17306, + 'n_id': None + } +] -SEARCH_URL = f"https://developers.ria.com/dom/search?category=1&realty_type=0&operation_type=1&state_id={state_id}&city_ids={city_ids}&api_key={API_KEY}&n_id={n_id}" +def search_adverts() -> SearchResponse: + count = 0 + items = [] -def search_adverts() -> SearchResponse: - response = requests.get(SEARCH_URL) - advertDetails = response.json() + for item in search_items: + + url = _get_url(item.get('state_id'), item.get('city_id'), item.get('n_id')) + + response = requests.get(url) + + advertDetails = response.json() - count = advertDetails["count"] - items = advertDetails["items"] + count += advertDetails["count"] + items.extend(advertDetails["items"]) searchResponse = SearchResponse( count=count, @@ -32,7 +47,7 @@ def search_adverts() -> SearchResponse: logging.error("No adverts found") return None - if searchResponse.count == 0: + if searchResponse.count == 0: logging.error("No adverts found") return None @@ -51,3 +66,12 @@ def get_advert_details(advertId: int) -> AdvertDetails: return None return details + + + +def _get_url(state_id, city_id, n_id = None): + + if n_id is None: + return f"https://developers.ria.com/dom/search?category=1&realty_type=0&operation_type=1&state_id={state_id}&city_ids={city_id}&api_key={API_KEY}" + + return f"https://developers.ria.com/dom/search?category=1&realty_type=0&operation_type=1&state_id={state_id}&city_ids={city_id}&api_key={API_KEY}&n_id={n_id}" diff --git a/dimria/models/AdvertDetails.py b/dimria/models/AdvertDetails.py index a537707..77ff017 100644 --- a/dimria/models/AdvertDetails.py +++ b/dimria/models/AdvertDetails.py @@ -12,3 +12,15 @@ class AdvertDetails(BaseModel): currency_type_uk: str """$""" +def parse_details_from_json(json_details, advert_id = -1) -> AdvertDetails: + return AdvertDetails( + advert_id=json_details['advert_id'] if advert_id == -1 else advert_id, + city_name=json_details['city_name'] if 'city_name' in json_details else 'Unknown', + state_id=json_details['state_id'] if 'state_id' in json_details else 0, + city_id=json_details['city_id'] if 'city_id' in json_details else 0, + currency_type_id=json_details['currency_type_id'] if 'currency_type_id' in json_details else 0, + price=json_details['price'] if 'price' in json_details else 0, + rooms_count=json_details['rooms_count'] if 'rooms_count' in json_details else 0, + currency_type_uk=json_details['currency_type_uk'] if 'currency_type_uk' in json_details else '$' + ) + diff --git a/dimria/requests_handle.py b/dimria/requests_handle.py index 9f032f1..77644fa 100644 --- a/dimria/requests_handle.py +++ b/dimria/requests_handle.py @@ -54,6 +54,10 @@ def get_http_advert_details(advert_id: int): details_url = f"https://developers.ria.com/dom/info/{advert_id}?api_key={API_KEY}" response = requests.get(details_url) + + if response.status_code != 200: + return None + json = response.json() if not json: diff --git a/function_app.py b/function_app.py index 0637836..f8205aa 100644 --- a/function_app.py +++ b/function_app.py @@ -8,7 +8,7 @@ from dimria.dimria_requests import search_adverts, get_advert_details from dimria.handle_details import build_ptoho_url, parse_photos, build_main_advert_url -from dimria.models.AdvertDetails import AdvertDetails +from dimria.models.AdvertDetails import AdvertDetails, parse_details_from_json from dimria.models.AdvertsList import AdvertsList from dimria.models.AdvertDetailsResponse import AdvertDetailsResponse, AdvertDetailsResponseEncoder from dimria.models.AdvertDtoEncoder import AdvertDtoEncoder @@ -21,7 +21,7 @@ # @app.route(route="search_adverts", auth_level=func.AuthLevel.ANONYMOUS) @app.schedule(schedule="0 */30 * * * *", arg_name="mytimer", run_on_startup=True, use_monitor=False) -def timer_search_adverts(mytimer: func.TimerRequest) -> None: # req: func.HttpRequest func.HttpResponse: # # mytimer: func.TimerRequest +def timer_search_adverts(mytimer: func.TimerRequest) -> None: # req: func.HttpRequest func.HttpResponse: # mytimer: func.TimerRequest -> None: searchResponse = search_adverts() if not searchResponse: @@ -33,6 +33,8 @@ def timer_search_adverts(mytimer: func.TimerRequest) -> None: # req: func.HttpRe advertList = AdvertsList(items=searchResponse.items) send_advert_list_message(advertList) + # return func.HttpResponse(f"Found {searchResponse.count} adverts", status_code=200) + ################################################################################################ @app.function_name("adverts_list_message_handler") @@ -54,9 +56,10 @@ def adverts_list_message_handler(msg: func.ServiceBusMessage): for advertId in advertList.items: logging.info(f"Get details for advert {advertId}") - details = get_advert_details(advertId) + details = get_http_advert_details(advertId) if details is not None: - logging.info(f"City: {details.city_id}: Rooms: {details.rooms_count}: Price: {details.currency_type_uk}{details.price}") + details = parse_details_from_json(details, advertId) + # logging.info(f"City: {details["city_id"]}: Rooms: {details["rooms_count"]}: Price: {details["currency_type_uk"]}{details["price"]}") send_advert_detail_message(details) else: logging.error(f"Error getting details for advert {advertId}") @@ -73,7 +76,7 @@ def advert_details_save_db(msg: func.ServiceBusMessage): try: str_data = msg.get_body().decode('utf-8') json_data = json.loads(str_data) - advert = parse_obj_as(AdvertDetails, json_data) + advert = parse_details_from_json(json_data) process_advert(advert) except Exception as e: @@ -105,7 +108,7 @@ def get_advert_details(req: func.HttpRequest) -> func.HttpResponse: resultRequest = get_http_advert_details(advert_id) if(resultRequest is None): - return func.HttpResponse("No advert details found", status_code=404) + return func.HttpResponse(f"No advert details found for advert id - {advert_id}", status_code=404) print(resultRequest)