Skip to content

Commit 5bae039

Browse files
committed
Python3 compatibility
1 parent a13011a commit 5bae039

File tree

2 files changed

+32
-35
lines changed

2 files changed

+32
-35
lines changed

__init__.py

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,7 @@
1212

1313
__all__ = ['Translator', 'TranslateApiException']
1414

15-
try:
16-
import simplejson as json
17-
from simplejson import JSONDecodeError
18-
except ImportError:
19-
import json
20-
class JSONDecodeError(Exception): pass
21-
# Ugly: No alternative because this exception class doesnt seem to be there
22-
# in the standard python module
23-
import urllib
24-
import urllib2
15+
import requests
2516
import warnings
2617
import logging
2718

@@ -96,15 +87,16 @@ def get_access_token(self):
9687
9788
:return: The access token to be used with subsequent requests
9889
"""
99-
args = urllib.urlencode({
90+
args = {
10091
'client_id': self.client_id,
10192
'client_secret': self.client_secret,
10293
'scope': self.scope,
10394
'grant_type': self.grant_type
104-
})
105-
response = json.loads(urllib.urlopen(
106-
'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13', args
107-
).read())
95+
}
96+
response = requests.post(
97+
'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13',
98+
data=args
99+
).json()
108100

109101
self.logger.debug(response)
110102

@@ -121,18 +113,20 @@ def call(self, url, params):
121113
if not self.access_token:
122114
self.access_token = self.get_access_token()
123115

124-
request = urllib2.Request(
125-
"%s?%s" % (url, urllib.urlencode(params)),
116+
resp = requests.get(
117+
"%s" % url,
118+
params=params,
126119
headers={'Authorization': 'Bearer %s' % self.access_token}
127120
)
128-
response = urllib2.urlopen(request).read()
129-
rv = json.loads(response.decode("UTF-8-sig"))
121+
resp.encoding = 'UTF-8-sig'
122+
rv = resp.json()
123+
#rv = json.loads(response.decode("UTF-8-sig"))
130124

131-
if isinstance(rv, basestring) and \
125+
if isinstance(rv, str) and \
132126
rv.startswith("ArgumentOutOfRangeException"):
133127
raise ArgumentOutOfRangeException(rv)
134128

135-
if isinstance(rv, basestring) and \
129+
if isinstance(rv, str) and \
136130
rv.startswith("TranslateApiException"):
137131
raise TranslateApiException(rv)
138132

@@ -170,23 +164,23 @@ def translate_array(self, texts, to_lang, from_lang=None, **options):
170164
"""Translates an array of text strings from one language to another.
171165
172166
:param texts: A list containing texts for translation.
173-
:param to_lang: A string representing the language code to
167+
:param to_lang: A string representing the language code to
174168
translate the text into.
175-
:param from_lang: A string representing the language code of the
176-
translation text. If left None the response will include the
169+
:param from_lang: A string representing the language code of the
170+
translation text. If left None the response will include the
177171
result of language auto-detection. (Default: None)
178-
:param options: A TranslateOptions element containing the values below.
172+
:param options: A TranslateOptions element containing the values below.
179173
They are all optional and default to the most common settings.
180174
181-
Category: A string containing the category (domain) of the
175+
Category: A string containing the category (domain) of the
182176
translation. Defaults to "general".
183-
ContentType: The format of the text being translated. The
184-
supported formats are "text/plain" and "text/html". Any
177+
ContentType: The format of the text being translated. The
178+
supported formats are "text/plain" and "text/html". Any
185179
HTML needs to be well-formed.
186-
Uri: A string containing the content location of this
180+
Uri: A string containing the content location of this
187181
translation.
188182
User: A string used to track the originator of the submission.
189-
State: User state to help correlate request and response. The
183+
State: User state to help correlate request and response. The
190184
same contents will be returned in the response.
191185
"""
192186
options = {

setup.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
"""
33
Microsoft translator API
44
5-
The Microsoft Translator services can be used in web or client
6-
applications to perform language translation operations. The services
7-
support users who are not familiar with the default language of a page or
8-
application, or those desiring to communicate with people of a different
5+
The Microsoft Translator services can be used in web or client
6+
applications to perform language translation operations. The services
7+
support users who are not familiar with the default language of a page or
8+
application, or those desiring to communicate with people of a different
99
language group.
1010
1111
This module implements the AJAX API for the Microsoft Translator service.
@@ -17,7 +17,7 @@
1717
>>> print translator.translate("Hello", "pt")
1818
"Olá"
1919
20-
The documentation for the service can be obtained here:
20+
The documentation for the service can be obtained here:
2121
http://msdn.microsoft.com/en-us/library/ff512423.aspx
2222
2323
The project is hosted on GitHub where your could fork the project or report
@@ -59,4 +59,7 @@ def read(fname):
5959
"Topic :: Utilities"
6060
],
6161
test_suite = "microsofttranslator.test.test_all",
62+
install_requires=[
63+
'requests >= 1.2.3',
64+
]
6265
)

0 commit comments

Comments
 (0)