Skip to content

Commit

Permalink
refactor(extractor.py): 重构视频下载地址提取逻辑
Browse files Browse the repository at this point in the history
修复已知问题
  • Loading branch information
JoeanAmier committed Dec 25, 2024
1 parent 49d113b commit 3fc4c67
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 61 deletions.
45 changes: 23 additions & 22 deletions docs/Release_Notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,29 @@
15. 恢复账号详细数据采集功能
16. 优化作品文件名称处理逻辑
17. 优化文件名称非法字符处理
18. 优化 earliest 参数设置规则
19. 适配最新版 FFmpeg 命令
20. 日志不再记录请求体数据
21. 捕获中断程序的异常错误
22. 优化重定向链接获取逻辑
23. 更新 proxy 参数设置规则
24. 优化检查版本更新功能
25. 优化请求参数编码处理
26. 优化音乐文件格式判断
27. 新增文件断点续传功能
28. 修复删除下载记录功能
29. 捕获响应码异常的错误
30. 优化文件名称长度限制
31. 优化程序运行状态提示
32. 更新网络接口请求参数
33. 适配新版本 HTTPX 库
34. 移除直播内置下载器
35. 新增英语语言支持
36. 优化文件下载功能
37. 优化代理测试逻辑
38. 修复其他已知问题
39. 优化代码运行逻辑
18. 重构视频下载地址提取逻辑
19. 优化 earliest 参数设置规则
20. 适配最新版 FFmpeg 命令
21. 日志不再记录请求体数据
22. 捕获中断程序的异常错误
23. 优化重定向链接获取逻辑
24. 更新 proxy 参数设置规则
25. 优化检查版本更新功能
26. 优化请求参数编码处理
27. 优化音乐文件格式判断
28. 新增文件断点续传功能
29. 修复删除下载记录功能
30. 捕获响应码异常的错误
31. 优化文件名称长度限制
32. 优化程序运行状态提示
33. 更新网络接口请求参数
34. 适配新版本 HTTPX 库
35. 移除直播内置下载器
36. 新增英语语言支持
37. 优化文件下载功能
38. 优化代理测试逻辑
39. 修复其他已知问题
40. 优化代码运行逻辑

<p><strong>注意:<code>5.5</code> 版本的配置文件与旧版本配置文件不兼容;旧版本程序更新后,建议重命名旧版本配置文件,然后运行程序生成默认配置文件,手动迁移配置文件内容;不兼容的参数:<code>browser_info</code>、<code>browser_info_tiktok</code>,其余参数无需处理!</strong></p>

Expand Down
4 changes: 2 additions & 2 deletions src/application/TikTokDownloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ async def main_menu(self, mode=None, ):
[i for i, __ in self.__function_menu],
self.console,
separate=(
4,
9,
5,
10,
))
await self.compatible(mode)
mode = None
Expand Down
23 changes: 15 additions & 8 deletions src/application/main_complete.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ async def account_acquisition_interactive_tiktok(
select="",
):
await self.__secondary_menu(
_("请选择账号链接来源"),
function=self.__function_account_tiktok,
select=select or safe_pop(self.run_command),
)
Expand All @@ -214,6 +215,7 @@ async def account_acquisition_interactive(
select="",
):
await self.__secondary_menu(
_("请选择账号链接来源"),
function=self.__function_account,
select=select or safe_pop(self.run_command),
)
Expand Down Expand Up @@ -290,7 +292,7 @@ async def __account_detail_batch(
count.success += 1
if index != len(accounts):
await suspend(index, self.console)
self.__summarize_results(count)
self.__summarize_results(count, _("账号"), )

async def check_sec_user_id(self, sec_user_id: str, tiktok=False, ) -> str:
match tiktok:
Expand Down Expand Up @@ -373,7 +375,7 @@ async def __account_detail_handle(
count.success += 1
if index != len(links):
await suspend(index, self.console)
self.__summarize_results(count)
self.__summarize_results(count, _("账号"), )

async def deal_account_detail(
self,
Expand Down Expand Up @@ -702,8 +704,10 @@ async def __detail_secondary_menu(self, menu, select="", *args, **kwargs):
async with logger(root, console=self.console, **params) as record:
if not select:
select = choose(
_("请选择作品链接来源"), [
i[0] for i in menu], self.console)
_("请选择作品链接来源"),
[i[0] for i in menu],
self.console,
)
if select.upper() == "Q":
self.running = False
try:
Expand Down Expand Up @@ -1063,7 +1067,7 @@ async def mix_inquire_collection(self) -> list[str]:
return self.input_download_index(data)

def input_download_index(self, data: list[dict]) -> list[str] | None:
if d := self.__input_download_index(data):
if d := self.__input_download_index(data, _("收藏合集"), ):
return [i["id"] for i in d]

def __input_download_index(
Expand Down Expand Up @@ -1137,7 +1141,7 @@ async def __mix_handle(
count.success += 1
if index != len(ids):
await suspend(index, self.console)
self.__summarize_results(count, _("合集"))
self.__summarize_results(count, _("合集"), )

async def mix_batch(self, ):
await self.__mix_batch(
Expand Down Expand Up @@ -1186,7 +1190,7 @@ async def __mix_batch(
count.success += 1
if index != len(mix):
await suspend(index, self.console)
self.__summarize_results(count, _("合集"))
self.__summarize_results(count, _("合集"), )

async def _deal_mix_detail(
self,
Expand Down Expand Up @@ -1314,6 +1318,7 @@ async def _deal_user_data(
@check_storage_format
async def user_interactive(self, select="", *args, **kwargs):
await self.__secondary_menu(
_("请选择账号链接来源"),
function=self.__function_user,
select=select or safe_pop(self.run_command),
)
Expand Down Expand Up @@ -1632,7 +1637,9 @@ async def run(self, run_command: list):
select = choose(
_("请选择采集功能"),
[i for i, __ in self.__function],
self.console)
self.console,
(10,),
)
if select in {"Q", "q", }:
self.running = False
try:
Expand Down
2 changes: 1 addition & 1 deletion src/config/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def __init__(
self.dynamic_cover = self.__check_bool(dynamic_cover)
self.original_cover = self.__check_bool(original_cover)
self.timeout = self.__check_timeout(timeout)
self.proxy: str | None = self.__check_proxy(proxy)
self.proxy: str | None = self.__check_proxy(proxy, remark=_("抖音"), )
self.proxy_tiktok: str | None = self.__check_proxy_tiktok(proxy_tiktok)
self.download = self.__check_bool(download)
self.max_size = self.__check_max_size(max_size)
Expand Down
1 change: 0 additions & 1 deletion src/custom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
SEARCH_AVATAR_INDEX,
MUSIC_COLLECTION_COVER_INDEX,
MUSIC_COLLECTION_DOWNLOAD_INDEX,
VIDEO_BIT_RATE_INDEX,
)
from .static import (
MAX_WORKERS,
Expand Down
1 change: 0 additions & 1 deletion src/custom/internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@

BLANK_PREVIEW = "static/images/blank.png"

VIDEO_BIT_RATE_INDEX = 0
VIDEO_INDEX: int = -1
IMAGE_INDEX: int = -1
IMAGE_TIKTOK_INDEX: int = -1
Expand Down
4 changes: 2 additions & 2 deletions src/downloader/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@ async def batch_processing(
"actual_root": actual_root
}
if (t := item["type"]) == _("图集"):
await self.download_image(**params)
await self.download_image(**params, type_=_("图集"), )
elif t == _("视频"):
await self.download_video(**params)
await self.download_video(**params, type_=_("视频"), )
elif t == _("实况"):
await self.download_image(
suffix="mp4",
Expand Down
43 changes: 28 additions & 15 deletions src/extract/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
VIDEO_INDEX,
IMAGE_INDEX,
IMAGE_TIKTOK_INDEX,
VIDEOS_INDEX,
DYNAMIC_COVER_INDEX,
ORIGIN_COVER_INDEX,
MUSIC_INDEX,
Expand All @@ -28,7 +27,6 @@
SEARCH_AVATAR_INDEX,
MUSIC_COLLECTION_COVER_INDEX,
MUSIC_COLLECTION_DOWNLOAD_INDEX,
VIDEO_BIT_RATE_INDEX,
)
from ..custom import condition_filter
from ..tools import TikTokDownloaderError
Expand Down Expand Up @@ -128,7 +126,7 @@ def depth_conversion(element):

@staticmethod
def safe_extract(
data: SimpleNamespace,
data: SimpleNamespace | list[SimpleNamespace],
attribute_chain: str,
default: str | int | list | dict | SimpleNamespace = "",
):
Expand Down Expand Up @@ -306,7 +304,7 @@ def __classifying_detail(self, item: dict, data: SimpleNamespace) -> None:
if images := self.safe_extract(data, "images"):
self.__extract_image_info(item, data, images)
else:
self.__extract_video_info(item, data)
self.__extract_video_info(item, data, _("视频"), )

def __classifying_detail_tiktok(
self,
Expand All @@ -315,7 +313,7 @@ def __classifying_detail_tiktok(
if images := self.safe_extract(data, "imagePost.images"):
self.__extract_image_info_tiktok(item, data, images)
else:
self.__extract_video_info_tiktok(item, data)
self.__extract_video_info_tiktok(item, data, _("视频"), )

def __extract_additional_info(
self,
Expand Down Expand Up @@ -362,13 +360,10 @@ def __extract_image_info(
if self.safe_extract(images[-1], "video"):
self.__set_blank_data(item, data, _("实况"), )
item["downloads"] = [
self.safe_extract(
i,
f'video.play_addr.url_list[{VIDEOS_INDEX}]',
) for i in images
self.__extract_video_download(i, ) for i in images
]
else:
self.__set_blank_data(item, data)
self.__set_blank_data(item, data, _("图集"), )
item["downloads"] = [
self.safe_extract(
i,
Expand All @@ -381,7 +376,7 @@ def __extract_image_info_tiktok(
item: dict,
data: SimpleNamespace,
images: list) -> None:
self.__set_blank_data(item, data)
self.__set_blank_data(item, data, _("图集"), )
item["downloads"] = [
self.safe_extract(
i,
Expand All @@ -404,17 +399,34 @@ def __extract_video_info(
self,
item: dict,
data: SimpleNamespace,
type_="视频",
type_=_("视频"),
) -> None:
item["type"] = type_
item["downloads"] = self.safe_extract(
data, f"video.bit_rate[{VIDEO_BIT_RATE_INDEX}].play_addr.url_list[{VIDEO_INDEX}]")
item["downloads"] = self.__extract_video_download(data, )
item["duration"] = self.time_conversion(
self.safe_extract(data, "video.duration", 0))
item["uri"] = self.safe_extract(
data, "video.play_addr.uri")
self.__extract_cover(item, data, True)

def __extract_video_download(self, data: SimpleNamespace, ) -> str:
bit_rate: list[SimpleNamespace] = self.safe_extract(
data,
"video.bit_rate",
[],
)
bit_rate: list[tuple[int, int, str, list[SimpleNamespace]]] = [(
i.FPS,
i.bit_rate,
i.gear_name,
i.play_addr
) for i in bit_rate]
bit_rate.sort(key=lambda x: (int(x[2].split("_")[-2]), x[1], x[0],), )
return self.safe_extract(
bit_rate[-1][-1],
f"url_list[{VIDEO_INDEX}]",
)

def __extract_video_info_tiktok(
self,
item: dict,
Expand Down Expand Up @@ -472,7 +484,8 @@ def __extract_cover(
self,
item: dict,
data: SimpleNamespace,
has=False) -> None:
has=False,
) -> None:
if has:
# 动态封面图链接
item["dynamic_cover"] = self.safe_extract(
Expand Down
8 changes: 4 additions & 4 deletions src/manager/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def __rename_folder(
mark: str,
):
new_folder = self.root.joinpath(f"{prefix}{id_}_{mark}_{suffix}")
self.__rename(old_folder, new_folder, _("文件夹"))
self.__rename(old_folder, new_folder, _("文件夹"), )
self.log.info(f"文件夹 {old_folder} 已重命名为 {new_folder}", False)

def __rename_works_folder(self,
Expand All @@ -117,7 +117,7 @@ def __rename_works_folder(self,
if (s := data[key]) in old_.name:
new_ = old_.parent / old_.name.replace(
s, {"name": name, "mark": mark}[key], 1)
self.__rename(old_, new_, _("文件夹"))
self.__rename(old_, new_, _("文件夹"), )
self.log.info(f"文件夹 {old_} 重命名为 {new_}", False)
return new_
return old_
Expand Down Expand Up @@ -169,12 +169,12 @@ def __rename_file(
field: str):
new_file = root.joinpath(old_file.name.replace(
key_words, {"name": name, "mark": mark}[field], 1))
self.__rename(old_file, new_file)
self.__rename(old_file, new_file, _("文件"), )
self.log.info(f"文件 {old_file} 重命名为 {new_file}", False)
return True

@PrivateRetry.retry_limited
def __rename(self, old_: Path, new_: Path, type_=_("文件")) -> bool:
def __rename(self, old_: Path, new_: Path, type_=_("文件"), ) -> bool:
try:
old_.rename(new_)
return True
Expand Down
4 changes: 1 addition & 3 deletions src/tools/choose.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ def choose(
console: Union["ColorfulConsole", "Console"],
separate=None) -> str:
screen = f"{title}:\n"
row = 0
for i, j in enumerate(options, start=1):
screen += f"{i: >2d}. {j}\n"
if separate and row in separate:
if separate and i in separate:
screen += f"{'=' * 25}\n"
row += 1
return console.input(screen)
4 changes: 2 additions & 2 deletions src/tools/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@


class TikTokDownloaderError(Exception):
def __init__(self, message=_("项目代码错误")):
self.message = message
def __init__(self, message: str = "", ):
self.message = message or _("项目代码错误")
super().__init__(self.message)

def __str__(self):
Expand Down

0 comments on commit 3fc4c67

Please sign in to comment.