-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtranslator.py
38 lines (32 loc) · 1.04 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
from http import client
import os
import sys
import json
import urllib.request
with open('SECRETS.json') as r:
secrets = json.load(r)
client_id = secrets['client_id']
client_secret = secrets['client_secret']
URL = "https://openapi.naver.com/v1/papago/n2mt"
client_id = client_id
client_secret = client_secret
def main():
text = "hello hi hi i'm papago"
source = 'en'
target = 'ko'
encText = urllib.parse.quote(text)
data = f"source={source}&target={target}&text=" + encText
request = urllib.request.Request(URL)
request.add_header("X-Naver-Client-Id",client_id)
request.add_header("X-Naver-Client-Secret",client_secret)
response = urllib.request.urlopen(request, data=data.encode("utf-8"))
rescode = response.getcode()
if(rescode==200):
response_body = response.read()
decode = json.loads(response_body.decode('utf-8'))
result = decode['message']['result']['translatedText']
print(result)
else:
print("Error Code:" + rescode)
if __name__ == "__main__":
main()