lushan88a / google_trans_new

A free and unlimited python API for google translate.
MIT License
393 stars 170 forks source link

detect_lang not working #60

Open vedansh5 opened 1 year ago

vedansh5 commented 1 year ago

I am using the new updated file but it's now showing error in detect_lang the error is google_trans_new.py, line 240, in detect, raise Exception Exception

me-suzy commented 1 year ago

I have the same issue:

image

regishmc commented 1 year ago

google_new_transError: 404 (Not Found) from TTS API. Probable cause: Unknown

napatnicky commented 1 year ago

Did anyone can fix it? -)

qingshuizheng commented 1 year ago

i believe the error originates from the shutdown of translate.google.cn. change it to translate.google.com or anything else might work. note: not tested.

direct-up commented 1 year ago

我调了调detect函数里面的json解析部分,截至2023.8.17已经能检测语言了,大家把下面这一部分替换一下即可:

def detect(self, text):
    text = str(text)
    if len(text) >= 5000:
        return log.debug("Warning: Can only detect less than 5000 characters")
    if len(text) == 0:
        return ""
    headers = {
        "Referer": "http://translate.google.{}/".format(self.url_suffix),
        "User-Agent":
            "Mozilla/5.0 (Windows NT 10.0; WOW64) "
            "AppleWebKit/537.36 (KHTML, like Gecko) "
            "Chrome/47.0.2526.106 Safari/537.36",
        "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
    }
    freq = self._package_rpc(text)
    response = requests.Request(method='POST',
                                url=self.url,
                                data=freq,
                                headers=headers)
    try:
        if self.proxies == None or type(self.proxies) != dict:
            self.proxies = {}
        with requests.Session() as s:
            s.proxies = self.proxies
            r = s.send(request=response.prepare(),
                       verify=False,
                       timeout=self.timeout)

        for line in r.iter_lines(chunk_size=1024):
            decoded_line = line.decode('utf-8')
            if "MkEWBc" in decoded_line:
                # regex_str = r"\[\[\"wrb.fr\",\"MkEWBc\",\"\[\[(.*).*?,\[\[\["
                try:
                    response = json.loads(decoded_line)  # decoded_line是str格式,转化成json列表或者字典按位置提取
                    response = json.loads(response[0][2])  # 提取的内容你那个仍然是str格式,需要继续转化成json列表或者字典按位置提取
                    response_ = list(response)
                    detect_lang = response[0][2]
                except Exception:
                    raise Exception
                # data_got = data_got.split('\\\"]')[0]
                return [detect_lang, LANGUAGES[detect_lang.lower()]]
        r.raise_for_status()
    except requests.exceptions.HTTPError as e:
        # Request successful, bad response
        log.debug(str(e))
        raise google_new_transError(tts=self, response=r)
    except requests.exceptions.RequestException as e:
        # Request failed
        log.debug(str(e))
        raise google_new_transError(tts=self)
Alex-Songs commented 5 months ago

@direct-up 请问404问题怎么解决呢?

chrk623 commented 3 months ago

@direct-up 请问404问题怎么解决呢?

Try translator = google_translator(url_suffix="com")