Skip to content

Instantly share code, notes, and snippets.

@y16ra
Created October 4, 2016 08:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save y16ra/3090fea68127ca3eac014f835c1a32ae to your computer and use it in GitHub Desktop.
Save y16ra/3090fea68127ca3eac014f835c1a32ae to your computer and use it in GitHub Desktop.
import json
import requests
import urllib
import sys
from xml.etree import ElementTree
doItAgain = "yes" #Control While loop
def GetToken(): #Get the access token from ADM, token is good for 10 minutes
urlArgs = {
'client_id': 'YOUR CLIENT ID',
'client_secret': 'YOUR CLIENT SECRET',
'scope': 'http://api.microsofttranslator.com',
'grant_type': 'client_credentials'
}
oauthUrl = 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13'
try:
oauthToken = json.loads(requests.post(oauthUrl, data = urllib.urlencode(urlArgs)).content) #make call to get ADM token and parse json
finalToken = "Bearer " + oauthToken['access_token'] #prepare the token
except OSError:
pass
return finalToken
#End GetToken
def GetLang(token, text):
headers = {"Authorization ": token}
detectUrl = "http://api.microsofttranslator.com/v2/Http.svc/Detect?text={}".format(text)
try:
res = requests.get(detectUrl, headers = headers) #make request
detectLang = ElementTree.fromstring(res.text.encode('utf-8'))
print(detectLang.text)
except OSError:
pass
return detectLang.text
def GetTextAndTranslate(finalToken):
fromLangCode = " "
toLangCode = " "
textToTranslate = " "
print " "
print " Language List"
print " Japanese (default)"
print " English"
print " German"
print " Italian"
print " Spanish"
print " French"
#Get the desitination Language
while (toLangCode == " "):
destLang = raw_input("Type the name of a language from the list that you want to translate to: ")
if (destLang == "english") or (destLang == "English"):
toLangCode = "en"
elif (destLang.lower() == "japanese"):
toLangCode = "ja"
elif (destLang == "German") or (destLang == "german"):
toLangCode = "de"
elif (destLang == "Italian") or (destLang == "italian"):
toLangCode = "it"
elif (destLang == "Spanish") or (destLang == "spanish"):
toLangCode = "es"
elif (destLang == "French") or (destLang == "french"):
toLangCode = "fr"
else:
toLangCode = "ja"
print " "
#End while
print " "
textToTranslate = raw_input("Type the text that you want to translate: ")
fromLangCode = GetLang(finalToken, textToTranslate)
print " "
#Call to Microsoft Translator Service
headers = {"Authorization ": finalToken}
translateUrl = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text={}&to={}".format(textToTranslate, toLangCode)
try:
translationData = requests.get(translateUrl, headers = headers) #make request
translation = ElementTree.fromstring(translationData.text.encode('utf-8')) # parse xml return values
print "The translation form {fromLang} to {toLang} :".format(fromLang=fromLangCode, toLang=toLangCode), translation.text #display translation
except OSError:
pass
print " "
#End GetTextAndTranslate()
if __name__ == "__main__":
finalToken = GetToken()
while (doItAgain == 'yes') or (doItAgain == 'Yes'):
GetTextAndTranslate(finalToken)
print ' '
doItAgain = raw_input('Type yes to translate more, any other key to end: ')
#end while
print('Good Bye')
#end main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment