-
Notifications
You must be signed in to change notification settings - Fork 2
/
lymbix.py
140 lines (111 loc) · 4.42 KB
/
lymbix.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
import urllib
import urllib2
import json
class Lymbix:
API_BASE = 'https://gyrus.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 authentication_key == None or len(authentication_key) == 0:
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
''' 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 articles == None or len(articles) == 0:
raise Exception('You must include articles to tonalize')
url = self.API_BASE + self.TONALIZE_MULTIPLE
data = {'articles': articles}
if options != None: data.update(options)
for key, value in data.iteritems(): data[key] = json.dumps(value)
data = urllib.urlencode(data)
headers = self._get_headers()
request = urllib2.Request(url, data, headers)
response = urllib2.urlopen(request)
return json.loads(response.read())
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 article == None or len(article) == 0:
raise Exception('You must include an article to tonalize')
url = self.API_BASE + self.TONALIZE_DETAILED
data = {'article': article}
if options != None: data.update(options)
for key, value in data.iteritems(): data[key] = json.dumps(value)
data = urllib.urlencode(data)
headers = self._get_headers()
request = urllib2.Request(url, data, headers)
response = urllib2.urlopen(request)
return json.loads(response.read())
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 article == None or len(article) == 0:
raise Exception('You must include an article to tonalize')
url = self.API_BASE + self.TONALIZE
data = {'article': article}
if options != None: data.update(options)
for key, value in data.iteritems(): data[key] = json.dumps(value)
data = urllib.urlencode(data)
headers = self._get_headers()
request = urllib2.Request(url, data, headers)
response = urllib2.urlopen(request)
return json.loads(response.read())
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 phrase == None or len(phrase) == 0:
raise Exception('You must include a phrase to flag')
url = self.API_BASE + self.FLAG_RESPONSE
data = {'phrase': phrase}
if (api_method != None): data['apiMethod'] = api_method
if (api_version != None): data['apiVersion'] = api_version
if (callback_url != None): data['callbackUrl'] = callback_url
if (options != None): data.update(options)
for key, value in data.iteritems(): data[key] = json.dumps(value)
data = urllib.urlencode(data)
headers = self._get_headers()
request = urllib2.Request(url, data, headers)
response = urllib2.urlopen(request)
return response.read()