-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathde
52 lines (42 loc) · 1.27 KB
/
de
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
#!/usr/bin/python
import sys
import requests
import requests.exceptions
def extract_arguments():
german_word = ""
arguments = sys.argv
if len(arguments) == 1:
exit("usage: de GERMAN-WORD")
elif len(arguments) == 2:
german_word = arguments[1]
return german_word
def send_request(german_word):
url = "https://translate.yandex.net/api/v1.5/tr.json/translate"
translation_direction = "de-en"
data = {
"text": german_word,
"lang": translation_direction,
"key": "trnsl.1.1.20160315T195419Z.3ed65dac0f5446de.c53b3cd387a69efaecdacffd1066c2f010522c2a"
}
response = {"code": 404}
try:
response = requests.post(url, data=data)
except ConnectionError:
exit("host not reachable")
else:
response = response.json()
return response
def get_translated_words(response):
translated_words = []
status_code = response.get("code", 200)
if status_code != 200:
exit("Yandex Error: " + status_code)
for eng_word in response["text"]:
translated_words.append(eng_word)
return translated_words
german_word = extract_arguments()
response = send_request(german_word)
words = get_translated_words(response)
print "Results:\n"
for word in words:
print word + "\n"