Last active
May 13, 2019 11:40
-
-
Save zevtyardt/2d5d694601b3209c5b8e3cb8676c8c9d to your computer and use it in GitHub Desktop.
Language translator Bing
This file contains 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
import requests, argparse, re, sys, os | |
id = {'el': 'Greek', 'en-IN': 'English India', 'zh-CHS': 'Chinese (Simplified)', 'zh-CHT': 'Chinese (Traditional)', 'af': 'Afrikaans', 'ko': 'Korean', 'ca': 'Catalan', 'it': 'Italian', 'cy': 'Welsh', 'ar': 'Arabic', 'uk': 'Ukrainian', 'bn-BD': 'Bangla', 'otq': 'Quer\xc3\xa9taro Otomi', 'cs': 'Czech', 'et': 'Estonian', 'id': 'Indonesian', 'es': 'Spanish', 'en-CA': 'English Canada', 'en-GB': 'English United Kingdom', 'ru': 'Russian', 'nl': 'Dutch', 'pt': 'Portuguese', 'no': 'Norwegian Bokm\xc3\xa5l', 'tr': 'Turkish', 'lv': 'Latvian', 'to': 'Tongan', 'lt': 'Lithuanian', 'en': 'English', 'th': 'Thai', 'vi': 'Vietnamese', 'en-US': 'English United States', 'ro': 'Romanian', 'is': 'Icelandic', 'fil': 'Filipino', 'mww': 'Hmong Daw', 'en-AU': 'English Australia', 'fr': 'French', 'bg': 'Bulgarian', 'sm': 'Samoan', 'hr': 'Croatian', 'sr-Cyrl': 'Serbian (Cyrillic)', 'de': 'German', 'mg': 'Malagasy', 'ht': 'Haitian Creole', 'da': 'Danish', 'fa': 'Persian', 'hi': 'Hindi', 'fi': 'Finnish', 'fj': 'Fijian', 'hu': 'Hungarian', 'ja': 'Japanese', 'he': 'Hebrew', 'bs-Latn': 'Bosnian (Latin)', 'te': 'Telugu', 'ta': 'Tamil', 'ty': 'Tahitian', 'sw': 'Kiswahili', 'sv': 'Swedish', 'ur': 'Urdu', 'sk': 'Slovak', 'mt': 'Maltese', 'tlh-Qaak': 'Klingon (plqaD)', 'sr-Latn': 'Serbian (Latin)', 'pl': 'Polish', 'sl': 'Slovenian', 'yua': 'Yucatec Maya', 'tlh': 'Klingon', 'yue': 'Cantonese (Traditional)'} | |
parser = argparse.ArgumentParser() | |
parser.add_argument('q', metavar='text', nargs='*', help='sentence / word / query') | |
parser.add_argument('-f', metavar='form', help='translate from, (default auto)', default='auto') | |
parser.add_argument('-d', metavar='dest', help='language destination, (default en)', default='en') | |
parser.add_argument('-c', '--code', help='language code', action='store_true') | |
arg = parser.parse_args() | |
headers={ | |
"Host": "www.bing.com", | |
"accept": "*/*", | |
"accept-language": "id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7,ms;q=0.6", | |
"content-length": "35", | |
"content-type": "application/x-www-form-urlencoded", | |
"origin": "https://www.bing.com", | |
"referer": "https://www.bing.com/translator", | |
"save-data": "on", | |
"user-agent": "Mozilla/5.0 (Linux; Android 7.0; 5060 Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.109 Mobile Safari/537.36" | |
} | |
reload(sys) | |
sys.setdefaultencoding("utf-8") | |
def main(): | |
if arg.code: | |
for lang in id: | |
print ('{0:<18} {1}'.format('[\x1b[32m%s\x1b[0m]' % lang, id[lang])) | |
elif arg.q: | |
arg.q = ' '.join(arg.q) | |
if os.path.isfile(arg.q): | |
arg.q = open(arg.q).read() | |
if arg.d not in id.keys() or arg.f not in id.keys() + ['auto']: | |
sys.exit('\n\x1b[31merror\x1b[0m invalid language code..\n') | |
if arg.f == 'auto': | |
arg.f = requests.post("https://www.bing.com/tdetect", | |
data={'text': arg.q}, headers=headers).text | |
if arg.f == 'ms': | |
arg.f = 'id' | |
tl = requests.post("https://www.bing.com/ttranslate", | |
data={'text': arg.q, 'from': arg.f, 'to': arg.d}, | |
headers=headers) | |
if len(arg.q) > 500: | |
arg.q = arg.q[:500] + '..' | |
sys.exit("\n{GREEN}{FROM}{RESET} {ORIGINAL}\n ->\n{GREEN}{TO}{RESET} {NEW[translationResponse]}\n".format(GREEN='\x1b[32m', FROM=id[arg.f], RESET='\x1b[0m', ORIGINAL=arg.q, TO=id[arg.d], NEW=tl.json())) | |
else: | |
parser.print_usage() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment