Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: CSCI 195
Views: 5930
Image: ubuntu2004
1
import requests
2
import json
3
4
from getpass import getpass as get_api_key
5
6
'''
7
For documentation on how this request must be structured and
8
what the response will look like, see
9
funtranslations.com/api/yoda
10
'''
11
12
# Note to Prof. McFall: API Key is stored in RoboForm entry
13
# for funtranslations.com
14
apikey = get_api_key(prompt='Enter your API key')
15
16
url = 'http://api.funtranslations.com/translate/yoda.json'
17
18
english_text = raw_input('Enter the text you want yoda-fied:\n')
19
20
response = requests.get(url, params={'text': english_text}, headers={'X-FunTranslations-Api-Secret': apikey})
21
22
'''
23
Here's the example JSON response that is document at the URL
24
funtranslations.com/api/yoda
25
{
26
"success": {
27
"total": 1
28
},
29
"contents": {
30
"translated": "Lost a planet, master obiwan has. ",
31
"text": "Master Obiwan has lost a planet.",
32
"translation": "yoda"
33
}
34
}
35
'''
36
decodedResponse = json.loads(response.content)
37
38
successIndicator = decodedResponse['success']
39
numberSuccessful = successIndicator['total']
40
if numberSuccessful != 1:
41
print "Unable to successfully translate", english, "to yoda"
42
else:
43
contents = decodedResponse['contents']
44
print 'The translation of', contents['text'], 'is ' + '"' + contents['translated'] + '"'
45
# Note we could have gotten the translation with
46
# translation = decodedResponse['contents']['translated']
47