Add support for custom aiohttp connector (#325)
This should allow users that need it to add support for SOCKS5 in their application via https://pypi.org/project/aiohttp-socks/. Partially fixes: https://github.com/rany2/edge-tts/issues/147 Signed-off-by: rany <rany2@riseup.net>
This commit is contained in:
@@ -238,7 +238,7 @@ def calc_max_mesg_size(tts_config: TTSConfig) -> int:
|
|||||||
|
|
||||||
class Communicate:
|
class Communicate:
|
||||||
"""
|
"""
|
||||||
Class for communicating with the service.
|
Communicate with the service.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
@@ -249,17 +249,11 @@ class Communicate:
|
|||||||
rate: str = "+0%",
|
rate: str = "+0%",
|
||||||
volume: str = "+0%",
|
volume: str = "+0%",
|
||||||
pitch: str = "+0Hz",
|
pitch: str = "+0Hz",
|
||||||
|
connector: Optional[aiohttp.BaseConnector] = None,
|
||||||
proxy: Optional[str] = None,
|
proxy: Optional[str] = None,
|
||||||
connect_timeout: int = 10,
|
connect_timeout: Optional[int] = 10,
|
||||||
receive_timeout: int = 60,
|
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.
|
# Validate TTS settings and store the TTSConfig object.
|
||||||
self.tts_config = TTSConfig(voice, rate, volume, pitch)
|
self.tts_config = TTSConfig(voice, rate, volume, pitch)
|
||||||
|
|
||||||
@@ -290,6 +284,11 @@ class Communicate:
|
|||||||
sock_read=receive_timeout,
|
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.
|
# Store current state of TTS.
|
||||||
self.state: Dict[str, Any] = {
|
self.state: Dict[str, Any] = {
|
||||||
"partial_text": None,
|
"partial_text": None,
|
||||||
@@ -351,6 +350,7 @@ class Communicate:
|
|||||||
# Create a new connection to the service.
|
# Create a new connection to the service.
|
||||||
ssl_ctx = ssl.create_default_context(cafile=certifi.where())
|
ssl_ctx = ssl.create_default_context(cafile=certifi.where())
|
||||||
async with aiohttp.ClientSession(
|
async with aiohttp.ClientSession(
|
||||||
|
connector=self.connector,
|
||||||
trust_env=True,
|
trust_env=True,
|
||||||
timeout=self.session_timeout,
|
timeout=self.session_timeout,
|
||||||
) as session, session.ws_connect(
|
) as session, session.ws_connect(
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ class SubMaker:
|
|||||||
|
|
||||||
def add_cue(self, timestamp: Tuple[float, float], text: str) -> 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:
|
Args:
|
||||||
timestamp (tuple): The offset and duration of the subtitle.
|
timestamp (tuple): The offset and duration of the subtitle.
|
||||||
|
|||||||
@@ -55,7 +55,9 @@ async def __list_voices(
|
|||||||
return data
|
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.
|
List all available voices and their attributes.
|
||||||
|
|
||||||
@@ -63,13 +65,14 @@ async def list_voices(*, proxy: Optional[str] = None) -> List[Voice]:
|
|||||||
all available voices.
|
all available voices.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
connector (Optional[aiohttp.BaseConnector]): The connector to use for the request.
|
||||||
proxy (Optional[str]): The proxy to use for the request.
|
proxy (Optional[str]): The proxy to use for the request.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
List[Voice]: A list of voices and their attributes.
|
List[Voice]: A list of voices and their attributes.
|
||||||
"""
|
"""
|
||||||
ssl_ctx = ssl.create_default_context(cafile=certifi.where())
|
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:
|
try:
|
||||||
data = await __list_voices(session, ssl_ctx, proxy)
|
data = await __list_voices(session, ssl_ctx, proxy)
|
||||||
except aiohttp.ClientResponseError as e:
|
except aiohttp.ClientResponseError as e:
|
||||||
|
|||||||
Reference in New Issue
Block a user