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

Add support for custom aiohttp connector #325

Merged
merged 1 commit into from
Nov 22, 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
20 changes: 10 additions & 10 deletions src/edge_tts/communicate.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def calc_max_mesg_size(tts_config: TTSConfig) -> int:

class Communicate:
"""
Class for communicating with the service.
Communicate with the service.
"""

def __init__(
Expand All @@ -249,17 +249,11 @@ def __init__(
rate: str = "+0%",
volume: str = "+0%",
pitch: str = "+0Hz",
connector: Optional[aiohttp.BaseConnector] = None,
proxy: Optional[str] = None,
connect_timeout: int = 10,
receive_timeout: int = 60,
connect_timeout: Optional[int] = 10,
receive_timeout: Optional[int] = 60,
):
"""
Initializes the Communicate class.

Raises:
ValueError: If the voice is not valid.
"""

# Validate TTS settings and store the TTSConfig object.
self.tts_config = TTSConfig(voice, rate, volume, pitch)

Expand Down Expand Up @@ -290,6 +284,11 @@ def __init__(
sock_read=receive_timeout,
)

# Validate the connector parameter.
if connector is not None and not isinstance(connector, aiohttp.BaseConnector):
raise TypeError("connector must be aiohttp.BaseConnector")
self.connector: Optional[aiohttp.BaseConnector] = connector

# Store current state of TTS.
self.state: Dict[str, Any] = {
"partial_text": None,
Expand Down Expand Up @@ -351,6 +350,7 @@ async def send_ssml_request() -> None:
# Create a new connection to the service.
ssl_ctx = ssl.create_default_context(cafile=certifi.where())
async with aiohttp.ClientSession(
connector=self.connector,
trust_env=True,
timeout=self.session_timeout,
) as session, session.ws_connect(
Expand Down
2 changes: 1 addition & 1 deletion src/edge_tts/submaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(self) -> None:

def add_cue(self, timestamp: Tuple[float, float], text: str) -> None:
"""
Add a subtitle part to the SubMaker object.
Add a cue to the SubMaker object.

Args:
timestamp (tuple): The offset and duration of the subtitle.
Expand Down
7 changes: 5 additions & 2 deletions src/edge_tts/voices.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,24 @@ async def __list_voices(
return data


async def list_voices(*, proxy: Optional[str] = None) -> List[Voice]:
async def list_voices(
*, connector: Optional[aiohttp.BaseConnector] = None, proxy: Optional[str] = None
) -> List[Voice]:
"""
List all available voices and their attributes.

This pulls data from the URL used by Microsoft Edge to return a list of
all available voices.

Args:
connector (Optional[aiohttp.BaseConnector]): The connector to use for the request.
proxy (Optional[str]): The proxy to use for the request.

Returns:
List[Voice]: A list of voices and their attributes.
"""
ssl_ctx = ssl.create_default_context(cafile=certifi.where())
async with aiohttp.ClientSession(trust_env=True) as session:
async with aiohttp.ClientSession(connector=connector, trust_env=True) as session:
try:
data = await __list_voices(session, ssl_ctx, proxy)
except aiohttp.ClientResponseError as e:
Expand Down
Loading