-
Notifications
You must be signed in to change notification settings - Fork 1
/
search_info.py
119 lines (97 loc) · 3.86 KB
/
search_info.py
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
#!/usr/bin/env python
# coding=utf-8
import pprint
import csv
import click
import requests
import datetime as datetime
from datetime import date
from xml.etree import ElementTree as ET
import os
# from random import sample
import random
import json
# import logging
def validate_d(date_text):
try:
datetime.datetime.strptime(date_text, '%Y-%m-%d')
except ValueError:
raise ValueError("Incorrect data format, should be YYYY-MM-DD")
def daterange(start_date, end_date):
for n in range(int ((end_date - start_date).days)):
yield start_date + datetime.timedelta(n)
# booking_id_secret = None
# with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'secrets.json')) as data_file:
# booking_id_secret = (json.load(data_file))['booking_id']
REF_API = 'api'
REF_CLIENT = 'client'
REF_AGENT = 'agent'
@click.command()
@click.option('--filename', default='search_item_info_keys')
@click.option('--client', default='ali')
# @click.option('--days', default=1, type=int)
def search_info(filename, client):
url = 'https://rbs.gta-travel.com/rbscnapi/RequestListenerServlet'
pp = pprint
res = []
agent_secret = None
with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'secrets.json')) as data_file:
agent_secret = (json.load(data_file))[client]
# validate_d(from_d)
# validate_d(to_d)
# from_date = datetime.datetime.today().date() + datetime.timedelta(days=days)
# to_date = from_date + datetime.timedelta(days=1)
print('Search client.. ' + client)
# print('Duration.. ' + )
# from_date = datetime.datetime.strptime(from_d, '%Y-%m-%d').date()
# to_date = datetime.datetime.strptime(to_d, '%Y-%m-%d').date()
# pp.pprint('? ' + str(skip))
hotel_codes = []
with open(filename, 'r') as file:
for line in file:
# pp.pprint(line)
try:
city_code, item_code = line.rstrip().split('_')
except ValueError:
print('Warning: Invalid GTA key format..')
continue
hotel_codes.append(dict([('city_code', city_code), ('item_code', item_code)]))
search_tree = ET.parse(os.path.join(os.getcwd(), 'SearchItemInformationRequest.xml'))
for hotel_code in hotel_codes:
pp.pprint('Searching info for ' + hotel_code['city_code'] + ' ' + hotel_code['item_code'])
search_tree.find('.//ItemDestination').set('DestinationCode', hotel_code['city_code'])
search_tree.find('.//ItemCode').text = hotel_code['item_code']
try:
r = requests.post(url, data=ET.tostring(search_tree.getroot(), encoding='UTF-8', method='xml'), timeout=600)
except OSError:
pp.pprint('Error: OSError.. Searching has stopped..')
continue
r_tree = ET.fromstring(r.text)
entry = {}
entry['GTA_key'] = hotel_code['city_code'] + '_' + hotel_code['item_code']
entry['hotel_name'] = ''
entry['hotel_email'] = ''
entry['Latitude'] = ''
entry['Longitude'] = ''
name_ele = r_tree.find('.//Item')
if name_ele != None:
entry['hotel_name'] = r_tree.find('.//Item').text
email_ele = r_tree.find('.//EmailAddress')
if email_ele != None:
entry['hotel_email'] = r_tree.find('.//EmailAddress').text
geo_ele = r_tree.find('.//Latitude')
if geo_ele != None:
entry['Latitude'] = r_tree.find('.//Latitude').text
geo_ele = r_tree.find('.//Longitude')
if geo_ele != None:
entry['Longitude'] = r_tree.find('.//Longitude').text
res.append(entry)
# keys = res[0].keys()
keys = res[0].keys()
# with open('output_SearchPrice_' + date.today().strftime('%Y_%m_%d') + '.csv', 'w', encoding='utf-8') as output_file:
with open('output_SearchInfo_' + datetime.datetime.today().date().strftime('%y%m%d') + '_' + datetime.datetime.now().strftime('%H%M') + '.csv', 'w', encoding='utf-8') as output_file:
dict_writer = csv.DictWriter(output_file, keys)
dict_writer.writeheader()
dict_writer.writerows(res)
if __name__ == '__main__':
search_info()