File size: 5,264 Bytes
206fbad
 
 
 
 
b062e1c
ab9823a
761655f
c355d60
 
206fbad
b062e1c
 
 
 
 
 
206fbad
 
 
 
 
 
 
 
 
 
 
c355d60
09ea957
 
ab9823a
c355d60
 
561e4d7
ab9823a
c355d60
761655f
c355d60
 
561e4d7
 
09ea957
 
 
 
 
 
 
 
 
 
 
 
 
 
 
561e4d7
 
ab9823a
206fbad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d3b1f9
 
 
 
206fbad
 
8d3b1f9
206fbad
 
 
8d3b1f9
206fbad
8d3b1f9
206fbad
 
 
 
 
 
 
 
 
ab9823a
206fbad
 
 
 
ab9823a
206fbad
 
f7198bf
 
 
 
 
 
 
 
 
 
 
 
 
 
206fbad
91818ed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import requests
import hashlib
import datetime
import streamlit as st
import pandas as pd
import openai
import os
from langchain.chat_models import ChatOpenAI
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate

# Make sure OpenAI's API key is set
openai.api_key = os.getenv("OPENAI_API_KEY")

if not openai.api_key:
    raise ValueError("OpenAI API key not set. Check your environment variables.")

# Hotelbeds API credentials
hotelbeds_api_key = "95ce3e45a02fc6fd9720ecc013a6f674"
hotelbeds_api_secret = "785150201f"

# Function to generate the API signature
def generate_signature():
    local_timestamp = int(datetime.datetime.now().timestamp())
    assemble = hotelbeds_api_key + hotelbeds_api_secret + str(local_timestamp)
    signature = hashlib.sha256(assemble.encode()).hexdigest()
    return signature

# Function to get geo-coordinates from a location name using OpenAI API through Langchain
import re

def get_coordinates(location_name):
    prompt_template = PromptTemplate(
        input_variables=["location"],
        template="What are the exact latitude and longitude of {location}? Please provide the values in decimal degrees."
    )
    
    llm = ChatOpenAI(model="gpt-4", openai_api_key=openai.api_key)
    chain = LLMChain(llm=llm, prompt=prompt_template)
    
    result = chain.run(location=location_name).strip()
    try:
        # Use regular expressions to find latitude and longitude in the text
        lat_lon_match = re.findall(r"(-?\d+\.\d+)°?\s*([NSEW])", result)
        if lat_lon_match and len(lat_lon_match) == 2:
            # Extract latitude and longitude from regex matches
            latitude, lat_dir = lat_lon_match[0]
            longitude, lon_dir = lat_lon_match[1]
            
            # Convert the strings to float and adjust sign according to the hemisphere
            latitude = float(latitude) * (-1 if lat_dir in ['S', 's'] else 1)
            longitude = float(longitude) * (-1 if lon_dir in ['W', 'w'] else 1)
            
            return latitude, longitude
        else:
            raise ValueError("Could not find both latitude and longitude in the response")
    except Exception as e:
        st.error(f"Unable to parse coordinates from the AI's response: '{result}'. Error: {str(e)}")
        return None, None

# Function to make the POST request to the Hotelbeds API and handle pagination
def fetch_activities(latitude, longitude, from_date, to_date, language="en", paxes=[{"age": 5}, {"age": 70}], order="DEFAULT"):
    url = "https://api.test.hotelbeds.com/activity-api/3.0/activities/availability"
    signature = generate_signature()
    headers = {
        "Content-Type": "application/json",
        "Api-Key": hotelbeds_api_key,
        "X-Signature": signature,
        "Accept": "application/json",
    }
    
    payload = {
        "filters": [
            {
                "searchFilterItems": [
                    {
                        "type": "gps",
                        "latitude": latitude,
                        "longitude": longitude
                    }
                ]
            }
        ],
        "from": from_date,
        "to": to_date,
        "language": language,
        "paxes": paxes,
        "order": order
    }

    activities = []
    page = 1
    items_per_page = 100
    total_items = 0

    while True:
        payload['pagination'] = {"page": page, "itemsPerPage": items_per_page}
        response = requests.post(url, headers=headers, json=payload)
        data = response.json()

        if response.status_code != 200 or 'errors' in data and data['errors']:
            st.error(f"Error fetching activities: {response.status_code}, Details: {data['errors']}")
            return None

        if 'pagination' in data:
            total_items = data['pagination']['totalItems']
            items_per_page = data['pagination']['itemsPerPage']

        activities.extend(data.get('activities', []))

        if 'pagination' not in data or total_items <= items_per_page * page:
            break

        page += 1

    return activities

# Streamlit app
st.title("Hotelbeds Activity Availability Checker")

location_name = st.text_input("Location Name", value="New York City")
from_date = st.date_input("From Date", value=pd.to_datetime("2024-06-20"))
to_date = st.date_input("To Date", value=pd.to_datetime("2024-06-24"))

if st.button("Check Availability"):
    latitude, longitude = get_coordinates(location_name)
    activities = fetch_activities(latitude, longitude, from_date.isoformat(), to_date.isoformat())
    if activities:
        activity_details = []
        for activity in activities:
            name = activity['content']['name']
            adult_price = next(
                (amount['amount'] for amount in activity['modalities'][0]['amountsFrom'] if amount['paxType'] == 'ADULT'),
                None
            )
            if adult_price is not None:
                activity_details.append(f"{name} - ${adult_price:.2f}")
            else:
                activity_details.append(name)
        
        st.json(activity_details)
        st.write(f"Total number of activities: {len(activity_details)}")
    else:
        st.error("No activities found or an error occurred.")