-
-
Save dmitrymomot/1dd904f76c66e874fb67 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
YandexApiKey = 'Your.Yandex.Translate.API.Key.Should.Be.Here' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import config | |
import sys | |
import requests | |
import json | |
import urllib | |
def error(message, status_code): | |
sys.stderr.write(message + '\n') | |
sys.stderr.write('Status code: {0}\n'.format(status_code)) | |
textFromInput = ''.join(sys.stdin.readlines()) | |
urlDetectLanguage = 'https://translate.yandex.net/api/v1.5/tr.json/detect?key={0}&text={1}' | |
urlTranslate = 'https://translate.yandex.net/api/v1.5/tr.json/translate?key={0}&lang={1}&text={2}' | |
requestDetectLanguage = requests.post(urlDetectLanguage.format(config.YandexApiKey, urllib.quote(textFromInput))) | |
if requestDetectLanguage.status_code == 200: | |
detectedLanguage = json.loads(requestDetectLanguage.text)['lang'] | |
langPair = '' | |
if detectedLanguage == 'ru': | |
langPair = 'ru-en' | |
else: | |
langPair = detectedLanguage + '-ru' | |
requestTranslate = requests.post(urlTranslate.format(config.YandexApiKey, langPair, urllib.quote(textFromInput))) | |
if requestTranslate.status_code == 200: | |
translatedText = ''.join(json.loads(requestTranslate.text)['text']) | |
sys.stdout.write(translatedText.encode('utf-8')) | |
else: | |
error('Error with translating', requestTranslate.status_code) | |
else: | |
error('Error with language detect', requestDetectLanguage.status_code) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
TEXT=$(xsel -o) | |
TRANSLATED=`echo $TEXT | python /path/to/previous/script/main.py` | |
notify-send "$TEXT" "$TRANSLATED" -i accessories-dictionary -t 2000 | |
echo $TRANSLATED | xsel --clipboard |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment