This repository was archived by the owner on Dec 10, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhackpad.py
More file actions
67 lines (54 loc) · 1.88 KB
/
hackpad.py
File metadata and controls
67 lines (54 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
import sys
from requests_oauthlib import OAuth1Session
from urllib.parse import urljoin
class Hackpad(object):
def __init__(self, sub_domain='', consumer_key='', consumer_secret=''):
self.sub_domain = sub_domain
self.consumer_key = consumer_key
self.consumer_secret = consumer_secret
return
def get_pad_content(self, padId, revision='latest', response_format='txt', asUser=''):
api_link = 'pad/%s/content/%s.%s' % (padId, revision, response_format)
params = {}
if asUser != '':
params['asUser'] = asUser
return self.do_api_request(api_link, 'GET', params)
def search_for_pads(self, q='', start=0, limit=10, asUser=''):
api_link = 'search'
params = {'q':q, 'start':start, 'limit':limit}
if asUser != '':
params['asUser'] = asUser
return self.do_api_request(api_link, 'GET', params)
def list_all(self):
api_link = 'pads/all'
return self.do_api_request(api_link, 'GET')
def do_api_request(self, path, method, post_data={}, body='', content_type=None):
method = method.upper()
hackpad = {}
try:
if self.sub_domain:
path = urljoin('https://%s.hackpad.com/api/1.0/' % self.sub_domain, path)
else:
path = urljoin('https://hackpad.com/api/1.0/', path)
params = {
'client_key': self.consumer_key,
'client_secret': self.consumer_secret
}
headers = {}
if content_type:
headers['content-type'] = content_type
for key in post_data.keys():
params[key] = post_data[key]
hackpad_api = OAuth1Session(**params)
if method == 'POST':
r = hackpad_api.post(path, data=body, headers=headers)
hackpad = r.json()
else:
r = hackpad_api.get(path, headers=headers)
try:
hackpad = r.json()
except:
hackpad = r.content
except:
print(sys.exc_info()[0])
return hackpad