Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bing Translation service now requires use of an access token #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions out.strings
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* example string to localize */
"greeting" = "Connectez-vous sur MyCochlear"
"greeting" = "Se connecter sur mon programme"
/*
I
am
Expand All @@ -8,4 +8,4 @@ multiline
comment.
Followed by another key/value pair
*/
"coffee" = "Je café de l'amour"
"coffee" = "J'ai aiment le café"
59 changes: 47 additions & 12 deletions tstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
SPEECH=chr(34)

api_url = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate"
app_id = ''
token_url = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"
access_token = ''
client_id = ''
client_secret = ''


def _unicode_urlencode(params):
"""
Expand All @@ -28,6 +32,26 @@ def _unicode_urlencode(params):
return urllib.urlencode([(k, isinstance(v, unicode) and v.encode('utf-8') or v) for k, v in params])


def _generate_token():
args = {
'grant_type': 'client_credentials',
'client_id': client_id,
'client_secret': client_secret,
'scope': 'http://api.microsofttranslator.com'
}
data = urllib.urlencode(args)
sock = urllib.urlopen(token_url, data)
result = sock.read()
if result.startswith(codecs.BOM_UTF8):
result = result.lstrip(codecs.BOM_UTF8).decode('utf-8')
elif result.startswith(codecs.BOM_UTF16_LE):
result = result.lstrip(codecs.BOM_UTF16_LE).decode('utf-16-le')
elif result.startswith(codecs.BOM_UTF16_BE):
result = result.lstrip(codecs.BOM_UTF16_BE).decode('utf-16-be')

return json.loads(result)


def _run_query(args):
"""
takes arguments and optional language argument and runs query on server
Expand All @@ -36,26 +60,34 @@ def _run_query(args):
sock = urllib.urlopen(api_url + '?' + data)
result = sock.read()
if result.startswith(codecs.BOM_UTF8):
result = result.lstrip(codecs.BOM_UTF8).decode('utf-8')
result = result.lstrip(codecs.BOM_UTF8).decode('utf-8')
elif result.startswith(codecs.BOM_UTF16_LE):
result = result.lstrip(codecs.BOM_UTF16_LE).decode('utf-16-le')
elif result.startswith(codecs.BOM_UTF16_BE):
result = result.lstrip(codecs.BOM_UTF16_BE).decode('utf-16-be')
return json.loads(result)

def set_app_id(new_app_id):
global app_id
app_id = new_app_id
def set_client_id(new_client_id):
global client_id
client_id = new_client_id

def set_client_secret(new_client_secret):
global client_secret
client_secret = new_client_secret

def set_access_token(new_access_token):
global access_token
access_token = new_access_token

def translate(text, source, target, html=False):
"""
action=opensearch
"""
if not app_id:
raise ValueError("AppId needs to be set by set_app_id")
if not access_token:
raise ValueError("App token isn't set. Check values of client_id and client_secret")

query_args = {
'appId': app_id,
'appId': "Bearer " + access_token,
'text': text,
'from': source,
'to': target,
Expand Down Expand Up @@ -117,7 +149,7 @@ def read_from_file(self, fname=None):
for c in comments:
self.strings.append(c)
#I'm not sure at this time of night how to migrate the regex symbols across but lucky I know what they are :-P
self.strings.append(SPEECH + string.key + SPEECH + " = " + SPEECH + translatedstring + SPEECH+'\n')
self.strings.append(SPEECH + string.key + SPEECH + " = " + SPEECH + translatedstring + SPEECH+';\n')
self.strings_d[string.key] = string

f.close()
Expand Down Expand Up @@ -161,9 +193,12 @@ def main():
parser.add_argument('-d',help='Dest Language (e.g fr,ko,ru etc -Check bing',required=False,default='fr')

Options = parser.parse_args()

#brads bing id using for translation
set_app_id('2A9444C210A043010CB9FC4AB8B6DE797AE88790')

set_client_id('<CLIENT ID>')
set_client_secret('<CLIENT SECRET>')
token = _generate_token()

set_access_token(token["access_token"])
#load in strings file
LF=LocalizedFile(translate,Options.i,True,Options.s,Options.d)
LF.save_to_file(Options.o)
Expand Down