fixes with attrs.attrib mostly

pull/963/head
Drugsosos 2 years ago
parent 6a2b448c73
commit 720511168c
No known key found for this signature in database
GPG Key ID: 8E35176FE617E28D

@ -3,6 +3,7 @@ import requests
from requests.adapters import HTTPAdapter, Retry from requests.adapters import HTTPAdapter, Retry
from attr import attrs, attrib from attr import attrs, attrib
from attr.validators import instance_of
from TTS.common import BaseApiTTS, get_random_voice from TTS.common import BaseApiTTS, get_random_voice
@ -62,15 +63,15 @@ voices['non_eng'] = [
# more or less: en_us_rocket, en_us_ghostface # more or less: en_us_rocket, en_us_ghostface
@attrs(auto_attribs=True) @attrs
class TikTok(BaseApiTTS): # TikTok Text-to-Speech Wrapper class TikTok(BaseApiTTS): # TikTok Text-to-Speech Wrapper
random_voice: bool = False random_voice: bool = attrib(
uri_base: str = attrib( validator=instance_of(bool),
default='https://api16-normal-useast5.us.tiktokv.com/media/api/text/speech/invoke/', default=False
kw_only=True,
) )
max_chars = 300 uri_base: str = 'https://api16-normal-useast5.us.tiktokv.com/media/api/text/speech/invoke/'
decode_base64 = True max_chars: int = 300
decode_base64: bool = True
def make_request( def make_request(
self, self,

@ -4,7 +4,8 @@ from botocore.exceptions import BotoCoreError, ClientError, ProfileNotFound
import sys import sys
from utils import settings from utils import settings
from attr import attrs from attr import attrs, attrib
from attr.validators import instance_of
from TTS.common import get_random_voice from TTS.common import get_random_voice
@ -28,9 +29,12 @@ voices = [
] ]
@attrs(auto_attribs=True) @attrs
class AWSPolly: class AWSPolly:
random_voice: bool = False random_voice: bool = attrib(
validator=instance_of(bool),
default=False
)
max_chars: int = 0 max_chars: int = 0
def run( def run(

@ -17,26 +17,21 @@ from TTS.TikTok import TikTok
from TTS.aws_polly import AWSPolly from TTS.aws_polly import AWSPolly
@attrs(auto_attribs=True) @attrs
class TTSEngine: class TTSEngine:
"""Calls the given TTS engine to reduce code duplication and allow multiple TTS engines. """Calls the given TTS engine to reduce code duplication and allow multiple TTS engines.
Args: Args:
tts_module : The TTS module. Your module should handle the TTS itself and saving to the given path under the run method. tts_module : The TTS module. Your module should handle the TTS itself and saving to the given path under the run method.
reddit_object : The reddit object that contains the posts to read. reddit_object : The reddit object that contains the posts to read.
path (Optional) : The unix style path to save the mp3 files to. This must not have leading or trailing slashes.
max_length (Optional) : The maximum length of the mp3 files in total.
Notes: Notes:
tts_module must take the arguments text and filepath. tts_module must take the arguments text and filepath.
""" """
tts_module: Union[GTTS, StreamlabsPolly, TikTok, AWSPolly] tts_module: Union[GTTS, StreamlabsPolly, TikTok, AWSPolly] = attrib()
reddit_object: dict reddit_object: dict = attrib()
path: str = 'assets/temp/mp3' __path: str = 'assets/temp/mp3'
__total_length: int = attrib( __total_length: int = 0
default=0,
kw_only=True
)
def __attrs_post_init__(self): def __attrs_post_init__(self):
# Calls an instance of the tts_module class # Calls an instance of the tts_module class
@ -59,12 +54,12 @@ class TTSEngine:
Returns: Returns:
Indexes of comments to be used in the final video Indexes of comments to be used in the final video
""" """
Path(self.path).mkdir(parents=True, exist_ok=True) Path(self.__path).mkdir(parents=True, exist_ok=True)
# This file needs to be removed in case this post does not use post text # This file needs to be removed in case this post does not use post text
# so that it won't appear in the final video # so that it won't appear in the final video
try: try:
Path(f'{self.path}/posttext.mp3').unlink() Path(f'{self.__path}/posttext.mp3').unlink()
except OSError: except OSError:
pass pass
@ -109,10 +104,10 @@ class TTSEngine:
self.tts_module.run( self.tts_module.run(
text=self.process_text(text), text=self.process_text(text),
filepath=f'{self.path}/{filename}.mp3' filepath=f'{self.__path}/{filename}.mp3'
) )
clip_length = audio_length(f'{self.path}/{filename}.mp3') clip_length = audio_length(f'{self.__path}/{filename}.mp3')
clip_offset = self.time_between_pictures + self.time_before_tts * 2 clip_offset = self.time_between_pictures + self.time_before_tts * 2
if clip_length and self.__total_length + clip_length + clip_offset <= self.max_length: if clip_length and self.__total_length + clip_length + clip_offset <= self.max_length:

@ -2,6 +2,7 @@ import requests
from requests.exceptions import JSONDecodeError from requests.exceptions import JSONDecodeError
from utils import settings from utils import settings
from attr import attrs, attrib from attr import attrs, attrib
from attr.validators import instance_of
from TTS.common import BaseApiTTS, get_random_voice from TTS.common import BaseApiTTS, get_random_voice
from utils.voice import check_ratelimit from utils.voice import check_ratelimit
@ -28,15 +29,14 @@ voices = [
# valid voices https://lazypy.ro/tts/ # valid voices https://lazypy.ro/tts/
@attrs(auto_attribs=True) @attrs
class StreamlabsPolly(BaseApiTTS): class StreamlabsPolly(BaseApiTTS):
random_voice: bool = False random_voice: bool = attrib(
url: str = attrib( validator=instance_of(bool),
default='https://streamlabs.com/polly/speak', default=False
kw_only=True,
) )
url: str = 'https://streamlabs.com/polly/speak',
max_chars = 550 max_chars: int = 550
def make_request( def make_request(
self, self,

@ -9,7 +9,6 @@ from moviepy.editor import (
VideoFileClip, VideoFileClip,
AudioFileClip, AudioFileClip,
ImageClip, ImageClip,
concatenate_videoclips,
CompositeAudioClip, CompositeAudioClip,
CompositeVideoClip, CompositeVideoClip,
) )
@ -35,7 +34,7 @@ def name_normalize(
name = re.sub(r'(\d+)\s?\/\s?(\d+)', r'\1 of \2', name) name = re.sub(r'(\d+)\s?\/\s?(\d+)', r'\1 of \2', name)
name = re.sub(r'(\w+)\s?\/\s?(\w+)', r'\1 or \2', name) name = re.sub(r'(\w+)\s?\/\s?(\w+)', r'\1 or \2', name)
name = re.sub(r'\/', '', name) name = re.sub(r'\/', '', name)
name[:30] # the hell this little guy does? # name[:30] # the hell this little guy does? commented until explained
lang = settings.config['reddit']['thread']['post_lang'] lang = settings.config['reddit']['thread']['post_lang']
if lang: if lang:
@ -44,9 +43,7 @@ def name_normalize(
print_substep('Translating filename...') print_substep('Translating filename...')
translated_name = ts.google(name, to_language=lang) translated_name = ts.google(name, to_language=lang)
return translated_name return translated_name
return name
else:
return name
def make_final_video( def make_final_video(

@ -46,7 +46,7 @@ class ExceptionDecorator:
import logging import logging
logger = logging.getLogger('webdriver_log') logger = logging.getLogger('webdriver_log')
logger.setLevel(logging.DEBUG) logger.setLevel(logging.ERROR)
handler = logging.FileHandler('.webdriver.log', mode='a+', encoding='utf-8') handler = logging.FileHandler('.webdriver.log', mode='a+', encoding='utf-8')
logger.addHandler(handler) logger.addHandler(handler)

Loading…
Cancel
Save