Created
November 24, 2017 09:06
-
-
Save vitasiku/ce2e89b8083e2190cbbdfc3aa7c41169 to your computer and use it in GitHub Desktop.
quering booking.com for data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import requests | |
from requests.auth import HTTPBasicAuth | |
import datetime | |
import json | |
API_BASEURL = 'https://distribution-xml.booking.com/2.0/json/hotelAvailability' | |
dir_name = os.path.dirname(__file__) | |
DATA_DIR = os.path.join(dir_name, 'data') | |
USER_NAME = "booking_hackathon_uganda" | |
PASSWORD = "UgandaH4ckerz" | |
def write(file_name, res_data): | |
""" | |
Write to a file storage | |
""" | |
try: | |
out = open(file_name, 'w') | |
json.dump(res_data, out, indent=4, sort_keys=True) | |
out.close() | |
except Exception as e: | |
raise | |
def read(in_file_path): | |
""" | |
Read json file | |
""" | |
try: | |
return json.load(open(in_file_path)) | |
except Exception as e: | |
raise e | |
class Retriever(): | |
def __init__(self, cnf): | |
self.cnf = cnf | |
def run(self): | |
city_id = self.cnf['city_id'] | |
res_data = self.retrieve(city_id) | |
self.save(city_id, res_data) | |
print('done') | |
def retrieve(self, city_id): | |
checkin = self.cnf['checkin'] | |
checkout = self.cnf['checkout'] | |
extras = self.cnf['extras'] | |
request_url = API_BASEURL + '?checkin=' + checkin + "&checkout=" + checkout + \ | |
"&city_ids=-"+city_id+"&room1=A,A&extras="+extras | |
try: | |
resp = requests.get(request_url, auth=HTTPBasicAuth(USER_NAME, PASSWORD)) | |
return resp.json() | |
except Exception as e: | |
raise e | |
def save(self, city_id, res_data): | |
""" | |
Save it to file | |
""" | |
download_time = datetime.datetime.now() | |
file_name = download_time.strftime("%Y-%m-%d_%H-%M-%S") | |
res_data['query_time'] = download_time.strftime("%Y-%m-%d %H:%M") | |
res_data['download_time'] = download_time.strftime("%Y-%m-%d_%H-%M-%S") | |
file_path = os.path.join(DATA_DIR, city_id, file_name + '.json') | |
write(file_path, res_data) | |
if __name__=="__main__": | |
config = { | |
'city_id': '1728891', | |
'checkin': '2018-11-23', | |
'checkout': '2018-11-24', | |
'extras': 'room_details,hotel_details,room_amenities,hotel_amenities,payment_terms' | |
} | |
ret = Retriever(config) | |
ret.run() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment