-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventful.py
66 lines (54 loc) · 1.98 KB
/
eventful.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
"""
eventful
A Python interface to the Eventful API.
"""
__author__ = "Edward O'Connor <ted@eventful.com>"
__copyright__ = "Copyright 2005, 2006 Eventful Inc."
__license__ = "MIT"
import md5
import urllib
import httplib2
import simplejson
__all__ = ['APIError', 'API']
class APIError(Exception):
pass
class API:
def __init__(self, app_key, server='api.eventful.com', cache=None):
"""Create a new Eventful API client instance.
If you don't have an application key, you can request one:
http://api.eventful.com/keys/"""
self.app_key = app_key
self.server = server
self.http = httplib2.Http(cache)
def call(self, method, **args):
"Call the Eventful API's METHOD with ARGS."
# Build up the request
args['app_key'] = self.app_key
if hasattr(self, 'user_key'):
args['user'] = self.user
args['user_key'] = self.user_key
args = urllib.urlencode(args)
url = "http://%s/json/%s?%s" % (self.server, method, args)
# Make the request
response, content = self.http.request(url, "GET")
# Handle the response
status = int(response['status'])
if status == 200:
try:
return simplejson.loads(content)
except ValueError:
raise APIError("Unable to parse API response!")
elif status == 404:
raise APIError("Method not found: %s" % method)
else:
raise APIError("Non-200 HTTP response status: %s" % response['status'])
def login(self, user, password):
"Login to the Eventful API as USER with PASSWORD."
nonce = self.call('/users/login')['nonce']
response = md5.new(nonce + ':'
+ md5.new(password).hexdigest()).hexdigest()
login = self.call('/users/login', user=user, nonce=nonce,
response=response)
self.user_key = login['user_key']
self.user = user
return user