forked from ByeongGil-Jung/2018-Jaram-Winter-Workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
translator.py
43 lines (39 loc) · 1.13 KB
/
translator.py
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
# -*- coding: utf-8 -*-
import urllib.parse
import requests
from TranslateBot import data as d
def translator(input):
inputtext = input.split(":")
batype = inputtext[0]
btext = inputtext[1]
if batype == "영한":
blanguage = "en"
alanguage = "ko"
elif batype == "한영":
blanguage = "ko"
alanguage = "en"
else:
return None
client_id = d.NaverUrls.client_id.value
client_secret = d.NaverUrls.client_secret.value
data = {
'source': blanguage,
'target': alanguage,
'text': btext
}
url = "https://openapi.naver.com/v1/papago/n2mt"
response = requests.post(url, data=data, headers={
'X-Naver-Client-Id': client_id,
'X-Naver-Client-Secret': client_secret
})
rescode = response.status_code
if rescode == 200:
response_body = response.json()
# print(response_body.decode('utf-8'))
print(response_body)
atext = response_body['message']['result']['translatedText']
return atext
else:
print("Error Code:", rescode)
print("번역실패")
return None