Skip to content

Commit

Permalink
Merge pull request #1 from Yariki/f-AddNewPlaceForSearching
Browse files Browse the repository at this point in the history
refactored parameters, added new city
  • Loading branch information
Yariki authored Mar 22, 2024
2 parents ef9cabb + b323ab3 commit 896d3af
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 16 deletions.
44 changes: 34 additions & 10 deletions dimria/dimria_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand All @@ -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}"
12 changes: 12 additions & 0 deletions dimria/models/AdvertDetails.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '$'
)

4 changes: 4 additions & 0 deletions dimria/requests_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
15 changes: 9 additions & 6 deletions function_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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")
Expand All @@ -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}")
Expand All @@ -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:
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 896d3af

Please sign in to comment.