def test_location_autosuggest_xml(self): hotels_service = Hotels(self.api_key, response_format='xml') self.result = hotels_service.location_autosuggest( market='DE', currency='EUR', locale='de-DE', query='Berlin').parsed self.assertTrue(None != self.result.find('./Results')) self.assertTrue( len(self.result.findall('./Results/HotelResultDto')) > 0)
def test_location_autosuggest_json(self): hotels_service = Hotels(self.api_key, response_format='json') self.result = hotels_service.location_autosuggest(market='UK', currency='GBP', locale='en-GB', query='Kuala').parsed self.assertTrue('results' in self.result) self.assertTrue(len(self.result['results']) > 0)
def get_best_hotel(city, date_in, date_out): hotels_service = Hotels('prtl6749387986743898559646983194') city = city_string_to_id_for_hotels(city) resp = hotels_service.get_result( **{ 'market': 'ES', 'currency': 'EUR', 'locale': 'en-GB', 'country': 'ES', 'entityid': city['individual_id'], 'checkindate': date_in, 'checkoutdate': date_out, 'guests': 1, 'rooms': 1 }) hotels_prices = dict([(hotel['id'], hotel) for hotel in resp.json()['hotels_prices']]) hotels = sorted(resp.json()['hotels'], key=lambda x: x['popularity']) for hotel in hotels: hotel['price'] = hotels_prices[ hotel['hotel_id']]['agent_prices'][0]['price_total'] if not hotels: return {} the_hotel_offer = random.choice(hotels[:5]) return { 'type': 'hotel', 'date': date_out, 'price': str(int(the_hotel_offer['price'])), # 'details': { # 'latitude': the_hotel_offer['latitude'], # 'longitude': the_hotel_offer['longitude'], # 'name': the_hotel_offer['name'], # 'checkin': date_in, # 'checkout': date_out # } 'description': "In %s, stay at %s, until %s" % (city['display_name'], the_hotel_offer['name'], parser.parse(date_out).strftime("%B %d, %Y, %A")), 'detailsLink': '#', 'img': '' }
def city_string_to_id_for_hotels(city_as_str): hotels_service = Hotels(API_KEY) city_suggestion_results = hotels_service.location_autosuggest( **{ 'market': 'TR', 'currency': 'EUR', 'locale': 'en-GB', 'query': city_as_str }).json()['results'] cities = list( filter(lambda x: x['geo_type'] == 'City', city_suggestion_results)) if not cities: return None else: return cities[0]
def test_create_session(self): """ http://partners.api.skyscanner.net/apiservices/carhire/liveprices/v2/{market}/{currency}/{locale}/{pickupplace}/{dropoffplace}/{pickupdatetime}/{dropoffdatetime}/{driverage}?apiKey={apiKey}&userip={userip} YYYY-MM-DDThh:mm """ hotels_service = Hotels(self.api_key) poll_url = hotels_service.create_session(market='UK', currency='GBP', locale='en-GB', entityid=27543923, checkindate=self.checkin, checkoutdate=self.checkout, guests=1, rooms=1) self.assertTrue(poll_url)
def test_get_result_xml(self): """ http://partners.api.skyscanner.net/apiservices/carhire/liveprices/v2/{market}/{currency}/{locale}/{pickupplace}/{dropoffplace}/{pickupdatetime}/{dropoffdatetime}/{driverage}?apiKey={apiKey}&userip={userip} YYYY-MM-DDThh:mm """ hotels_service = Hotels(self.api_key, response_format='xml') self.result = hotels_service.get_result(market='DE', currency='EUR', locale='de-DE', entityid=27543923, checkindate=self.checkin, checkoutdate=self.checkout, guests=1, rooms=1).parsed self.assertTrue(None != self.result.find('./Hotels')) self.assertTrue(len(self.result.findall('./Hotels/HotelDto')) > 0)
def test_get_result_json(self): """ http://partners.api.skyscanner.net/apiservices/carhire/liveprices/v2/{market}/{currency}/{locale}/{pickupplace}/{dropoffplace}/{pickupdatetime}/{dropoffdatetime}/{driverage}?apiKey={apiKey}&userip={userip} YYYY-MM-DDThh:mm """ hotels_service = Hotels(self.api_key, response_format='json') self.result = hotels_service.get_result(market='UK', currency='GBP', locale='en-GB', entityid=27543923, checkindate=self.checkin, checkoutdate=self.checkout, guests=1, rooms=1).parsed self.assertTrue('hotels' in self.result) self.assertTrue(len(self.result['hotels']) > 0)
import re from datetime import datetime, timedelta from pprint import pprint import pandas as pd from skyscanner.skyscanner import Hotels from google_places import geocode hotels_service = Hotels('prtl6749387986743898559646983194') def map_images(x): splitted = x[0].split('{') base = splitted[0:2] imgs = splitted[2:] base = ''.join(base)[:-1] imgs = re.split(r':\[\d*,\d*\],', imgs[0])[:-2] result = [] for img in imgs: result.append(base + img) return result def get_hotels(city, checkin, checkout, guests=1, rooms=1): latlon = geocode(city) entity = '{},{}-latlong'.format(latlon['lat'], latlon['lng']) hotels = hotels_service.get_result(market='CH', currency='EUR', locale='en-GB', entityid=entity,
def get_hotels_service(): keys = get_keys() return Hotels(keys[2])