Repository navigation
Expand file tree
/
Copy pathMTPythonSampleCode.py
More file actions
134 lines (100 loc) · 3.88 KB
/
Copy pathMTPythonSampleCode.py
File metadata and controls
134 lines (100 loc) · 3.88 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
import websocket
import thread
import json
import requests
import urllib
import wave
import audioop
from time import sleep
import StringIO
import struct
import sys
import codecs
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': 'ENTER YOU CLIENT ID',
'client_secret': 'ENTER 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 GetTextAndTranslate(finalToken):
fromLangCode = " "
toLangCode = " "
textToTranslate = " "
print " "
print " Language List"
print " English"
print " German"
print " Italian"
print " Spanish"
print " French"
#Get the source Language
while (fromLangCode == " "):
sourceLang = raw_input("Type the name of a language from the list that you want to translate from: ")
if (sourceLang == "english") or (sourceLang == "English"):
fromLangCode = "en"
elif (sourceLang == "German") or (sourceLang == "german"):
fromLangCode = "de"
elif (sourceLang == "Italian") or (sourceLang == "italian"):
fromLangCode = "it"
elif (sourceLang == "Spanish") or (sourceLang == "spanish"):
fromLangCode = "es"
elif (sourceLang == "French") or (sourceLang == "french"):
fromLangCode = "fr"
else:
print " "
print "You need to pick a language from the List"
error = raw_input("Press any key to continue")
#End while
print " "
#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 == "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:
print " "
print "You need to pick a language from the List"
error = raw_input("Press any key to continue")
#End while
print " "
textToTranslate = raw_input("Type the text that you want to translate: ")
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 is---> ", 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
goodBye = raw_input('Thank you for using Microsoft Translator, we appreciate it. Good Bye')
#end main