Clean up code comments and docstrings (#318)

Signed-off-by: rany <rany2@riseup.net>
This commit is contained in:
Rany
2024-11-22 18:28:43 +02:00
committed by GitHub
parent d10060fc60
commit 6bc3a9e20f
13 changed files with 40 additions and 54 deletions

View File

@@ -1,8 +1,5 @@
#!/usr/bin/env python3 """The edge_playback package wraps the functionality of mpv and edge-tts to generate
text-to-speech (TTS) using edge-tts and then plays back the generated audio using mpv."""
"""
Init file for the package.
"""
from .__main__ import _main from .__main__ import _main

View File

@@ -1,8 +1,4 @@
#!/usr/bin/env python3 """Main entrypoint for the edge-playback package."""
"""
Playback TTS with subtitles using edge-tts and mpv.
"""
import os import os
import subprocess import subprocess

View File

@@ -1,6 +1,6 @@
""" """edge-tts allows you to use Microsoft Edge's online text-to-speech service without
__init__ for edge_tts needing Windows or the Edge browser. It Microsoft Edge's internal API to generate
""" speech from text."""
from . import exceptions from . import exceptions
from .communicate import Communicate from .communicate import Communicate

View File

@@ -1,6 +1,4 @@
""" """Main entrypoint for the edge-tts package."""
__main__ for edge_tts.
"""
from .util import main from .util import main

View File

@@ -1,6 +1,5 @@
""" """Communicate with the service. Only the Communicate class should be used by
Communicate package. end-users. The other classes and functions are for internal use only."""
"""
import asyncio import asyncio
import concurrent.futures import concurrent.futures

View File

@@ -1,6 +1,4 @@
""" """Constants for the edge_tts package."""
Constants for the Edge TTS project.
"""
BASE_URL = "speech.platform.bing.com/consumer/speech/synthesize/readaloud" BASE_URL = "speech.platform.bing.com/consumer/speech/synthesize/readaloud"
TRUSTED_CLIENT_TOKEN = "6A5AA1D4EAFF4E9FB37E23D68491D6F4" TRUSTED_CLIENT_TOKEN = "6A5AA1D4EAFF4E9FB37E23D68491D6F4"

View File

@@ -1,4 +1,6 @@
"""DRM module for handling DRM operations with clock skew correction.""" """DRM module is used to handle DRM operations with clock skew correction.
Currently the only DRM operation is generating the Sec-MS-GEC token value
used in all API requests to Microsoft Edge's online text-to-speech service."""
import hashlib import hashlib
from datetime import datetime as dt from datetime import datetime as dt
@@ -40,10 +42,10 @@ class DRM:
@staticmethod @staticmethod
def get_unix_timestamp() -> float: def get_unix_timestamp() -> float:
""" """
Gets the current timestamp in Windows file time format with clock skew correction. Gets the current timestamp in Unix format with clock skew correction.
Returns: Returns:
float: The current timestamp in Windows file time format. float: The current timestamp in Unix format with clock skew correction.
""" """
return dt.now(tz.utc).timestamp() + DRM.clock_skew_seconds return dt.now(tz.utc).timestamp() + DRM.clock_skew_seconds
@@ -101,7 +103,7 @@ class DRM:
""" """
Generates the Sec-MS-GEC token value. Generates the Sec-MS-GEC token value.
This function generates a token value based on the current time in Windows file time format, This function generates a token value based on the current time in Windows file time format
adjusted for clock skew, and rounded down to the nearest 5 minutes. The token is then hashed adjusted for clock skew, and rounded down to the nearest 5 minutes. The token is then hashed
using SHA256 and returned as an uppercased hex digest. using SHA256 and returned as an uppercased hex digest.
@@ -112,7 +114,7 @@ class DRM:
https://github.com/rany2/edge-tts/issues/290#issuecomment-2464956570 https://github.com/rany2/edge-tts/issues/290#issuecomment-2464956570
""" """
# Get the current timestamp in Windows file time format with clock skew correction # Get the current timestamp in Unix format with clock skew correction
ticks = DRM.get_unix_timestamp() ticks = DRM.get_unix_timestamp()
# Switch to Windows file time epoch (1601-01-01 00:00:00 UTC) # Switch to Windows file time epoch (1601-01-01 00:00:00 UTC)

View File

@@ -1,28 +1,28 @@
"""Exceptions for the Edge TTS project.""" """Custom exceptions for the edge-tts package."""
class BaseEdgeTTSException(Exception): class EdgeTTSException(Exception):
"""Base exception for the Edge TTS project.""" """Base exception for the edge-tts package."""
class UnknownResponse(BaseEdgeTTSException): class UnknownResponse(EdgeTTSException):
"""Raised when an unknown response is received from the server.""" """Raised when an unknown response is received from the server."""
class UnexpectedResponse(BaseEdgeTTSException): class UnexpectedResponse(EdgeTTSException):
"""Raised when an unexpected response is received from the server. """Raised when an unexpected response is received from the server.
This hasn't happened yet, but it's possible that the server will This hasn't happened yet, but it's possible that the server will
change its response format in the future.""" change its response format in the future."""
class NoAudioReceived(BaseEdgeTTSException): class NoAudioReceived(EdgeTTSException):
"""Raised when no audio is received from the server.""" """Raised when no audio is received from the server."""
class WebSocketError(BaseEdgeTTSException): class WebSocketError(EdgeTTSException):
"""Raised when a WebSocket error occurs.""" """Raised when a WebSocket error occurs."""
class SkewAdjustmentError(BaseEdgeTTSException): class SkewAdjustmentError(EdgeTTSException):
"""Raised when an error occurs while adjusting the clock skew.""" """Raised when an error occurs while adjusting the clock skew."""

View File

@@ -1,4 +1,5 @@
"""Models for the Edge TTS module.""" """This module contains the TTSConfig dataclass, which represents the
internal TTS configuration for edge-tts's Communicate class."""
import re import re
from dataclasses import dataclass from dataclasses import dataclass
@@ -7,7 +8,7 @@ from dataclasses import dataclass
@dataclass @dataclass
class TTSConfig: class TTSConfig:
""" """
Represents the internal TTS configuration for Edge TTS's communicate class. Represents the internal TTS configuration for edge-tts's Communicate class.
""" """
voice: str voice: str

View File

@@ -1,9 +1,4 @@
""" """SubMaker module is used to generate subtitles from WordBoundary events."""
SubMaker package for the Edge TTS project.
SubMaker is a package that makes the process of creating subtitles with
information provided by the service easier.
"""
import math import math
from typing import List, Tuple from typing import List, Tuple
@@ -37,20 +32,23 @@ def mktimestamp(time_unit: float) -> str:
class SubMaker: class SubMaker:
""" """
SubMaker class SubMaker is used to generate subtitles from WordBoundary messages.
""" """
def __init__(self) -> None: def __init__(self) -> None:
""" """
SubMaker constructor. SubMaker constructor initializes the list of subtitles and the list of offsets.
Returns:
None
""" """
self.offset: List[Tuple[float, float]] = [] self.offset: List[Tuple[float, float]] = []
self.subs: List[str] = [] self.subs: List[str] = []
def create_sub(self, timestamp: Tuple[float, float], text: str) -> None: def create_sub(self, timestamp: Tuple[float, float], text: str) -> None:
""" """
create_sub creates a subtitle with the given timestamp and text create_sub creates a subtitle from the given timestamp and text,
and adds it to the list of subtitles and appends it to the list of subtitles.
Args: Args:
timestamp (tuple): The offset and duration of the subtitle. timestamp (tuple): The offset and duration of the subtitle.

View File

@@ -1,6 +1,4 @@
""" """Utility functions for the command line interface. Used by the main module."""
Main package.
"""
import argparse import argparse
import asyncio import asyncio

View File

@@ -1,4 +1,4 @@
"""Edge TTS version information.""" """Version information for the edge_tts package."""
__version__ = "6.1.19" __version__ = "6.1.19"
__version_info__ = tuple(int(num) for num in __version__.split(".")) __version_info__ = tuple(int(num) for num in __version__.split("."))

View File

@@ -1,6 +1,5 @@
""" """This module contains functions to list all available voices and a class to find the
list_voices package for edge_tts. correct voice based on their attributes."""
"""
import json import json
import ssl import ssl