Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 403 error for list voices endpoint #272

Merged
merged 1 commit into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions src/edge_tts/communicate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
26 changes: 26 additions & 0 deletions src/edge_tts/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
16 changes: 2 additions & 14 deletions src/edge_tts/list_voices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
Loading