上传完整项目文件
This commit is contained in:
1 parent
e63d58df8d
commit
aa0dd45839
8663 files changed
+2393125
No files matched your search
@@ -0,0 +1,15 @@
|
||||
"""数据模型模块
|
||||
|
||||
包含下载任务模型和设置模型。
|
||||
"""
|
||||
|
||||
from .task import DownloadTask, TaskStatus, DownloadType
|
||||
from .settings import Settings, ThemeMode
|
||||
|
||||
__all__ = [
|
||||
"DownloadTask",
|
||||
"TaskStatus",
|
||||
"DownloadType",
|
||||
"Settings",
|
||||
"ThemeMode",
|
||||
]
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,120 @@
|
||||
"""应用设置数据模型
|
||||
|
||||
管理所有用户配置项,包括下载目录、并发数、主题等。
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
from typing import Optional
|
||||
|
||||
|
||||
class ThemeMode(Enum):
|
||||
"""主题模式枚举"""
|
||||
LIGHT = "浅色"
|
||||
DARK = "深色"
|
||||
SYSTEM = "跟随系统"
|
||||
|
||||
|
||||
@dataclass
|
||||
class Settings:
|
||||
"""应用设置数据模型
|
||||
|
||||
包含所有可配置的应用设置项,支持 JSON 序列化与反序列化。
|
||||
"""
|
||||
# ========== 下载设置 ==========
|
||||
download_dir: str = "" # 下载保存目录,空字符串表示使用默认目录
|
||||
|
||||
# ========== 并发设置 ==========
|
||||
max_workers: int = 4 # 最大并发下载数 (1-32)
|
||||
|
||||
# ========== 格式设置 ==========
|
||||
default_video_format: str = "best" # 默认视频格式
|
||||
default_audio_format: str = "mp3" # 默认音频格式
|
||||
default_subtitle_lang: str = "zh" # 默认字幕语言
|
||||
default_subtitle_format: str = "srt" # 默认字幕格式
|
||||
auto_subtitle: bool = True # 是否自动下载字幕
|
||||
auto_merge: bool = True # 是否自动合并音视频
|
||||
|
||||
# ========== 外观设置 ==========
|
||||
theme: ThemeMode = ThemeMode.DARK # 主题模式
|
||||
|
||||
# ========== 高级设置 ==========
|
||||
ffmpeg_path: str = "" # FFmpeg 可执行文件路径,空字符串表示自动检测
|
||||
cookies_from_browser: str = "auto" # 浏览器名称(或逗号分隔列表),"auto"=自动尝试所有,留空=不使用
|
||||
cookies_file: str = "" # Cookie 文件路径(Netscape 格式),优先于 cookies_from_browser
|
||||
proxy: str = "" # 代理服务器地址
|
||||
max_retries: int = 3 # 失败重试次数
|
||||
rate_limit: str = "" # 下载限速,如 "1M", "500K",空字符串表示不限速
|
||||
|
||||
# ========== 界面设置 ==========
|
||||
window_width: int = 1200
|
||||
window_height: int = 800
|
||||
window_x: int = -1 # -1 表示居中
|
||||
window_y: int = -1
|
||||
|
||||
# ========== 其他设置 ==========
|
||||
language: str = "zh_CN" # 界面语言
|
||||
check_update: bool = True # 启动时检查更新
|
||||
minimize_to_tray: bool = False # 关闭时最小化到系统托盘
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
"""将设置序列化为字典"""
|
||||
return {
|
||||
"download_dir": self.download_dir,
|
||||
"max_workers": self.max_workers,
|
||||
"default_video_format": self.default_video_format,
|
||||
"default_audio_format": self.default_audio_format,
|
||||
"default_subtitle_lang": self.default_subtitle_lang,
|
||||
"default_subtitle_format": self.default_subtitle_format,
|
||||
"auto_subtitle": self.auto_subtitle,
|
||||
"auto_merge": self.auto_merge,
|
||||
"theme": self.theme.name,
|
||||
"ffmpeg_path": self.ffmpeg_path,
|
||||
"cookies_from_browser": self.cookies_from_browser,
|
||||
"cookies_file": self.cookies_file,
|
||||
"proxy": self.proxy,
|
||||
"max_retries": self.max_retries,
|
||||
"rate_limit": self.rate_limit,
|
||||
"window_width": self.window_width,
|
||||
"window_height": self.window_height,
|
||||
"window_x": self.window_x,
|
||||
"window_y": self.window_y,
|
||||
"language": self.language,
|
||||
"check_update": self.check_update,
|
||||
"minimize_to_tray": self.minimize_to_tray,
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: dict) -> "Settings":
|
||||
"""从字典反序列化创建设置对象"""
|
||||
settings = cls()
|
||||
|
||||
# 简单字段映射
|
||||
str_fields = [
|
||||
"download_dir", "default_video_format", "default_audio_format",
|
||||
"default_subtitle_lang", "default_subtitle_format",
|
||||
"ffmpeg_path", "cookies_from_browser", "cookies_file", "proxy", "rate_limit", "language",
|
||||
]
|
||||
for field_name in str_fields:
|
||||
if field_name in data:
|
||||
setattr(settings, field_name, data[field_name])
|
||||
|
||||
int_fields = ["max_workers", "max_retries", "window_width",
|
||||
"window_height", "window_x", "window_y"]
|
||||
for field_name in int_fields:
|
||||
if field_name in data:
|
||||
setattr(settings, field_name, int(data[field_name]))
|
||||
|
||||
bool_fields = ["auto_subtitle", "auto_merge", "check_update", "minimize_to_tray"]
|
||||
for field_name in bool_fields:
|
||||
if field_name in data:
|
||||
setattr(settings, field_name, bool(data[field_name]))
|
||||
|
||||
# 主题枚举
|
||||
if "theme" in data:
|
||||
try:
|
||||
settings.theme = ThemeMode[data["theme"]]
|
||||
except (KeyError, ValueError):
|
||||
settings.theme = ThemeMode.DARK
|
||||
|
||||
return settings
|
||||
+258
@@ -0,0 +1,258 @@
|
||||
"""下载任务数据模型
|
||||
|
||||
定义下载任务的所有状态、属性和数据结构。
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
from typing import Optional
|
||||
from datetime import datetime
|
||||
import uuid
|
||||
|
||||
|
||||
class TaskStatus(Enum):
|
||||
"""任务状态枚举"""
|
||||
WAITING = "等待中"
|
||||
DOWNLOADING = "下载中"
|
||||
MERGING = "合并中"
|
||||
COMPLETED = "已完成"
|
||||
FAILED = "失败"
|
||||
PAUSED = "已暂停"
|
||||
CANCELLED = "已取消"
|
||||
|
||||
|
||||
class DownloadType(Enum):
|
||||
"""下载类型枚举"""
|
||||
VIDEO = "视频"
|
||||
AUDIO = "音频"
|
||||
SUBTITLE = "字幕"
|
||||
THUMBNAIL = "封面"
|
||||
|
||||
|
||||
@dataclass
|
||||
class VideoInfo:
|
||||
"""视频信息数据结构"""
|
||||
title: str = ""
|
||||
author: str = ""
|
||||
duration: int = 0 # 秒
|
||||
upload_date: str = "" # YYYYMMDD 格式
|
||||
thumbnail_url: str = ""
|
||||
description: str = ""
|
||||
webpage_url: str = ""
|
||||
view_count: int = 0
|
||||
like_count: int = 0
|
||||
|
||||
@property
|
||||
def duration_str(self) -> str:
|
||||
"""格式化时长为 HH:MM:SS"""
|
||||
if not self.duration:
|
||||
return "未知"
|
||||
duration = int(self.duration) # yt-dlp 可能返回 float
|
||||
hours, remainder = divmod(duration, 3600)
|
||||
minutes, seconds = divmod(remainder, 60)
|
||||
if hours > 0:
|
||||
return f"{hours}:{minutes:02d}:{seconds:02d}"
|
||||
return f"{minutes}:{seconds:02d}"
|
||||
|
||||
@property
|
||||
def upload_date_str(self) -> str:
|
||||
"""格式化上传日期为 YYYY-MM-DD"""
|
||||
if not self.upload_date or len(self.upload_date) != 8:
|
||||
return self.upload_date or "未知"
|
||||
return f"{self.upload_date[:4]}-{self.upload_date[4:6]}-{self.upload_date[6:8]}"
|
||||
|
||||
|
||||
@dataclass
|
||||
class VideoFormat:
|
||||
"""视频格式数据结构"""
|
||||
format_id: str = ""
|
||||
ext: str = "" # 文件扩展名,如 mp4, webm
|
||||
resolution: str = "" # 如 1080p
|
||||
video_codec: str = "" # 如 avc1, vp9
|
||||
audio_codec: str = "" # 如 mp4a, opus
|
||||
fps: int = 0 # 帧率
|
||||
file_size: int = 0 # 字节
|
||||
tbr: float = 0.0 # 总比特率 (kbps)
|
||||
vbr: float = 0.0 # 视频比特率 (kbps)
|
||||
abr: float = 0.0 # 音频比特率 (kbps)
|
||||
has_video: bool = True
|
||||
has_audio: bool = True
|
||||
is_audio_only: bool = False
|
||||
note: str = "" # 格式备注,如 "Premium", "HDR"
|
||||
|
||||
@property
|
||||
def resolution_int(self) -> int:
|
||||
"""获取分辨率高度数值"""
|
||||
if not self.resolution:
|
||||
return 0
|
||||
try:
|
||||
return int(self.resolution.lower().replace('p', ''))
|
||||
except ValueError:
|
||||
return 0
|
||||
|
||||
@property
|
||||
def file_size_str(self) -> str:
|
||||
"""格式化文件大小"""
|
||||
if not self.file_size:
|
||||
return "未知大小"
|
||||
size = self.file_size
|
||||
for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
|
||||
if size < 1024:
|
||||
return f"{size:.1f} {unit}"
|
||||
size /= 1024
|
||||
return f"{size:.1f} PB"
|
||||
|
||||
@property
|
||||
def display_name(self) -> str:
|
||||
"""用于 UI 显示的格式名称"""
|
||||
parts = []
|
||||
# 分辨率/类型
|
||||
if self.is_audio_only:
|
||||
parts.append(self.ext.upper())
|
||||
elif self.resolution:
|
||||
parts.append(self.resolution.upper())
|
||||
else:
|
||||
parts.append(self.ext.upper())
|
||||
|
||||
# 编码
|
||||
if self.video_codec:
|
||||
parts.append(self.video_codec.upper())
|
||||
if self.audio_codec and not self.is_audio_only:
|
||||
parts.append(self.audio_codec.upper())
|
||||
|
||||
# 帧率 (仅视频)
|
||||
if self.fps and not self.is_audio_only:
|
||||
parts.append(f"{self.fps}FPS")
|
||||
|
||||
# 文件大小
|
||||
parts.append(self.file_size_str)
|
||||
|
||||
# 备注
|
||||
if self.note:
|
||||
parts.append(self.note)
|
||||
|
||||
return " | ".join(parts)
|
||||
|
||||
|
||||
@dataclass
|
||||
class SubtitleInfo:
|
||||
"""字幕信息数据结构"""
|
||||
language: str = "" # 语言代码,如 zh, en
|
||||
language_name: str = "" # 语言名称,如 中文, English
|
||||
is_auto: bool = False # 是否为自动生成字幕
|
||||
ext: str = "vtt" # 字幕格式
|
||||
url: str = ""
|
||||
|
||||
|
||||
@dataclass
|
||||
class DownloadProgress:
|
||||
"""下载进度数据结构"""
|
||||
downloaded_bytes: int = 0
|
||||
total_bytes: int = 0
|
||||
speed: float = 0.0 # 字节/秒
|
||||
percent: float = 0.0 # 0-100
|
||||
eta: int = 0 # 预计剩余秒数
|
||||
status: str = "" # 进度状态描述
|
||||
|
||||
@property
|
||||
def speed_str(self) -> str:
|
||||
"""格式化下载速度"""
|
||||
if not self.speed:
|
||||
return "计算中..."
|
||||
speed = self.speed
|
||||
for unit in ['B/s', 'KB/s', 'MB/s', 'GB/s']:
|
||||
if speed < 1024:
|
||||
return f"{speed:.1f} {unit}"
|
||||
speed /= 1024
|
||||
return f"{speed:.1f} TB/s"
|
||||
|
||||
@property
|
||||
def downloaded_str(self) -> str:
|
||||
"""格式化已下载大小"""
|
||||
size = self.downloaded_bytes
|
||||
for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
|
||||
if size < 1024:
|
||||
return f"{size:.1f} {unit}"
|
||||
size /= 1024
|
||||
return f"{size:.1f} PB"
|
||||
|
||||
@property
|
||||
def total_str(self) -> str:
|
||||
"""格式化总大小"""
|
||||
if not self.total_bytes:
|
||||
return "未知"
|
||||
size = self.total_bytes
|
||||
for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
|
||||
if size < 1024:
|
||||
return f"{size:.1f} {unit}"
|
||||
size /= 1024
|
||||
return f"{size:.1f} PB"
|
||||
|
||||
@property
|
||||
def eta_str(self) -> str:
|
||||
"""格式化剩余时间"""
|
||||
if not self.eta:
|
||||
return "计算中..."
|
||||
eta = self.eta
|
||||
if eta < 60:
|
||||
return f"{eta} 秒"
|
||||
elif eta < 3600:
|
||||
return f"{eta // 60} 分 {eta % 60} 秒"
|
||||
else:
|
||||
hours = eta // 3600
|
||||
minutes = (eta % 3600) // 60
|
||||
return f"{hours} 时 {minutes} 分"
|
||||
|
||||
|
||||
@dataclass
|
||||
class DownloadTask:
|
||||
"""下载任务数据模型
|
||||
|
||||
封装单个下载任务的所有信息,包括视频信息、格式选择、
|
||||
进度跟踪和状态管理。
|
||||
"""
|
||||
# 唯一标识
|
||||
task_id: str = field(default_factory=lambda: str(uuid.uuid4())[:8])
|
||||
|
||||
# URL 信息
|
||||
url: str = ""
|
||||
is_playlist: bool = False
|
||||
|
||||
# 视频信息
|
||||
video_info: Optional[VideoInfo] = None
|
||||
|
||||
# 下载配置
|
||||
selected_format: Optional[VideoFormat] = None
|
||||
download_video: bool = True
|
||||
download_audio: bool = False
|
||||
download_subtitle: bool = False
|
||||
download_thumbnail: bool = False
|
||||
subtitle_language: str = "zh" # 字幕语言代码
|
||||
subtitle_format: str = "srt" # 字幕文件格式
|
||||
subtitle_auto: bool = True # 是否使用自动字幕
|
||||
thumbnail_format: str = "jpg" # 封面图格式
|
||||
|
||||
# 输出配置
|
||||
output_dir: str = ""
|
||||
output_filename: str = ""
|
||||
|
||||
# 状态
|
||||
status: TaskStatus = TaskStatus.WAITING
|
||||
progress: DownloadProgress = field(default_factory=DownloadProgress)
|
||||
error_message: str = ""
|
||||
|
||||
# 时间戳
|
||||
created_at: datetime = field(default_factory=datetime.now)
|
||||
started_at: Optional[datetime] = None
|
||||
completed_at: Optional[datetime] = None
|
||||
|
||||
# 额外数据
|
||||
extra_data: dict = field(default_factory=dict)
|
||||
|
||||
def reset_progress(self) -> None:
|
||||
"""重置进度信息"""
|
||||
self.progress = DownloadProgress()
|
||||
self.status = TaskStatus.WAITING
|
||||
self.error_message = ""
|
||||
self.started_at = None
|
||||
self.completed_at = None
|
||||
Reference in new issue
Block a user