From 5a860edd27a6772d7facc384927e3b87439e2ccd Mon Sep 17 00:00:00 2001 From: Pierre Nicolas Durette Date: Fri, 28 Apr 2023 20:30:26 -0700 Subject: [PATCH] fix(cli): add deprecated language fallback support to CLI fix: add error case for when a custom tld gives a 404 docs(cli): use correct lang code and valid tld --- docs/cli.rst | 4 ++-- gtts/cli.py | 5 ++++- gtts/tts.py | 4 +++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/docs/cli.rst b/docs/cli.rst index 39745a20..408dcde1 100644 --- a/docs/cli.rst +++ b/docs/cli.rst @@ -24,9 +24,9 @@ Read "c'est la vie" in French to ``cestlavie.mp3``:: $ gtts-cli "c'est la vie" --lang fr --output cestlavie.mp3 -Read '你好' to ``你好.mp3`` (in Mandarin, using google.cn):: +Read '你好' to ``你好.mp3`` (in Mandarin, using google.com.hk):: - $ gtts-cli '你好' --tld cn --lang zh-cn --output 你好.mp3 + $ gtts-cli '你好' --tld .com.hk --lang zh-CN --output 你好.mp3 Read 'slow' slowly to ``slow.mp3``:: diff --git a/gtts/cli.py b/gtts/cli.py index d99bb9f6..f3026c63 100644 --- a/gtts/cli.py +++ b/gtts/cli.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- from gtts import gTTS, gTTSError, __version__ -from gtts.lang import tts_langs +from gtts.lang import tts_langs, _fallback_deprecated_lang import click import logging import logging.config @@ -46,6 +46,9 @@ def validate_lang(ctx, param, lang): if ctx.params["nocheck"]: return lang + # Fallback from deprecated language if needed + lang = _fallback_deprecated_lang(lang) + try: if lang not in tts_langs(): raise click.UsageError( diff --git a/gtts/tts.py b/gtts/tts.py index 9a87a6af..9496bd44 100644 --- a/gtts/tts.py +++ b/gtts/tts.py @@ -42,7 +42,7 @@ class gTTS: can produce different localized 'accents' for a given language. This is also useful when ``google.com`` might be blocked within a network but a local or different Google host - (e.g. ``google.cn``) is not. Default is ``com``. + (e.g. ``google.com.hk``) is not. Default is ``com``. lang (string, optional): The language (IETF language tag) to read the text in. Default is ``en``. slow (bool, optional): Reads text more slowly. Defaults to ``False``. @@ -364,6 +364,8 @@ def infer_msg(self, tts, rsp=None): if status == 403: cause = "Bad token or upstream API changes" + elif status == 404 and tts.tld != "com": + cause = "Unsupported tld '{}'".format(tts.tld) elif status == 200 and not tts.lang_check: cause = ( "No audio stream in response. Unsupported language '%s'"