-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathed
52 lines (42 loc) · 1.28 KB
/
ed
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():
english_word = ""
arguments = sys.argv
if len(arguments) == 1:
exit("usage: de ENGLISH-WORD")
elif len(arguments) == 2:
english_word = arguments[1]
return english_word
def send_request(english_word):
url = "https://translate.yandex.net/api/v1.5/tr.json/translate"
translation_direction = "en-de"
data = {
"text": english_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
english_word = extract_arguments()
response = send_request(english_word)
words = get_translated_words(response)
print "Results:\n"
for word in words:
print word + "\n"