-
Notifications
You must be signed in to change notification settings - Fork 1
/
pcheck_pool.py
325 lines (296 loc) · 10.9 KB
/
pcheck_pool.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/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
import random
import json
# import logging
from requests.exceptions import ConnectionError
from requests.exceptions import ChunkedEncodingError
from requests.exceptions import ReadTimeout
# from multiprocessing.dummy import Pool as ThreadPool
import multiprocessing
from xml.etree.ElementTree import ParseError
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)
MAX_RETRIES = 2
def has_item_price(r):
r_tree = ET.fromstring(r.text)
if r_tree.find('.//ItemPrice') == None:
return False
return True
def asp(s_request):
url = 'https://rbs.gta-travel.com/rbscnapi/RequestListenerServlet'
r = None
for i in range(MAX_RETRIES):
try:
r = requests.post(url, data=s_request['body'], timeout=10)
print('asp PPID: {} PID: {} GTA_key: {}'.format(os.getppid(), os.getpid(), str(s_request['gta_code'])) )
except OSError:
print('Error: ignoring OSError...' + str(i))
continue
except ConnectionError:
print('Error: ignoring ConnectionError...' + str(i))
continue
except ChunkedEncodingError:
print('Error: ignoring ChunkedEncodingError...' + str(i))
continue
except ReadTimeout:
print('Error: ignoring ReadTimeout...' + str(i))
continue
else:
break
if r == None:
print('Warning: Reached MAX RETRIES.. r==None.. ')
ent = {}
ent['gta_code'] = s_request['gta_code']
ent['city_code'] = s_request['city_code']
ent['item_code'] = s_request['item_code']
ent['body'] = s_request['body']
if r != None:
ent['text'] = r.text
return ent
def aspp(s_request):
url = 'https://rbs.gta-travel.com/rbscnapi/RequestListenerServlet'
r = None
for i in range(MAX_RETRIES):
try:
r = requests.post(url, data=s_request['body'], timeout=10)
print('aspp PPID: {} PID: {} GTA_key: {}'.format(os.getppid(), os.getpid(), str(s_request['gta_code'])) )
except OSError:
print('Error: ignoring OSError...' + str(i))
continue
except ConnectionError:
print('Error: ignoring ConnectionError...' + str(i))
continue
except ChunkedEncodingError:
print('Error: ignoring ChunkedEncodingError...' + str(i))
continue
except ReadTimeout:
print('Error: ignoring ReadTimeout...' + str(i))
continue
else:
break
if r == None:
print('Warning: Reached MAX RETRIES.. r==None.. ')
ent = {}
ent['gta_code'] = s_request['gta_code']
ent['city_code'] = s_request['city_code']
ent['item_code'] = s_request['item_code']
ent['room_id'] = s_request['room_id']
ent['price'] = s_request['price']
ent['f_body'] = s_request['body']
ent['xml_req'] = s_request['xml_req']
ent['xml_res'] = s_request['xml_res']
ent['timestamp'] = datetime.datetime.utcnow()
ent['text'] = None
if r != None:
ent['text'] = r.text
# print('Warning: Reached MAX RETRIES.. r==None.. ')
return ent
def asp_p(func, search_requests):
# pool = ThreadPool(threads)
# results = pool.map(asp, search_requests)
# pool.close()
# pool.join()
PROCESSES = 4
with multiprocessing.Pool(PROCESSES) as pool:
results = pool.map(func, search_requests)
return results
def add_empty_ent(response, checkin_date, res):
ent = {}
ent['GTA_key'] = response['gta_key']
ent['Check_in'] = checkin_date.strftime('%Y-%m-%d')
res.append(ent)
@click.command()
@click.option('--file_name', default='f_p_a_c_qunar')
@click.option('--client', default='qunar')
@click.option('--days', default=20, type=int)
def pcheck_pool(file_name, client, days):
res = []
f_res = []
search_requests = []
f_search_requests = []
excel_cell_char_limit = 32000
checkin_date = datetime.datetime.now().date() + datetime.timedelta(days=days)
hotel_ids = set()
hotel_codes = []
with open(file_name, 'r') as file:
for line in file:
if line in hotel_ids:
continue
try:
gta_code = line.rstrip()
city_code, item_code = line.rstrip().split('_')
except ValueError:
print('Warning: skipping invalid key.. ' + line.rstrip())
continue
hotel_codes.append(dict([('city_code', city_code), ('item_code', item_code), ('gta_code', gta_code)]))
hotel_ids.add(line)
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]
search_tree = ET.parse(os.path.join(os.getcwd(), 'SearchHotelPricePaxRequest.xml'))
search_tree.find('.//RequestorID').set('Client', agent_secret['id'])
search_tree.find('.//RequestorID').set('EMailAddress', agent_secret['email'])
search_tree.find('.//RequestorID').set('Password', agent_secret['password'])
# prep search_requests
for counter, hotel_code in enumerate(hotel_codes):
# search_tree.find('.//BookingReference').text = booking_id
search_tree.find('.//ItemDestination').set('DestinationCode', hotel_code['city_code'])
search_tree.find('.//ItemCode').text = hotel_code['item_code']
search_tree.find('.//PaxRoom').set('Adults', str(2))
search_tree.find('.//CheckInDate').text = checkin_date.strftime('%Y-%m-%d')
ent = {}
ent['gta_code'] = hotel_code['gta_code']
ent['city_code'] = hotel_code['city_code']
ent['item_code'] = hotel_code['item_code']
ent['body'] = ET.tostring(search_tree.getroot(), encoding='UTF-8', method='xml')
search_requests.append(ent)
# multi thread
responses = asp_p(asp, search_requests)
# process res
for response in responses:
if response == None:
entry = {}
entry['gta_code'] = response['gta_code']
entry['xml_req'] = response['body']#.replace(',', ' ')
entry['xml_res'] = None
res.append(entry)
continue
try:
r_tree = ET.fromstring(response['text'])
except ParseError:
print('Warning: Parse error for..\n' + response.text)
continue
if r_tree.find('.//RoomCategories') == None or len(r_tree.find('.//RoomCategories')) == 0:
entry = {}
entry['gta_code'] = response['gta_code']
entry['xml_req'] = response['body']#.replace(',', ' ')
entry['xml_res'] = response['text'].replace(',', ' ')[:excel_cell_char_limit]
res.append(entry)
continue
# entry['booking_id'] = response['booking_id']
for counter, room_ele in enumerate(r_tree.find('.//RoomCategories')):
entry = {}
entry['gta_code'] = response['gta_code']
entry['city_code'] = response['city_code']
entry['item_code'] = response['item_code']
entry['room_id'] = room_ele.get('Id')
entry['price'] = room_ele.find('.//ItemPrice').text
entry['checkin'] = checkin_date.strftime('%Y-%m-%d')
entry['xml_req'] = response['body']#.replace(',', ' ')
entry['xml_res'] = response['text'].replace(',', ' ')[:excel_cell_char_limit]
res.append(entry)
for entry in res:
if 'room_id' not in entry.keys():
ent = {}
ent['client'] = client
ent['client_id'] = agent_secret['id']
ent['gta_code'] = entry['gta_code']
ent['checkin'] = checkin_date.strftime('%Y-%m-%d')
f_res.append(ent)
continue
search_tree.find('.//ItemDestination').set('DestinationCode', entry['city_code'])
search_tree.find('.//ItemCode').text = entry['item_code']
# search_tree.find('.//PaxRoom').set('Adults', str(2))
search_tree.find('.//CheckInDate').text = entry['checkin']
search_tree.find('.//PaxRoom').set('Id', entry['room_id'])
ent = {}
ent['gta_code'] = entry['gta_code']
ent['city_code'] = entry['city_code']
ent['item_code'] = entry['item_code']
ent['room_id'] = entry['room_id']
ent['price'] = entry['price']
ent['xml_req'] = entry['xml_req']
ent['xml_res'] = entry['xml_res']
ent['body'] = ET.tostring(search_tree.getroot(), encoding='UTF-8', method='xml')
f_search_requests.append(ent)
# multi thread
f_responses = asp_p(aspp, f_search_requests)
for response in f_responses:
if response == None or response['text'] == None:
entry = {}
entry['client'] = client
entry['client_id'] = agent_secret['id']
entry['gta_code'] = response['gta_code']
entry['room_id'] = response['room_id']
entry['s_price'] = response['price']
entry['checkin'] = checkin_date.strftime('%Y-%m-%d')
entry['timestamp'] = response['timestamp']
entry['xml_req'] = response['xml_req']
entry['xml_res'] = response['xml_res']
entry['f_xml_req'] = response['f_body']
entry['f_xml_res'] = response['text'].replace(',', ' ')[:excel_cell_char_limit]
f_res.append(entry)
continue
try:
r_tree = ET.fromstring(response['text'])
except ParseError:
print('Warning: Parse error for..\n' + response.text)
continue
if r_tree.find('.//ItemPrice') == None:
entry = {}
entry['client'] = client
entry['client_id'] = agent_secret['id']
entry['gta_code'] = response['gta_code']
entry['room_id'] = response['room_id']
entry['s_price'] = response['price']
entry['checkin'] = checkin_date.strftime('%Y-%m-%d')
entry['timestamp'] = response['timestamp']
entry['xml_req'] = response['xml_req']
entry['xml_res'] = response['xml_res']
entry['f_xml_req'] = response['f_body']
entry['f_xml_res'] = response['text'].replace(',', ' ')[:excel_cell_char_limit]
f_res.append(entry)
continue
# entry['booking_id'] = response['booking_id']
for counter, room_ele in enumerate(r_tree.find('.//RoomCategories')):
entry = {}
entry['client'] = client
entry['client_id'] = agent_secret['id']
entry['gta_code'] = response['gta_code']
entry['city_code'] = response['city_code']
entry['item_code'] = response['item_code']
entry['room_id'] = response['room_id']
entry['s_price'] = response['price']
entry['f_price'] = room_ele.find('.//ItemPrice').text
entry['checkin'] = checkin_date.strftime('%Y-%m-%d')
entry['timestamp'] = response['timestamp']
entry['xml_req'] = response['xml_req']
entry['xml_res'] = response['xml_res']
entry['f_xml_req'] = response['f_body']
entry['f_xml_res'] = response['text'].replace(',', ' ')[:excel_cell_char_limit]
# entry['xml'] = response['text']
f_res.append(entry)
# for ent in f_res:
output_file_name = '_'.join([ 'Output_final_price_check',
file_name,
datetime.datetime.now().strftime('%y%m%d_%H%M')
]) + '.csv'
keys = None
max_len = 0
for ent in f_res:
if len(ent.keys()) > max_len:
max_len = len(ent.keys())
keys = ent.keys()
# keys = res[0].keys()
with open(output_file_name, 'w', newline='', encoding='utf-8') as output_file:
dict_writer = csv.DictWriter(output_file, keys)
dict_writer.writeheader()
dict_writer.writerows(f_res)
if __name__ == '__main__':
multiprocessing.freeze_support()
pcheck_pool()