TRIPEO commited on
Commit
206fbad
·
verified ·
1 Parent(s): ebf7cc2

Activity connector V1

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import hashlib
3
+ import datetime
4
+ import streamlit as st
5
+ import pandas as pd
6
+
7
+ # Hotelbeds API credentials
8
+ hotelbeds_api_key = "95ce3e45a02fc6fd9720ecc013a6f674"
9
+ hotelbeds_api_secret = "785150201f"
10
+
11
+ # Function to generate the API signature
12
+ def generate_signature():
13
+ local_timestamp = int(datetime.datetime.now().timestamp())
14
+ assemble = hotelbeds_api_key + hotelbeds_api_secret + str(local_timestamp)
15
+ signature = hashlib.sha256(assemble.encode()).hexdigest()
16
+ return signature
17
+
18
+ # Function to make the POST request to the Hotelbeds API and handle pagination
19
+ def fetch_activities(latitude, longitude, from_date, to_date, language="en", paxes=[{"age": 5}, {"age": 70}], order="DEFAULT"):
20
+ url = "https://api.test.hotelbeds.com/activity-api/3.0/activities/availability"
21
+ signature = generate_signature()
22
+ headers = {
23
+ "Content-Type": "application/json",
24
+ "Api-Key": hotelbeds_api_key,
25
+ "X-Signature": signature,
26
+ "Accept": "application/json",
27
+ }
28
+
29
+ payload = {
30
+ "filters": [
31
+ {
32
+ "searchFilterItems": [
33
+ {
34
+ "type": "gps",
35
+ "latitude": latitude,
36
+ "longitude": longitude
37
+ }
38
+ ]
39
+ }
40
+ ],
41
+ "from": from_date,
42
+ "to": to_date,
43
+ "language": language,
44
+ "paxes": paxes,
45
+ "order": order
46
+ }
47
+
48
+ activities = []
49
+ page = 1
50
+ items_per_page = 100
51
+ total_items = 0
52
+
53
+ while True:
54
+ payload['pagination'] = {"page": page, "itemsPerPage": items_per_page}
55
+ response = requests.post(url, headers=headers, json=payload)
56
+ if response.status_code != 200:
57
+ st.error(f"Error fetching activities: {response.status_code}")
58
+ return None
59
+
60
+ data = response.json()
61
+ if page == 1:
62
+ total_items = data['pagination']['totalItems']
63
+ items_per_page = data['pagination']['itemsPerPage']
64
+
65
+ activities.extend(data['activities'])
66
+
67
+ if total_items <= items_per_page * page:
68
+ break
69
+
70
+ page += 1
71
+
72
+ return activities
73
+
74
+ # Streamlit app
75
+ st.title("Hotelbeds Activity Availability Checker")
76
+
77
+ latitude = st.number_input("Latitude", value=40.730610)
78
+ longitude = st.number_input("Longitude", value=-73.93524)
79
+ from_date = st.date_input("From Date", value=pd.to_datetime("2024-06-20"))
80
+ to_date = st.date_input("To Date", value=pd.to_datetime("2024-06-24"))
81
+
82
+ if st.button("Check Availability"):
83
+ activities = fetch_activities(latitude, longitude, from_date.isoformat(), to_date.isoformat())
84
+ if activities:
85
+ st.json(activities)
86
+ else:
87
+ st.error("No activities found or an error occurred.")