2121# ADDTIONAL IMPORTS
2222import io
2323import time
24- import signal
2524import threading
2625from threading import Timer , Thread
2726import PySimpleGUI as sg
@@ -565,22 +564,24 @@ def find_speech_regions(filename, frame_width=4096, min_region_size=0.3, max_reg
565564 return regions
566565
567566
568- async def GoogleTranslate (text , src , dst ):
567+ def GoogleTranslate (text , src , dst ):
569568 url = 'https://translate.googleapis.com/translate_a/'
570569 params = 'single?client=gtx&sl=' + src + '&tl=' + dst + '&dt=t&q=' + text ;
571- async with httpx .AsyncClient () as client :
572- response = await client .get (url + params )
573- #print('response = {}'.format(response))
574- response_json = response .json ()[0 ]
575- #print('response_json = {}'.format(response_json))
576- length = len (response_json )
577- #print('length = {}'.format(length))
578- translation = ""
579- for i in range (length ):
580- #print("{} {}".format(i, response_json[i][0]))
581- translation = translation + response_json [i ][0 ]
582- return translation
583-
570+ with httpx .Client (http2 = True ) as client :
571+ client .headers .update ({'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)' , 'Referer' : 'https://translate.google.com' ,})
572+ response = client .get (url + params )
573+ #print('response.status_code = {}'.format(response.status_code))
574+ if response .status_code == 200 :
575+ response_json = response .json ()[0 ]
576+ #print('response_json = {}'.format(response_json))
577+ length = len (response_json )
578+ #print('length = {}'.format(length))
579+ translation = ""
580+ for i in range (length ):
581+ #print("{} {}".format(i, response_json[i][0]))
582+ translation = translation + response_json [i ][0 ]
583+ return translation
584+ return
584585
585586class SentenceTranslator (object ):
586587 def __init__ (self , src , dest , patience = - 1 ):
@@ -593,7 +594,9 @@ def __call__(self, sentence):
593594 # handle the special case: empty string.
594595 if not sentence :
595596 return None
596- translated_sentence = asyncio .get_event_loop ().run_until_complete (GoogleTranslate (sentence , src = self .src , dst = self .dest )) # using self made GoogleTranslate2()
597+
598+ translated_sentence = GoogleTranslate (sentence , src = self .src , dst = self .dest )
599+
597600 fail_to_translate = translated_sentence [- 1 ] == '\n '
598601 while fail_to_translate and patience :
599602 translated_sentence = translator .translate (translated_sentence , src = self .src , dest = self .dest ).text
@@ -607,7 +610,6 @@ def __call__(self, sentence):
607610
608611
609612def transcribe (src , dest , filename , subtitle_format , main_window ):
610- #global thread_transcribe, not_transcribing, pool, wav_filename, subtitle_file, translated_subtitle_file, subtitle_folder_name, converter, recognizer, extracted_regions, transcriptions
611613 global thread_transcribe , not_transcribing , pool
612614
613615 if not_transcribing : return
0 commit comments