(self, query_text: str, from_language: str = 'auto', to_language: str = 'en', **kwargs)
return data if is_detail_result else ' '.join([x[0] for x in data[1][0][0][5]])
https://translate.google.com, https://translate.google.cn.
        :param query_text: str, must.
        :param from_language: str, default 'auto'.
        :param to_language: str, default 'en'.
        :param **kwargs:
                :param reset_host_url: str, default None. eg: 'https://translate.google.fr'
                :param if_use_cn_host: boolean, default None. affected by `reset_host_url`.
                :param if_ignore_limit_of_length: boolean, default False.
                :param is_detail_result: boolean, default False.
                :param timeout: float, default None.
                :param proxies: dict, default None.
                :param sleep_seconds: float, default `random.random()`.
        :return: str or list
https://translate.google.com, https://translate.google.cn.
        :param query_text: str, must.
        :param from_language: str, default 'auto'.
        :param to_language: str, default 'en'.
        :param **kwargs:
                :param reset_host_url: str, default None. eg: 'https://translate.google.fr'
                :param if_use_cn_host: boolean, default None. affected by `reset_host_url`.
                :param if_ignore_limit_of_length: boolean, default False.
                :param is_detail_result: boolean, default False.
                :param timeout: float, default None.
                :param proxies: dict, default None.
                :param sleep_seconds: float, default `random.random()`.
        :return: str or list
https : // translate . google . com https : // translate . google . cn . : param query_text : str must . : param from_language : str default auto . : param to_language : str default en . : param ** kwargs : : param reset_host_url : str default None . eg : https : // translate . google . fr : param if_use_cn_host : boolean default None . affected by reset_host_url . : param if_ignore_limit_of_length : boolean default False . : param is_detail_result : boolean default False . : param timeout : float default None . : param proxies : dict default None . : param sleep_seconds : float default random . random () . : return : str or list
def google_api(self, query_text: str, from_language: str = 'auto', to_language: str = 'en', **kwargs) -> Union[str, list]:
        """
        https://translate.google.com, https://translate.google.cn.
        :param query_text: str, must.
        :param from_language: str, default 'auto'.
        :param to_language: str, default 'en'.
        :param **kwargs:
                :param reset_host_url: str, default None. eg: 'https://translate.google.fr'
                :param if_use_cn_host: boolean, default None. affected by `reset_host_url`.
                :param if_ignore_limit_of_length: boolean, default False.
                :param is_detail_result: boolean, default False.
                :param timeout: float, default None.
                :param proxies: dict, default None.
                :param sleep_seconds: float, default `random.random()`.
        :return: str or list
        """
        reset_host_url = kwargs.get('reset_host_url', None)
        if reset_host_url:
            assert reset_host_url[:25] == 'https://translate.google.'
            self.host_url = reset_host_url
        else:
            use_cn_condition = kwargs.get('if_use_cn_host', None) or self.request_server_region_info.get('countryCode')=='CN'
            self.host_url = self.cn_host_url if use_cn_condition else self.en_host_url
        self.api_url = f'{self.host_url}/_/TranslateWebserverUi/data/batchexecute'

        self.host_headers = self.host_headers or self.get_headers(self.cn_host_url, if_api=False)  # reuse cookie header
        self.api_headers = self.get_headers(self.cn_host_url, if_api=True, if_referer_for_host=True, if_ajax_for_api=True)
        is_detail_result = kwargs.get('is_detail_result', False)
        timeout = kwargs.get('timeout', None)
        proxies = kwargs.get('proxies', None)
        sleep_seconds = kwargs.get('sleep_seconds', random.random())
        if_ignore_limit_of_length = kwargs.get('if_ignore_limit_of_length', False)
        query_text = self.check_query_text(query_text, if_ignore_limit_of_length)
        delete_temp_language_map_label = 0

        with requests.Session() as ss:
            r = ss.get(self.host_url, headers=self.host_headers, timeout=timeout, proxies=proxies)
            if 'consent.google.com' == urllib.parse.urlparse(r.url).hostname:
                self.host_headers.update({'cookie': self.get_consent_cookie(r.text)})
                host_html = ss.get(self.host_url, headers=self.host_headers, timeout=timeout, proxies=proxies).text
            else:
                host_html = r.text

            if not self.language_map:
                self.language_map = self.get_language_map(host_html)
            if not self.language_map:
                delete_temp_language_map_label += 1
                self.language_map = self.make_temp_language_map(from_language, to_language)
            from_language, to_language = self.check_language(from_language, to_language, self.language_map, output_zh=self.output_zh)

            rpc_data = self.get_rpc(query_text, from_language, to_language)
            rpc_data = urllib.parse.urlencode(rpc_data)
            r = ss.post(self.api_url, headers=self.api_headers, data=rpc_data, timeout=timeout, proxies=proxies)
            r.raise_for_status()
            json_data = json.loads(r.text[6:])
            data = json.loads(json_data[0][2])

        if delete_temp_language_map_label != 0:
            self.language_map = None
        time.sleep(sleep_seconds)
        self.query_count += 1
        return data if is_detail_result else ' '.join([x[0] for x in data[1][0][0][5]])
def google_api ( self , query_text : str , from_language : str = 'auto' , to_language : str = 'en' , * * kwargs ) -> Union [ str , list ] : reset_host_url = kwargs . get ( 'reset_host_url' , None ) if reset_host_url : assert reset_host_url [ : 25 ] == 'https://translate.google.' self . host_url = reset_host_url else : use_cn_condition = kwargs . get ( 'if_use_cn_host' , None ) or self . request_server_region_info . get ( 'countryCode' ) == 'CN' self . host_url = self . cn_host_url if use_cn_condition else self . en_host_url self . api_url = f'{self.host_url}/_/TranslateWebserverUi/data/batchexecute' self . host_headers = self . host_headers or self . get_headers ( self . cn_host_url , if_api = False ) # reuse cookie header self . api_headers = self . get_headers ( self . cn_host_url , if_api = True , if_referer_for_host = True , if_ajax_for_api = True ) is_detail_result = kwargs . get ( 'is_detail_result' , False ) timeout = kwargs . get ( 'timeout' , None ) proxies = kwargs . get ( 'proxies' , None ) sleep_seconds = kwargs . get ( 'sleep_seconds' , random . random ( ) ) if_ignore_limit_of_length = kwargs . get ( 'if_ignore_limit_of_length' , False ) query_text = self . check_query_text ( query_text , if_ignore_limit_of_length ) delete_temp_language_map_label = 0 with requests . Session ( ) as ss : r = ss . get ( self . host_url , headers = self . host_headers , timeout = timeout , proxies = proxies ) if 'consent.google.com' == urllib . parse . urlparse ( r . url ) . hostname : self . host_headers . update ( { 'cookie' : self . get_consent_cookie ( r . text ) } ) host_html = ss . get ( self . host_url , headers = self . host_headers , timeout = timeout , proxies = proxies ) . text else : host_html = r . text if not self . language_map : self . language_map = self . get_language_map ( host_html ) if not self . language_map : delete_temp_language_map_label += 1 self . language_map = self . make_temp_language_map ( from_language , to_language ) from_language , to_language = self . check_language ( from_language , to_language , self . language_map , output_zh = self . output_zh ) rpc_data = self . get_rpc ( query_text , from_language , to_language ) rpc_data = urllib . parse . urlencode ( rpc_data ) r = ss . post ( self . api_url , headers = self . api_headers , data = rpc_data , timeout = timeout , proxies = proxies ) r . raise_for_status ( ) json_data = json . loads ( r . text [ 6 : ] ) data = json . loads ( json_data [ 0 ] [ 2 ] ) if delete_temp_language_map_label != 0 : self . language_map = None time . sleep ( sleep_seconds ) self . query_count += 1 return data if is_detail_result else ' ' . join ( [ x [ 0 ] for x in data [ 1 ] [ 0 ] [ 0 ] [ 5 ] ] )