forked from tripit/python_binding_v1
-
Notifications
You must be signed in to change notification settings - Fork 1
/
__init__.py
561 lines (438 loc) · 18.6 KB
/
__init__.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
#!/usr/bin/env python
#
# Copyright 2008-2012 Concur Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
Methods to interact with the TripIt v1 API
"""
import base64
import datetime
import hmac
from hashlib import md5
import random
import re
import time
import urllib
import urllib2
import traceback
import xml.sax
import json
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
class WebAuthCredential:
def __init__(self, username, password):
self._username = username
self._password = password
def getUsername(self):
return self._username
def getPassword(self):
return self._password
def authorize(self, request, args):
pair = "%s:%s" % (self._username, self._password)
token = base64.b64encode(pair)
request.add_header('Authorization', 'Basic %s' % token)
# } class:WebAuthCredential
class OAuthConsumerCredential:
OAUTH_SIGNATURE_METHOD = 'HMAC-SHA1'
OAUTH_VERSION = '1.0'
# You can construct 3 kinds of OAuth credentials:
# 1. A credential with no token (to get a request token):
# OAuthConsumerCredential('consumer_key', 'consumer_secret')
# OAuthConsumerCredential(oauth_consumer_key='consumer_key', oauth_consumer_secret='consumer_secret')
# 2. A 3 legged OAuth credential (request or authorized token):
# OAuthConsumerCredential('consumer_key', 'consumer_secret', 'token', 'token_secret')
# OAuthConsumerCredential(oauth_consumer_key='consumer_key', oauth_consumer_secret='consumer_secret', oauth_token='token', oauth_token_secret='token_secret')
# 3. A 2 legged OAuth credential:
# OAuthConsumerCredential('consumer_key', 'consumer_secret', 'requestor_id')
# OAuthConsumerCredential(oauth_consumer_key='consumer_key', oauth_consumer_secret='consumer_secret', oauth_requestor_id='requestor_id')
def __init__(self, oauth_consumer_key, oauth_consumer_secret,
oauth_token='', oauth_token_secret='', oauth_requestor_id=''):
self._oauth_consumer_key = oauth_consumer_key
self._oauth_consumer_secret = oauth_consumer_secret
self._oauth_oauth_token = self._oauth_token_secret = self._oauth_requestor_id = ''
if oauth_token != '' and oauth_token_secret != '':
self._oauth_oauth_token = oauth_token
self._oauth_token_secret = oauth_token_secret
elif oauth_token != '':
self._oauth_requestor_id = oauth_token
elif oauth_requestor_id != '':
self._oauth_requestor_id = oauth_requestor_id
def authorize(self, request, args):
request.add_header('Authorization',
self._generate_authorization_header(
request, args))
def validateSignature(self, url):
base_url, query = url.split("?", 1)
params = {}
def parse_param(param_string):
name, value = param_string.split("=", 1)
params[urllib.unquote(name)] = urllib.unquote(value)
map(parse_param, query.split("&"))
signature = params.get('oauth_signature')
return signature == self._generate_signature('GET', base_url, params)
def getOAuthConsumerKey(self):
return self._oauth_consumer_key
def getOAuthConsumerSecret(self):
return self._oauth_consumer_secret
def getOAuthToken(self):
return self._oauth_oauth_token
def getOAuthTokenSecret(self):
return self._oauth_token_secret
def getOAuthRequestorId(self):
return self._oauth_requestor_id
def getSessionParameters(self, redirect_url, action):
params = self._generate_oauth_parameters('GET', action, {'redirect_url':redirect_url})
params['redirect_url'] = redirect_url
params['action'] = action
return json.dumps(params)
def _generate_authorization_header(self, request, args):
realm = request.get_type() + '://' + request.get_host()
http_method = request.get_method().upper()
http_url = request.get_type() + '://' + request.get_host() + request.get_selector().split('?', 1)[0]
return ('OAuth realm="%s",' % (realm)) + \
','.join(
['%s="%s"' % (_escape(k), _escape(v))
for k, v in self._generate_oauth_parameters(
http_method, http_url, args).items()])
def _generate_oauth_parameters(self, http_method, http_url, args):
oauth_parameters = {
'oauth_consumer_key' :
self._oauth_consumer_key,
'oauth_nonce' : _generate_nonce(),
'oauth_timestamp' : str(int(time.time())),
'oauth_signature_method' :
OAuthConsumerCredential.OAUTH_SIGNATURE_METHOD,
'oauth_version' : OAuthConsumerCredential.OAUTH_VERSION
}
if self._oauth_oauth_token != '':
oauth_parameters['oauth_token'] = \
self._oauth_oauth_token
if self._oauth_requestor_id != '':
oauth_parameters['xoauth_requestor_id'] = \
self._oauth_requestor_id
oauth_parameters_for_base_string = oauth_parameters.copy()
if args is not None:
oauth_parameters_for_base_string.update(args)
oauth_parameters['oauth_signature'] = self._generate_signature(http_method, http_url, oauth_parameters_for_base_string)
return oauth_parameters
def _generate_signature(self, method, base_url, params):
base_url = _escape(base_url)
params.pop('oauth_signature', None)
parameters = _escape(
'&'.join(
['%s=%s' % \
(_escape(str(k)), _escape(str(params[k]))) \
for k in sorted(params)]))
signature_base_string = '&'.join([method, base_url, parameters])
key = self._oauth_consumer_secret + '&' + self._oauth_token_secret
try:
import hashlib
hashed = hmac.new(key, signature_base_string, hashlib.sha1)
except ImportError:
import sha
hashed = hmac.new(key, signature_base_string, sha)
return base64.b64encode(hashed.digest())
# } class:OAuthConsumerCredential
def _escape(s):
return urllib.quote(str(s), safe='~')
def _generate_nonce():
random_number = ''.join(str(random.randint(0, 9)) for _ in range(40))
m = md5(str(time.time()) + str(random_number))
return m.hexdigest()
class ResponseHandler(xml.sax.handler.ContentHandler):
def __init__(self):
xml.sax.handler.ContentHandler.__init__(self)
self._element_stack = []
self._current_content = None
self._root_obj = None
def get_response_obj(self):
return self._root_obj
def startElement(self, name, attrs):
if re.match('[A-Z]', name):
type_name = str(name)
data_node = TravelObj(type_name, (),
{ '_attributes' : { }, '_children' : [] })
if len(self._element_stack) > 0:
self._element_stack[-1].add_child(data_node)
self._element_stack.append(data_node)
if self._root_obj is None:
self._root_obj = self._element_stack[0]
def endElement(self, name):
if self._current_content is not None:
if name.endswith('date'):
self._current_content = datetime.date(
*(time.strptime(
self._current_content, '%Y-%m-%d')[0:3]))
elif name.endswith('time'):
self._current_content = datetime.time(
*(time.strptime(
self._current_content, '%H:%M:%S')[3:6]))
self._element_stack[-1].set_attribute(name, self._current_content)
self._current_content = None
if re.match('[A-Z]', name):
self._element_stack.pop()
def characters(self, content):
if self._current_content is not None:
self._current_content = '%s%s' % (self._current_content, content)
else:
self._current_content = content
# } class:ResponseHandler
class TravelObj(type):
def __new__(cls, name, bases, dict):
return type.__new__(cls, name, bases, dict)
def __init__(cls, name, bases, dict):
super(TravelObj, cls).__init__(name, bases, dict)
def __getattr__(self, name):
return self.get_attribute_value(name)
def __cmp__(self, other):
# start_date
try:
if self.start_date and other.start_date:
return cmp(self.start_date, other.start_date)
except AttributeError:
pass
# StartDateTime or DateTime
try:
cls_start_datetime_obj = None
other_start_datetime_obj = None
for child in self.get_children():
if child.__name__ == 'StartDateTime' or \
child.__name__ == 'DateTime':
cls_start_datetime_obj = datetime.datetime.combine(
child.date, child.time)
break
for child in other.get_children():
if child.__name__ == 'StartDateTime' or \
child.__name__ == 'DateTime':
other_start_datetime_obj = datetime.datetime.combine(
child.date, child.time)
break
return cmp(cls_start_datetime_obj, other_start_datetime_obj)
except Exception:
pass
def set_attribute(self, name, value):
self._attributes[name] = value
def add_child(self, child):
self._children.append(child)
def get_attribute_names(self):
return self._attributes.keys()
def get_attribute_value(self, name):
if name in self._attributes:
return self._attributes[name]
else:
raise AttributeError("'TravelObj' has no attribute '%s'" % name)
def get_children(self):
return self._children
def has_error(self):
for o in self._children:
if o.__name__ == 'Error':
return True
return False
def has_warning(self):
for o in self._children:
if o.__name__ == 'Warning':
return True
return False
# } class:TravelObj
class TripIt(object):
# webauth_credentials and oauth_credentials are for backward compatibility
# of keyword arguments. Consider them deprecated and instead use either
# the first positional argument, or the "credential" keyword argument
# for either type of credential.
#
# NOTE that if you were using positional arguments before for anything but
# webauth credentials, the signature of this constructor has changed, and
# you will need to update your code to match.
#
# Also, note that despite having a default value, the "credential" parameter
# is NOT optional. In a future release, the default value, and the
# webauth_credentials and oauth_credentials keyword parameters will be removed.
def __init__(self, credential = None, api_url='https://api.tripit.com',
webauth_credentials = None, oauth_credentials = None):
self._api_url = api_url
self._api_version = 'v1'
self._credential = credential or oauth_credentials or webauth_credentials
self.resource = None
self.response = None
self.http_code = None
def _do_request(self, verb, entity=None, url_args=None, post_args=None):
"""
Makes a request POST/GET to the API and returns the response
from the server.
"""
if verb in ['/oauth/request_token', '/oauth/access_token']:
base_url = self._api_url + verb
else:
if entity is not None:
base_url = '/'.join(
[self._api_url, self._api_version, verb, entity])
else:
base_url = '/'.join([self._api_url, self._api_version, verb])
args = None
if url_args is not None:
args = url_args
url = base_url + '?' + urllib.urlencode(url_args)
else:
url = base_url
self.resource = url
if post_args is not None:
args = post_args
request = urllib2.Request(url, urllib.urlencode(post_args))
else:
request = urllib2.Request(url)
self._credential.authorize(request, args)
stream = None
try:
stream = urllib2.urlopen(request)
self.http_code = 200
except urllib2.HTTPError, http_error:
self.http_code = http_error.code
stream = http_error
data = stream.read()
stream.close()
self.response = data
return data
def _parse_command(self, params=None, post_args=None, override_verb=None):
verb = override_verb
entity = None
if verb is None:
command = traceback.extract_stack()[-2][2]
try:
(verb, entity) = command.split('_', 1)
except ValueError:
verb = command
entity = None
response_data = self._do_request(verb, entity, params, post_args)
return _xml_to_py(response_data)
def get_trip(self, id, filter=None):
if filter is None:
filter = {}
filter['id'] = id
return self._parse_command(filter)
def get_air(self, id):
return self._parse_command({ 'id' : id })
def get_lodging(self, id):
return self._parse_command({ 'id' : id })
def get_car(self, id):
return self._parse_command({ 'id' : id })
def get_points_program(self, id):
return self._parse_command({ 'id' : id })
def get_profile(self):
return self._parse_command()
def get_rail(self, id):
return self._parse_command({ 'id' : id })
def get_transport(self, id):
return self._parse_command({ 'id' : id })
def get_cruise(self, id):
return self._parse_command({ 'id' : id })
def get_restaurant(self, id):
return self._parse_command({ 'id' : id })
def get_activity(self, id):
return self._parse_command({ 'id' : id })
def get_note(self, id):
return self._parse_command({ 'id' : id })
def get_map(self, id):
return self._parse_command({ 'id' : id })
def get_directions(self, id):
return self._parse_command({ 'id' : id })
def delete_trip(self, id):
return self._parse_command({ 'id' : id })
def delete_air(self, id):
return self._parse_command({ 'id' : id })
def delete_lodging(self, id):
return self._parse_command({ 'id' : id })
def delete_car(self, id):
return self._parse_command({ 'id' : id })
def delete_rail(self, id):
return self._parse_command({ 'id' : id })
def delete_transport(self, id):
return self._parse_command({ 'id' : id })
def delete_cruise(self, id):
return self._parse_command({ 'id' : id })
def delete_restaurant(self, id):
return self._parse_command({ 'id' : id })
def delete_activity(self, id):
return self._parse_command({ 'id' : id })
def delete_note(self, id):
return self._parse_command({ 'id' : id })
def delete_map(self, id):
return self._parse_command({ 'id' : id })
def delete_directions(self, id):
return self._parse_command({ 'id' : id })
def replace_trip(self, id, xml):
return self._parse_command({ 'id' : id, 'xml' : xml })
def replace_air(self, id, xml):
return self._parse_command({ 'id' : id, 'xml' : xml })
def replace_lodging(self, id, xml):
return self._parse_command({ 'id' : id , 'xml' : xml})
def replace_car(self, id, xml):
return self._parse_command({ 'id' : id, 'xml' : xml })
def replace_rail(self, id, xml):
return self._parse_command({ 'id' : id, 'xml' : xml })
def replace_transport(self, id, xml):
return self._parse_command({ 'id' : id, 'xml' : xml })
def replace_cruise(self, id, xml):
return self._parse_command({ 'id' : id, 'xml' : xml })
def replace_restaurant(self, id, xml):
return self._parse_command({ 'id' : id, 'xml' : xml })
def replace_activity(self, id, xml):
return self._parse_command({ 'id' : id, 'xml' : xml })
def replace_note(self, id, xml):
return self._parse_command({ 'id' : id, 'xml' : xml })
def replace_map(self, id, xml):
return self._parse_command({ 'id' : id, 'xml' : xml })
def replace_directions(self, id, xml):
return self._parse_command({ 'id' : id, 'xml' : xml })
def list_trip(self, filter=None):
return self._parse_command(filter)
def list_object(self, filter=None):
return self._parse_command(filter)
def list_points_program(self):
return self._parse_command()
def create(self, xml):
return self._parse_command(None, { 'xml' : xml })
def crs_load_reservations(self, xml, company_key=None):
args = {'xml' : xml}
if company_key is not None:
args['company_key'] = company_key
return self._parse_command(None, args, 'crsLoadReservations')
def crs_delete_reservations(self, record_locator):
return self._parse_command({'record_locator' : record_locator}, None, 'crsDeleteReservations')
def get_request_token(self):
response = self._do_request('/oauth/request_token')
if self.http_code == 200:
return _parse_qs(response)
else:
return response
def get_access_token(self):
response = self._do_request('/oauth/access_token')
if self.http_code == 200:
return _parse_qs(response)
else:
return response
# } class:TripIt
def _parse_qs(qs):
request_params = {}
for param in qs.split('&'):
(request_param, request_param_value) = param.split('=')
request_params[request_param] = request_param_value
return request_params
def _xml_to_py(data):
parser = xml.sax.make_parser()
handler = ResponseHandler()
parser.setContentHandler(handler)
parser.parse(StringIO(data))
return handler.get_response_obj()