This repository was archived by the owner on Oct 4, 2022. It is now read-only.
forked from lymbix/Python-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlymbix.py
More file actions
139 lines (107 loc) · 4.26 KB
/
lymbix.py
File metadata and controls
139 lines (107 loc) · 4.26 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
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
import urllib
import urllib2
import json
class Lymbix:
API_BASE = 'http://api.lymbix.com/'
TONALIZE_MULTIPLE = 'tonalize_multiple'
TONALIZE_DETAILED = 'tonalize_detailed'
TONALIZE = 'tonalize'
FLAG_RESPONSE = 'flag_response'
def __init__(self, authentication_key):
'''
Args:
-authentication_key: your Lymbix authentication key
'''
if not authentication_key:
raise Exception('You must include your authentication key')
self.authentication_key = authentication_key
''' utility functions '''
def _get_headers(self):
headers = {
'Authentication': self.authentication_key,
'Accept': 'application/json',
'Version': '2.2'}
return headers
def _prep_data(self, data, options):
if options:
data.update(options)
for key, value in data.iteritems():
data[key] = json.dumps(value)
return urllib.urlencode(data)
def _call(self, url, data, returns_json=False):
headers = self._get_headers()
request = urllib2.Request(url, data, headers)
response = urllib2.urlopen(request)
if returns_json:
return json.loads(response.read())
return response.read()
''' api methods '''
def tonalize_multiple(self, articles, options=None):
'''
tonalize multiple articles
Args:
-articles: articles to tonalize
-options: additional parameters (reference_ids and return_fields)
Returns:
-see the api documentation for the format of this object
'''
if not articles:
raise Exception('You must include articles to tonalize')
url = self.API_BASE + self.TONALIZE_MULTIPLE
data = {'articles': articles}
data = self._prep_data(data, options)
return self._call(url, data, returns_json=True)
def tonalize_detailed(self, article, options=None):
'''
tonalize an article
Args:
-article: article to tonalize
-options: additional parameters (reference_id and return_fields)
Returns:
-see the api documentation for the format of this object
'''
if not article:
raise Exception('You must include an article to tonalize')
url = self.API_BASE + self.TONALIZE_DETAILED
data = {'article': article}
data = self._prep_data(data, options)
return self._call(url, data, returns_json=True)
def tonalize(self, article, options=None):
'''
tonalize an article
Args:
-article: article to tonalize
-options: additional parameters (reference_id and return_fields)
Returns:
-see the api documentation for the format of this object
'''
if not article:
raise Exception('You must include an article to tonalize')
url = self.API_BASE + self.TONALIZE
data = {'article': article}
data = self._prep_data(data, options)
return self._call(url, data, returns_json=True)
def flag_response(self, phrase, api_method=None, api_version='2.2', callback_url=None, options=None):
'''
flag a response as inaccurate
Args:
-phrase: the phrase that returns an inaccurate response
-api_method: the method that returns an inaccurate response
-api_version: the version that returns an inaccurate response
-callback_url: a url to call when the phrase has been re-rated
-options: additional parameters (reference_id)
Returns:
-see the api documentation for the format of this object
'''
if not phrase:
raise Exception('You must include a phrase to flag')
url = self.API_BASE + self.FLAG_RESPONSE
data = {'phrase': phrase}
if (api_method is not None):
data['apiMethod'] = api_method
if (api_version is not None):
data['apiVersion'] = api_version
if (callback_url is not None):
data['callbackUrl'] = callback_url
data = self._prep_data(data, options)
return self._call(url, data)