Skip to content

Instantly share code, notes, and snippets.

@BharatKalluri
Created January 22, 2023 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BharatKalluri/4122158bfe6a1edbfaaa242df24a14c1 to your computer and use it in GitHub Desktop.
Save BharatKalluri/4122158bfe6a1edbfaaa242df24a14c1 to your computer and use it in GitHub Desktop.
Find all available dorms across zostels in a time range
import requests
# find these from avalibility get request for your session
TOKEN = "Bearer "
CLIENT_APP_ID = ""
CLIENT_USER_ID = ""
# input dates here
check_in = "2023-01-25"
check_out = "2023-01-29"
def get_zostel_proprties_list() -> list[dict]:
response_json = requests.get(
"https://api.zostel.com/api/v1/stay/operators/?fields=name,type_code,operating_model,latitude,longitude,code,slug,destination"
).json()
return response_json["operators"]
def get_operator_room_details(operator_id: str) -> list[dict]:
response_json = requests.get(
f"https://api.zostel.com/api/v1/stay/operators/{operator_id}/",
).json()
rooms: list[dict] = response_json.get("operator", {}).get("rooms")
return rooms
def get_availability(
check_in: str,
check_out: str,
property_code: str,
room_codes_interested_in: list[int],
):
response_json = requests.get(
f"https://api.zostel.com/api/v1/stay/availability/?checkin={check_in}&checkout={check_out}&property_code={property_code}",
headers={
"authorization": TOKEN,
"client-app-id": CLIENT_APP_ID,
"client-user-id": CLIENT_USER_ID,
},
).json()
availability_data = response_json.get("availability")
filtered = list(
filter(
lambda x: x.get("room_id") in room_codes_interested_in, availability_data
)
)
return filtered
def is_units_available(
selected_slug: str,
check_in: str,
check_out: str,
):
property_code = selected_slug.split("-")[-1].upper()
accomadation_types_data = get_operator_room_details(selected_slug)
room_ids_interested_in = [
el.get("id")
for el in accomadation_types_data
if "dorm" in el.get("sub_category") and "female" not in el.get("name").lower()
]
availability_data = get_availability(
check_in=check_in,
check_out=check_out,
property_code=property_code,
room_codes_interested_in=room_ids_interested_in,
)
units_to_book_arr = [el.get("units") for el in availability_data]
available_everyday = len(units_to_book_arr) > 0 and all(
[el > 0 for el in units_to_book_arr]
)
return available_everyday
if __name__ == "__main__":
zostel_list_raw = get_zostel_proprties_list()
zostel_list_slugs = [el.get("slug") for el in zostel_list_raw]
available_slugs = [
selected_slug
for selected_slug in zostel_list_slugs
if is_units_available(
selected_slug=selected_slug,
check_in=check_in,
check_out=check_out,
)
]
print("checked between ", check_in, check_out)
print("available ", available_slugs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment