diff --git a/src/edge_tts/communicate.py b/src/edge_tts/communicate.py index b345d66..6d28f52 100644 --- a/src/edge_tts/communicate.py +++ b/src/edge_tts/communicate.py @@ -27,7 +27,7 @@ import aiohttp import certifi -from .constants import WSS_URL +from .constants import WSS_HEADERS, WSS_URL from .exceptions import ( NoAudioReceived, UnexpectedResponse, @@ -369,15 +369,7 @@ async def send_ssml_request() -> None: f"{WSS_URL}&ConnectionId={connect_id()}", compress=15, proxy=self.proxy, - headers={ - "Pragma": "no-cache", - "Cache-Control": "no-cache", - "Origin": "chrome-extension://jdiccldimpdaibmpdkjnbmckianbfold", - "Accept-Encoding": "gzip, deflate, br", - "Accept-Language": "en-US,en;q=0.9", - "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36" - " (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0", - }, + headers=WSS_HEADERS, ssl=ssl_ctx, ) as websocket: # Send the request to the service. diff --git a/src/edge_tts/constants.py b/src/edge_tts/constants.py index ab62b28..60f8919 100644 --- a/src/edge_tts/constants.py +++ b/src/edge_tts/constants.py @@ -7,3 +7,29 @@ WSS_URL = f"wss://{BASE_URL}/edge/v1?TrustedClientToken={TRUSTED_CLIENT_TOKEN}" VOICE_LIST = f"https://{BASE_URL}/voices/list?trustedclienttoken={TRUSTED_CLIENT_TOKEN}" + +CHROMIUM_MAJOR_VERSION = "129" +BASE_HEADERS = { + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36" + f" (KHTML, like Gecko) Chrome/${CHROMIUM_MAJOR_VERSION}.0.0.0 Safari/537.36" + f" Edg/${CHROMIUM_MAJOR_VERSION}.0.0.0", + "Accept-Encoding": "gzip, deflate, br", + "Accept-Language": "en-US,en;q=0.9", +} +WSS_HEADERS = { + "Pragma": "no-cache", + "Cache-Control": "no-cache", + "Origin": "chrome-extension://jdiccldimpdaibmpdkjnbmckianbfold", +} +WSS_HEADERS.update(BASE_HEADERS) +VOICE_HEADERS = { + "Authority": "speech.platform.bing.com", + "Sec-CH-UA": f'" Not;A Brand";v="99", "Microsoft Edge";v="{CHROMIUM_MAJOR_VERSION}",' + f' "Chromium";v="{CHROMIUM_MAJOR_VERSION}"', + "Sec-CH-UA-Mobile": "?0", + "Accept": "*/*", + "Sec-Fetch-Site": "none", + "Sec-Fetch-Mode": "cors", + "Sec-Fetch-Dest": "empty", +} +VOICE_HEADERS.update(BASE_HEADERS) diff --git a/src/edge_tts/list_voices.py b/src/edge_tts/list_voices.py index 9b703f8..a18859f 100644 --- a/src/edge_tts/list_voices.py +++ b/src/edge_tts/list_voices.py @@ -9,7 +9,7 @@ import aiohttp import certifi -from .constants import VOICE_LIST +from .constants import VOICE_HEADERS, VOICE_LIST async def list_voices(*, proxy: Optional[str] = None) -> Any: @@ -26,19 +26,7 @@ async def list_voices(*, proxy: Optional[str] = None) -> Any: async with aiohttp.ClientSession(trust_env=True) as session: async with session.get( VOICE_LIST, - headers={ - "Authority": "speech.platform.bing.com", - "Sec-CH-UA": '" Not;A Brand";v="99", "Microsoft Edge";v="91", "Chromium";v="91"', - "Sec-CH-UA-Mobile": "?0", - "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " - "(KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36 Edg/91.0.864.41", - "Accept": "*/*", - "Sec-Fetch-Site": "none", - "Sec-Fetch-Mode": "cors", - "Sec-Fetch-Dest": "empty", - "Accept-Encoding": "gzip, deflate, br", - "Accept-Language": "en-US,en;q=0.9", - }, + headers=VOICE_HEADERS, proxy=proxy, ssl=ssl_ctx, ) as url: