wangzhibo
2025-07-27 e0fb1b468f7d607ff78232d250271c46f603c3a9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from __future__ import annotations
from abc import ABC, abstractmethod
from auto_media_publisher.utils.logger import get_logger
import tempfile
import os
import glob
 
class MediaGenerator(ABC):
    def __init__(self):
        self.logger = get_logger(self.__class__.__name__,"admin")
    
 
    @abstractmethod
    def generate(self):
        """所有子类都必须实现的主方法"""
        pass
 
    @classmethod
    def create_by_name(cls, name: str, *args, **kwargs):
        for subclass in cls.__subclasses__():
            if subclass.__name__.lower() == name.lower():
                return subclass(*args, **kwargs)
        raise ValueError(f"未找到名为 '{name}' 的子类")
    
 
    def cleanup_old_temp_files(self):
        temp_dir = tempfile.gettempdir()
        for temp_file in glob.glob(os.path.join(temp_dir, "*.ts")):
            try:
                os.unlink(temp_file)
                self.logger.info(f"Deleted old temp file: {temp_file}")
            except (PermissionError, FileNotFoundError):
                self.logger.error(f"Could not delete old temp file: {temp_file}")