-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathyellowbot.py
More file actions
69 lines (49 loc) · 1.88 KB
/
yellowbot.py
File metadata and controls
69 lines (49 loc) · 1.88 KB
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
import json, urllib, urllib2, time
import hashlib, hmac
from urlparse import urlparse
class YellowBot():
def __init__(self, **args):
assert args.get('api_key'), "Missing api_key parameter"
assert args.get('api_secret'), "Missing api_secret parameter"
if not args.get('server'):
args['server'] = 'www.yellowbot.com'
self.server = args['server']
self.api_base = 'http://' + args['server'] + "/api/"
self.api_key = args['api_key']
self.api_secret = args['api_secret']
return None
def call(self, method, **args):
request = self._request(method, args)
response = urllib2.urlopen(request)
#print response.info()
api_data = response.read()
data = json.loads(api_data)
return data
def signin_url(self, **args):
domain = args.get('domain');
if not domain:
domain = self.domain
url = "http://" + domain + "/signin/partner"
args = self._query_args(args)
url += "?" + urllib.urlencode(args)
return url
def _query_args(self, args):
args.update({
'api_key': self.api_key,
'api_ts' : str(int(time.time()))
})
args.update({
'api_sig': self._signature(args)
})
return args
def _request(self, method, args):
args = self._query_args(args)
r = urllib2.Request( url = self.api_base + method, data = urllib.urlencode(args) )
return r
def _signature(self, args):
parameters = ""
if args:
for key in sorted(list(args.keys())):
parameters += str(key) + str(args[key])
signature = hmac.new(self.api_secret, parameters, hashlib.sha256).hexdigest()
return signature