From df25f02e6957ddebd44a1836878ae13e9058bb8e Mon Sep 17 00:00:00 2001 From: Yaroslav Lisovskyi Date: Fri, 15 Mar 2024 00:31:14 +0200 Subject: [PATCH] updated details fro advert --- dimria/dimria_requests.py | 2 +- dimria/handle_details.py | 15 +++++++++++++++ dimria/models/AdvertDetailsResponse.py | 9 ++++++++- function_app.py | 19 ++++++++++++++++--- 4 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 dimria/handle_details.py diff --git a/dimria/dimria_requests.py b/dimria/dimria_requests.py index c76c6f4..1f26333 100644 --- a/dimria/dimria_requests.py +++ b/dimria/dimria_requests.py @@ -3,7 +3,7 @@ import logging from pydantic.tools import parse_obj_as -from dimria.models.SearchResponse import SearchResponse +from dimria.models.searchresponse import SearchResponse from dimria.models.AdvertDetails import AdvertDetails API_KEY = os.getenv("DIMRIA_API_KEY") diff --git a/dimria/handle_details.py b/dimria/handle_details.py new file mode 100644 index 0000000..453db9c --- /dev/null +++ b/dimria/handle_details.py @@ -0,0 +1,15 @@ + +def build_ptoho_url(file): + split = file.split(".") + file = split[0] + "xl." + split[1] + return f'https://cdn.riastatic.com/photos/{file}' + +def parse_photos(photos): + result = [] + for photoId in photos: + photo = photos[photoId] + result.append(build_ptoho_url(photo["file"])) + return result + +def build_main_advert_url(url): + return f'https://dom.ria.com/uk/{url}' diff --git a/dimria/models/AdvertDetailsResponse.py b/dimria/models/AdvertDetailsResponse.py index 70b8fd6..6e5bcef 100644 --- a/dimria/models/AdvertDetailsResponse.py +++ b/dimria/models/AdvertDetailsResponse.py @@ -3,7 +3,7 @@ class AdvertDetailsResponse: - def __init__(self, advert_id, city_name, description, price, currency,floor, rooms_count) -> None: + def __init__(self, advert_id, city_name, description, price, currency,floor, rooms_count, main_photo, lat, lon, building_name, url, photos = []) -> None: self.advert_id = advert_id self.city_name = city_name self.description = description @@ -11,6 +11,13 @@ def __init__(self, advert_id, city_name, description, price, currency,floor, ro self.currency = currency self.floor = floor self.rooms_count = rooms_count + self.main_photo = main_photo + self.lat = lat + self.lon = lon + self.building_name = building_name + self.url = url + self.photos = photos, + class AdvertDetailsResponseEncoder (JSONEncoder): diff --git a/function_app.py b/function_app.py index 3980bc0..0c641ab 100644 --- a/function_app.py +++ b/function_app.py @@ -7,6 +7,7 @@ from dimria.cosmos_db import process_advert 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.AdvertsList import AdvertsList from dimria.models.AdvertDetailsResponse import AdvertDetailsResponse, AdvertDetailsResponseEncoder @@ -18,7 +19,7 @@ ################################################################################################ -#@app.route(route="search_adverts", auth_level=func.AuthLevel.ANONYMOUS) +# @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 @@ -116,7 +117,13 @@ def get_advert_details(req: func.HttpRequest) -> func.HttpResponse: rooms_count = resultRequest['rooms_count'] currency_type_uk = resultRequest['currency_type_uk'] description = resultRequest['description_uk'] - floor=resultRequest['floor_info'] + floor=resultRequest['floor'] + photos = parse_photos(resultRequest['photos']) + main_photo = build_ptoho_url(resultRequest['main_photo']) + lat = resultRequest['latitude'] + lon = resultRequest['longitude'] + building_name = resultRequest['user_newbuild_name'] + url = build_main_advert_url(resultRequest['beautiful_url']) resultResponse: AdvertDetailsResponse = AdvertDetailsResponse( advert_id=advert_id, @@ -125,7 +132,13 @@ def get_advert_details(req: func.HttpRequest) -> func.HttpResponse: rooms_count=rooms_count, currency=currency_type_uk, description=description, - floor=floor + floor=floor, + main_photo=main_photo, + lat = lat, + lon = lon, + building_name = building_name, + url = url, + photos=photos ) data = json.dumps(resultResponse, cls=AdvertDetailsResponseEncoder)