-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
37 lines (29 loc) · 899 Bytes
/
main.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
import os
import requests
class APIKeyException(BaseException):
"""Не указан API ключ"""
class Translate:
URL = 'https://translate.yandex.net/api/v1.5/tr.json/translate'
PRICE = lambda content: (len(content) / 1000000)*15
KEY = ''
def __init__(self, content, key=None):
self.key = key or Translate.KEY
self.source = content
def get_price(self):
return f'{Translate.PRICE(self.source)*65}'
def translate(self):
if not self.key:
raise APIKeyException
response = requests.get(
self.URL,
params = dict(
key=self.key,
text=self.source,
lang='en-ru',
format='plain'
)
)
if response.ok:
return response.json()
if __name__ == '__main__':
Translate('Hello World').translate()