Skip to content

Instantly share code, notes, and snippets.

@nikhilkumarsingh
Created April 30, 2017 12:56
Show Gist options
  • Save nikhilkumarsingh/c8267a28cf869d10db61578ad4c5c8d1 to your computer and use it in GitHub Desktop.
Save nikhilkumarsingh/c8267a28cf869d10db61578ad4c5c8d1 to your computer and use it in GitHub Desktop.
Googl text to speech API demo
import requests
API_ENDPOINT = "https://translate.google.com/translate_tts"
headers = {'User-Agent': "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0"}
text = "good morning"
params = {
'ie': 'UTF-8',
'q': text,
'tl': 'en',
'client': 'gtx'
}
r = requests.get(url, params = params, headers = headers)
print(r.status_code)
with open("clip.mp3", 'wb') as f:
f.write(r.content)
'''
Supported languages:
LANG_MAP = {'swahili': 'sw', 'russian': 'ru', 'ukrainian': 'uk', 'macedonian': 'mk', 'kannada': 'kn', 'hebrew': 'iw', 'chinese simplified': 'zh-CN', 'latvian': 'lv', 'icelandic': 'is', 'hungarian': 'hu', 'czech': 'cs', 'turkish': 'tr', 'esperanto': 'eo', 'irish': 'ga', 'danish': 'da', 'german': 'de', 'bulgarian': 'bg', 'basque': 'eu', 'filipino': 'tl', 'maltese': 'mt', 'thai': 'th', 'haitian creole': 'ht', 'english': 'en', 'galician': 'gl', 'chinese traditional': 'zh-TW', 'yiddish': 'yi', 'polish': 'pl', 'vietnamese': 'vi', 'belarusian': 'be', 'portuguese': 'pt', 'welsh': 'cy', 'croatian': 'hr', 'korean': 'ko', 'bengali': 'bn', 'urdu': 'ur', 'slovak': 'sk', 'azerbaijani': 'az', 'georgian': 'ka', 'afrikaans': 'af', 'norwegian': 'no', 'telugu': 'te', 'lithuanian': 'lt', 'persian': 'fa', 'estonian': 'et', 'gujarati': 'gu', 'hindi': 'hi', 'albanian': 'sq', 'tamil': 'ta', 'indonesian': 'id', 'serbian': 'sr', 'swedish': 'sv', 'french': 'fr', 'malay': 'ms', 'spanish': 'es', 'dutch': 'nl', 'italian': 'it', 'finnish': 'fi', 'arabic': 'ar', 'slovenian': 'sl', 'latin': 'la', 'japanese': 'ja', 'catalan': 'ca', 'greek': 'el', 'romanian': 'ro'}
'''
@adyavanapalli
Copy link

Line 6,

r = requests.get(url, params = params, headers = headers)

should be

r = requests.get(API_ENDPOINT, params = params, headers = headers)

@innfinite4evr
Copy link

Using a paragraph instead of two words is possible?

@porimol
Copy link

porimol commented Jun 12, 2018

@innfinite4evr
yes, it is possible too :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment