From e368c1d74651f8cfe3ebdfb92b5177b56d02fa0b Mon Sep 17 00:00:00 2001 From: lc4t Date: Fri, 9 Jun 2023 01:11:58 +0800 Subject: [PATCH 1/6] =?UTF-8?q?feat:=20=E5=85=83=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=8C=87=E5=AE=9Apremiered=E7=9A=84=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 ++++++++ yutto/__main__.py | 4 ++++ yutto/_typing.py | 1 + yutto/api/bangumi.py | 4 ++-- yutto/api/cheese.py | 4 ++-- yutto/api/ugc_video.py | 8 ++++++-- yutto/processor/downloader.py | 3 ++- yutto/utils/metadata.py | 21 +++++++++++++++++---- yutto/utils/time.py | 4 ++++ 9 files changed, 46 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 44980b3cb..4d0510cfd 100644 --- a/README.md +++ b/README.md @@ -410,6 +410,14 @@ cat ~/.yutto_alias | yutto tensura-nikki --batch --alias-file - - 参数 `--metadata-only` - 默认值 `False` +#### 指定媒体元数据值的格式 + +当前仅支持`premiered` + +- 参数 `--metadata-format-premiered` +- 默认值 `%Y-%m-%d` +- 常用值 `%Y-%m-%d %H:%M:%S` + #### 严格校验大会员状态有效 - 参数 `--vip-strict` diff --git a/yutto/__main__.py b/yutto/__main__.py index 436e4355e..9f1f397bc 100644 --- a/yutto/__main__.py +++ b/yutto/__main__.py @@ -109,6 +109,7 @@ def cli() -> argparse.ArgumentParser: group_common.add_argument( "-af", "--alias-file", type=argparse.FileType("r", encoding="utf-8"), help="设置 url 别名文件路径" ) + group_common.add_argument("--metadata-format-premiered", default="%Y-%m-%d", help="专用于metadata文件中premiered字段的日期格式") # 资源选择 group_common.add_argument( @@ -286,6 +287,9 @@ async def run(args_list: list[argparse.Namespace]): "overwrite": args.overwrite, "block_size": int(args.block_size * 1024 * 1024), "num_workers": args.num_workers, + "metadata_format": { + "premiered": args.metadata_format_premiered, + }, }, ) Logger.new_line() diff --git a/yutto/_typing.py b/yutto/_typing.py index 48b6e50a8..eb8c18023 100644 --- a/yutto/_typing.py +++ b/yutto/_typing.py @@ -184,6 +184,7 @@ class DownloaderOptions(TypedDict): overwrite: bool block_size: int num_workers: int + metadata_format: dict[str, str | None] class FavouriteMetaData(TypedDict): diff --git a/yutto/api/bangumi.py b/yutto/api/bangumi.py index c32b939da..c7dc9192b 100644 --- a/yutto/api/bangumi.py +++ b/yutto/api/bangumi.py @@ -21,7 +21,7 @@ from yutto.utils.console.logger import Logger from yutto.utils.fetcher import Fetcher from yutto.utils.metadata import MetaData -from yutto.utils.time import get_time_str_by_now, get_time_str_by_stamp +from yutto.utils.time import get_time_str_by_now, get_time_struct_by_stamp class BangumiListItem(TypedDict): @@ -165,7 +165,7 @@ def _parse_bangumi_metadata(item: dict[str, Any]) -> MetaData: show_title=item["share_copy"], plot=item["share_copy"], thumb=item["cover"], - premiered=get_time_str_by_stamp(item["pub_time"], "%Y-%m-%d"), + premiered=get_time_struct_by_stamp(item["pub_time"]), dateadded=get_time_str_by_now(), source="", # TODO actor=[], # TODO diff --git a/yutto/api/cheese.py b/yutto/api/cheese.py index 5ea60415c..a8ec5002e 100644 --- a/yutto/api/cheese.py +++ b/yutto/api/cheese.py @@ -19,7 +19,7 @@ from yutto.utils.console.logger import Logger from yutto.utils.fetcher import Fetcher from yutto.utils.metadata import MetaData -from yutto.utils.time import get_time_str_by_now, get_time_str_by_stamp +from yutto.utils.time import get_time_str_by_now, get_time_struct_by_stamp class CheeseListItem(TypedDict): @@ -138,7 +138,7 @@ def _parse_cheese_metadata(item: dict[str, Any]) -> MetaData: show_title=item["title"], # 无此字段,用 title 代替 plot=item["title"], # 无此字段,用 title 代替 thumb=item["cover"], - premiered=get_time_str_by_stamp(item["release_date"], "%Y-%m-%d"), + premiered=get_time_struct_by_stamp(item["release_date"]), dateadded=get_time_str_by_now(), source="", # TODO actor=[], # TODO diff --git a/yutto/api/ugc_video.py b/yutto/api/ugc_video.py index 9915fa026..f25c741b7 100644 --- a/yutto/api/ugc_video.py +++ b/yutto/api/ugc_video.py @@ -25,7 +25,11 @@ from yutto.utils.console.logger import Logger from yutto.utils.fetcher import Fetcher from yutto.utils.metadata import Actor, MetaData -from yutto.utils.time import get_time_str_by_now, get_time_str_by_stamp +from yutto.utils.time import ( + get_time_str_by_now, + get_time_str_by_stamp, + get_time_struct_by_stamp, +) class _UgcVideoPageInfo(TypedDict): @@ -271,7 +275,7 @@ def _parse_ugc_video_metadata( show_title=page_info["part"], plot=video_info["description"], thumb=page_info["first_frame"] if page_info["first_frame"] is not None else video_info["picture"], - premiered=get_time_str_by_stamp(video_info["pubdate"], "%Y-%m-%d"), + premiered=get_time_struct_by_stamp(video_info["pubdate"]), dateadded=get_time_str_by_now(), actor=video_info["actor"], genre=video_info["genre"], diff --git a/yutto/processor/downloader.py b/yutto/processor/downloader.py index ee3eb86f2..262a455fd 100644 --- a/yutto/processor/downloader.py +++ b/yutto/processor/downloader.py @@ -203,6 +203,7 @@ async def start_downloader( filename = episode_data["filename"] require_video = options["require_video"] require_audio = options["require_audio"] + metadata_format = options["metadata_format"] Logger.info(f"开始处理视频 {filename}") tmp_dir.mkdir(parents=True, exist_ok=True) @@ -260,7 +261,7 @@ async def start_downloader( # 保存媒体描述文件 if metadata is not None: - write_metadata(metadata, output_path) + write_metadata(metadata, output_path, metadata_format) Logger.custom("NFO 媒体描述文件已生成", badge=Badge("描述文件", fore="black", back="cyan")) if output_path.exists(): diff --git a/yutto/utils/metadata.py b/yutto/utils/metadata.py index 7f773509e..ca2a5e060 100644 --- a/yutto/utils/metadata.py +++ b/yutto/utils/metadata.py @@ -1,8 +1,8 @@ from __future__ import annotations +import time from pathlib import Path from typing import TypedDict -from xml.dom.minidom import parseString # type: ignore from dict2xml import dict2xml # type: ignore @@ -20,7 +20,7 @@ class MetaData(TypedDict): show_title: str plot: str thumb: str - premiered: str + premiered: time.struct_time dateadded: str actor: list[Actor] genre: list[str] @@ -30,9 +30,22 @@ class MetaData(TypedDict): website: str -def write_metadata(metadata: MetaData, video_path: Path): +def metadata_value_format(metadata: MetaData, metadata_format: dict[str, str | None]) -> TypedDict: + for key, fmt in metadata_format.items(): + if fmt and key in metadata: + # datetime + if isinstance(metadata[key], (time.struct_time)): # TODO: datetime.datetime + metadata[key]: str = time.strftime(fmt, metadata[key]) # type: ignore + return metadata + + +def write_metadata(metadata: MetaData, video_path: Path, metadata_format: dict[str, str | None]): metadata_path = video_path.with_suffix(".nfo") custom_root = "episodedetails" # TODO: 不同视频类型使用不同的root name - xml_content = dict2xml(metadata, wrap=custom_root, indent=" ") # type: ignore + # 增加字段格式化内容,后续如果需要调整可以继续调整 + user_formatted_metadata: TypedDict = ( + metadata_value_format(metadata, metadata_format) if metadata_format else metadata + ) + xml_content = dict2xml(user_formatted_metadata, wrap=custom_root, indent=" ") # type: ignore with metadata_path.open("w", encoding="utf-8") as f: # type: ignore f.write(xml_content) # type: ignore diff --git a/yutto/utils/time.py b/yutto/utils/time.py index 8198e6018..24f855c34 100644 --- a/yutto/utils/time.py +++ b/yutto/utils/time.py @@ -13,3 +13,7 @@ def get_time_str_by_now(fmt: str = TIME_FMT): def get_time_str_by_stamp(stamp: float, fmt: str = TIME_FMT): local_time = time.localtime(stamp) return time.strftime(fmt, local_time) + + +def get_time_struct_by_stamp(stamp: float): + return time.localtime(stamp) From a686d14e0679cc530e8db984d48279cad2c620d0 Mon Sep 17 00:00:00 2001 From: lc4t Date: Fri, 9 Jun 2023 01:15:25 +0800 Subject: [PATCH 2/6] =?UTF-8?q?feat:=20=E5=85=83=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=8C=87=E5=AE=9Apremiered=E7=9A=84=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yutto/utils/metadata.py | 1 - 1 file changed, 1 deletion(-) diff --git a/yutto/utils/metadata.py b/yutto/utils/metadata.py index ca2a5e060..2816271d2 100644 --- a/yutto/utils/metadata.py +++ b/yutto/utils/metadata.py @@ -33,7 +33,6 @@ class MetaData(TypedDict): def metadata_value_format(metadata: MetaData, metadata_format: dict[str, str | None]) -> TypedDict: for key, fmt in metadata_format.items(): if fmt and key in metadata: - # datetime if isinstance(metadata[key], (time.struct_time)): # TODO: datetime.datetime metadata[key]: str = time.strftime(fmt, metadata[key]) # type: ignore return metadata From 9c7bfcc5c157e78b30c3b6fc2edd27cee528d7b2 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Sat, 10 Jun 2023 14:34:52 +0800 Subject: [PATCH 3/6] change premiered to int --- README.md | 4 ++-- yutto/__main__.py | 4 +++- yutto/api/bangumi.py | 4 ++-- yutto/api/cheese.py | 4 ++-- yutto/api/ugc_video.py | 8 ++------ yutto/utils/metadata.py | 27 ++++++++++++++------------- 6 files changed, 25 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 27258e454..b88e440c6 100644 --- a/README.md +++ b/README.md @@ -328,7 +328,7 @@ yutto -c "d8bc7493%2C2843925707%2C08c3e*81" |username|UP 主用户名|个人空间、收藏夹、稍后再看、合集、视频列表下载| |series_title|合集标题|收藏夹、视频合集、视频列表下载| |pubdate🕛|投稿日期|仅投稿视频| -|download_date|下载日期|全部| +|download_date🕛|下载日期|全部| |owner_uid|UP 主UID|个人空间、收藏夹、稍后再看、合集、视频列表下载| > **Note** @@ -412,7 +412,7 @@ cat ~/.yutto_alias | yutto tensura-nikki --batch --alias-file - #### 指定媒体元数据值的格式 -当前仅支持`premiered` +当前仅支持 `premiered` - 参数 `--metadata-format-premiered` - 默认值 `%Y-%m-%d` diff --git a/yutto/__main__.py b/yutto/__main__.py index 9f1f397bc..4cac014a8 100644 --- a/yutto/__main__.py +++ b/yutto/__main__.py @@ -109,7 +109,9 @@ def cli() -> argparse.ArgumentParser: group_common.add_argument( "-af", "--alias-file", type=argparse.FileType("r", encoding="utf-8"), help="设置 url 别名文件路径" ) - group_common.add_argument("--metadata-format-premiered", default="%Y-%m-%d", help="专用于metadata文件中premiered字段的日期格式") + group_common.add_argument( + "--metadata-format-premiered", default="%Y-%m-%d", help="专用于 metadata 文件中 premiered 字段的日期格式" + ) # 资源选择 group_common.add_argument( diff --git a/yutto/api/bangumi.py b/yutto/api/bangumi.py index c7dc9192b..d93c08b0c 100644 --- a/yutto/api/bangumi.py +++ b/yutto/api/bangumi.py @@ -21,7 +21,7 @@ from yutto.utils.console.logger import Logger from yutto.utils.fetcher import Fetcher from yutto.utils.metadata import MetaData -from yutto.utils.time import get_time_str_by_now, get_time_struct_by_stamp +from yutto.utils.time import get_time_str_by_now class BangumiListItem(TypedDict): @@ -165,7 +165,7 @@ def _parse_bangumi_metadata(item: dict[str, Any]) -> MetaData: show_title=item["share_copy"], plot=item["share_copy"], thumb=item["cover"], - premiered=get_time_struct_by_stamp(item["pub_time"]), + premiered=item["pub_time"], dateadded=get_time_str_by_now(), source="", # TODO actor=[], # TODO diff --git a/yutto/api/cheese.py b/yutto/api/cheese.py index a8ec5002e..9b28a45de 100644 --- a/yutto/api/cheese.py +++ b/yutto/api/cheese.py @@ -19,7 +19,7 @@ from yutto.utils.console.logger import Logger from yutto.utils.fetcher import Fetcher from yutto.utils.metadata import MetaData -from yutto.utils.time import get_time_str_by_now, get_time_struct_by_stamp +from yutto.utils.time import get_time_str_by_now class CheeseListItem(TypedDict): @@ -138,7 +138,7 @@ def _parse_cheese_metadata(item: dict[str, Any]) -> MetaData: show_title=item["title"], # 无此字段,用 title 代替 plot=item["title"], # 无此字段,用 title 代替 thumb=item["cover"], - premiered=get_time_struct_by_stamp(item["release_date"]), + premiered=item["release_date"], dateadded=get_time_str_by_now(), source="", # TODO actor=[], # TODO diff --git a/yutto/api/ugc_video.py b/yutto/api/ugc_video.py index c19264cc2..6a4f6e189 100644 --- a/yutto/api/ugc_video.py +++ b/yutto/api/ugc_video.py @@ -25,11 +25,7 @@ from yutto.utils.console.logger import Logger from yutto.utils.fetcher import Fetcher from yutto.utils.metadata import Actor, MetaData -from yutto.utils.time import ( - get_time_str_by_now, - get_time_str_by_stamp, - get_time_struct_by_stamp, -) +from yutto.utils.time import get_time_str_by_now class _UgcVideoPageInfo(TypedDict): @@ -275,7 +271,7 @@ def _parse_ugc_video_metadata( show_title=page_info["part"], plot=video_info["description"], thumb=page_info["first_frame"] if page_info["first_frame"] is not None else video_info["picture"], - premiered=get_time_struct_by_stamp(video_info["pubdate"]), + premiered=video_info["pubdate"], dateadded=get_time_str_by_now(), actor=video_info["actor"], genre=video_info["genre"], diff --git a/yutto/utils/metadata.py b/yutto/utils/metadata.py index 2816271d2..2efb17899 100644 --- a/yutto/utils/metadata.py +++ b/yutto/utils/metadata.py @@ -1,11 +1,12 @@ from __future__ import annotations -import time from pathlib import Path -from typing import TypedDict +from typing import Any, TypedDict from dict2xml import dict2xml # type: ignore +from yutto.utils.time import get_time_str_by_stamp + class Actor(TypedDict): name: str @@ -20,7 +21,7 @@ class MetaData(TypedDict): show_title: str plot: str thumb: str - premiered: time.struct_time + premiered: int dateadded: str actor: list[Actor] genre: list[str] @@ -30,21 +31,21 @@ class MetaData(TypedDict): website: str -def metadata_value_format(metadata: MetaData, metadata_format: dict[str, str | None]) -> TypedDict: - for key, fmt in metadata_format.items(): - if fmt and key in metadata: - if isinstance(metadata[key], (time.struct_time)): # TODO: datetime.datetime - metadata[key]: str = time.strftime(fmt, metadata[key]) # type: ignore - return metadata +def metadata_value_format(metadata: MetaData, metadata_format: dict[str, str | None]) -> dict[str, Any]: + formatted_metadata: dict[str, Any] = {} + for key, value in metadata.items(): + if key in metadata_format: + assert isinstance(value, int) + value = get_time_str_by_stamp(value) + formatted_metadata[key] = value + return formatted_metadata def write_metadata(metadata: MetaData, video_path: Path, metadata_format: dict[str, str | None]): metadata_path = video_path.with_suffix(".nfo") - custom_root = "episodedetails" # TODO: 不同视频类型使用不同的root name + custom_root = "episodedetails" # TODO: 不同视频类型使用不同的 root name # 增加字段格式化内容,后续如果需要调整可以继续调整 - user_formatted_metadata: TypedDict = ( - metadata_value_format(metadata, metadata_format) if metadata_format else metadata - ) + user_formatted_metadata = metadata_value_format(metadata, metadata_format) if metadata_format else metadata xml_content = dict2xml(user_formatted_metadata, wrap=custom_root, indent=" ") # type: ignore with metadata_path.open("w", encoding="utf-8") as f: # type: ignore f.write(xml_content) # type: ignore From a1d1bc52991092fefc30d723eeec684e45e55010 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Sat, 10 Jun 2023 14:40:43 +0800 Subject: [PATCH 4/6] update dateadded --- yutto/__main__.py | 2 ++ yutto/api/bangumi.py | 4 ++-- yutto/api/cheese.py | 4 ++-- yutto/api/ugc_video.py | 4 ++-- yutto/processor/path_resolver.py | 2 +- yutto/utils/metadata.py | 2 +- yutto/utils/time.py | 4 ++++ 7 files changed, 14 insertions(+), 8 deletions(-) diff --git a/yutto/__main__.py b/yutto/__main__.py index 4cac014a8..87c9f613f 100644 --- a/yutto/__main__.py +++ b/yutto/__main__.py @@ -36,6 +36,7 @@ from yutto.utils.console.logger import Badge, Logger from yutto.utils.fetcher import Fetcher from yutto.utils.funcutils import as_sync +from yutto.utils.time import TIME_FMT from yutto.validator import ( initial_validation, validate_basic_arguments, @@ -291,6 +292,7 @@ async def run(args_list: list[argparse.Namespace]): "num_workers": args.num_workers, "metadata_format": { "premiered": args.metadata_format_premiered, + "dateadded": TIME_FMT, }, }, ) diff --git a/yutto/api/bangumi.py b/yutto/api/bangumi.py index d93c08b0c..eb85bd57e 100644 --- a/yutto/api/bangumi.py +++ b/yutto/api/bangumi.py @@ -21,7 +21,7 @@ from yutto.utils.console.logger import Logger from yutto.utils.fetcher import Fetcher from yutto.utils.metadata import MetaData -from yutto.utils.time import get_time_str_by_now +from yutto.utils.time import get_time_stamp_by_now class BangumiListItem(TypedDict): @@ -166,7 +166,7 @@ def _parse_bangumi_metadata(item: dict[str, Any]) -> MetaData: plot=item["share_copy"], thumb=item["cover"], premiered=item["pub_time"], - dateadded=get_time_str_by_now(), + dateadded=get_time_stamp_by_now(), source="", # TODO actor=[], # TODO genre=[], # TODO diff --git a/yutto/api/cheese.py b/yutto/api/cheese.py index 9b28a45de..f3e9255f1 100644 --- a/yutto/api/cheese.py +++ b/yutto/api/cheese.py @@ -19,7 +19,7 @@ from yutto.utils.console.logger import Logger from yutto.utils.fetcher import Fetcher from yutto.utils.metadata import MetaData -from yutto.utils.time import get_time_str_by_now +from yutto.utils.time import get_time_stamp_by_now class CheeseListItem(TypedDict): @@ -139,7 +139,7 @@ def _parse_cheese_metadata(item: dict[str, Any]) -> MetaData: plot=item["title"], # 无此字段,用 title 代替 thumb=item["cover"], premiered=item["release_date"], - dateadded=get_time_str_by_now(), + dateadded=get_time_stamp_by_now(), source="", # TODO actor=[], # TODO genre=[], # TODO diff --git a/yutto/api/ugc_video.py b/yutto/api/ugc_video.py index 6a4f6e189..839a1aaa5 100644 --- a/yutto/api/ugc_video.py +++ b/yutto/api/ugc_video.py @@ -25,7 +25,7 @@ from yutto.utils.console.logger import Logger from yutto.utils.fetcher import Fetcher from yutto.utils.metadata import Actor, MetaData -from yutto.utils.time import get_time_str_by_now +from yutto.utils.time import get_time_stamp_by_now class _UgcVideoPageInfo(TypedDict): @@ -272,7 +272,7 @@ def _parse_ugc_video_metadata( plot=video_info["description"], thumb=page_info["first_frame"] if page_info["first_frame"] is not None else video_info["picture"], premiered=video_info["pubdate"], - dateadded=get_time_str_by_now(), + dateadded=get_time_stamp_by_now(), actor=video_info["actor"], genre=video_info["genre"], tag=video_info["tag"], diff --git a/yutto/processor/path_resolver.py b/yutto/processor/path_resolver.py index 36c0567f3..14cc41fcf 100644 --- a/yutto/processor/path_resolver.py +++ b/yutto/processor/path_resolver.py @@ -86,7 +86,7 @@ def resolve_path_template( subpath_variables[key] = repair_filename(value) # 将时间变量转换为对应的时间格式 - time_vars: list[PathTemplateVariable] = ["pubdate"] # TODO: add download_date + time_vars: list[PathTemplateVariable] = ["pubdate", "download_date"] for var in time_vars: value = subpath_variables.pop(var) if value == UNKNOWN: diff --git a/yutto/utils/metadata.py b/yutto/utils/metadata.py index 2efb17899..2edeb3537 100644 --- a/yutto/utils/metadata.py +++ b/yutto/utils/metadata.py @@ -22,7 +22,7 @@ class MetaData(TypedDict): plot: str thumb: str premiered: int - dateadded: str + dateadded: int actor: list[Actor] genre: list[str] tag: list[str] diff --git a/yutto/utils/time.py b/yutto/utils/time.py index 24f855c34..ececaa90c 100644 --- a/yutto/utils/time.py +++ b/yutto/utils/time.py @@ -5,6 +5,10 @@ TIME_FMT = "%Y-%m-%d %H:%M:%S" +def get_time_stamp_by_now() -> int: + return int(time.time()) + + def get_time_str_by_now(fmt: str = TIME_FMT): time_stamp_now = time.time() return get_time_str_by_stamp(time_stamp_now, fmt) From 62c763e4f0ed4c25ecb43afab88399f97464e451 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Sat, 10 Jun 2023 20:06:34 +0800 Subject: [PATCH 5/6] make metadata_format type to dict[str, str] --- yutto/__main__.py | 6 +++--- yutto/_typing.py | 2 +- yutto/utils/metadata.py | 6 +++--- yutto/utils/time.py | 7 ++++--- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/yutto/__main__.py b/yutto/__main__.py index 87c9f613f..af0697778 100644 --- a/yutto/__main__.py +++ b/yutto/__main__.py @@ -36,7 +36,7 @@ from yutto.utils.console.logger import Badge, Logger from yutto.utils.fetcher import Fetcher from yutto.utils.funcutils import as_sync -from yutto.utils.time import TIME_FMT +from yutto.utils.time import TIME_DATE_FMT, TIME_FULL_FMT from yutto.validator import ( initial_validation, validate_basic_arguments, @@ -111,7 +111,7 @@ def cli() -> argparse.ArgumentParser: "-af", "--alias-file", type=argparse.FileType("r", encoding="utf-8"), help="设置 url 别名文件路径" ) group_common.add_argument( - "--metadata-format-premiered", default="%Y-%m-%d", help="专用于 metadata 文件中 premiered 字段的日期格式" + "--metadata-format-premiered", default=TIME_DATE_FMT, help="专用于 metadata 文件中 premiered 字段的日期格式" ) # 资源选择 @@ -292,7 +292,7 @@ async def run(args_list: list[argparse.Namespace]): "num_workers": args.num_workers, "metadata_format": { "premiered": args.metadata_format_premiered, - "dateadded": TIME_FMT, + "dateadded": TIME_FULL_FMT, }, }, ) diff --git a/yutto/_typing.py b/yutto/_typing.py index eb8c18023..55e01fe7e 100644 --- a/yutto/_typing.py +++ b/yutto/_typing.py @@ -184,7 +184,7 @@ class DownloaderOptions(TypedDict): overwrite: bool block_size: int num_workers: int - metadata_format: dict[str, str | None] + metadata_format: dict[str, str] class FavouriteMetaData(TypedDict): diff --git a/yutto/utils/metadata.py b/yutto/utils/metadata.py index 2edeb3537..deed5cfa3 100644 --- a/yutto/utils/metadata.py +++ b/yutto/utils/metadata.py @@ -31,17 +31,17 @@ class MetaData(TypedDict): website: str -def metadata_value_format(metadata: MetaData, metadata_format: dict[str, str | None]) -> dict[str, Any]: +def metadata_value_format(metadata: MetaData, metadata_format: dict[str, str]) -> dict[str, Any]: formatted_metadata: dict[str, Any] = {} for key, value in metadata.items(): if key in metadata_format: assert isinstance(value, int) - value = get_time_str_by_stamp(value) + value = get_time_str_by_stamp(value, metadata_format[key]) formatted_metadata[key] = value return formatted_metadata -def write_metadata(metadata: MetaData, video_path: Path, metadata_format: dict[str, str | None]): +def write_metadata(metadata: MetaData, video_path: Path, metadata_format: dict[str, str]): metadata_path = video_path.with_suffix(".nfo") custom_root = "episodedetails" # TODO: 不同视频类型使用不同的 root name # 增加字段格式化内容,后续如果需要调整可以继续调整 diff --git a/yutto/utils/time.py b/yutto/utils/time.py index ececaa90c..0a5589c1b 100644 --- a/yutto/utils/time.py +++ b/yutto/utils/time.py @@ -2,19 +2,20 @@ import time -TIME_FMT = "%Y-%m-%d %H:%M:%S" +TIME_FULL_FMT = "%Y-%m-%d %H:%M:%S" +TIME_DATE_FMT = "%Y-%m-%d" def get_time_stamp_by_now() -> int: return int(time.time()) -def get_time_str_by_now(fmt: str = TIME_FMT): +def get_time_str_by_now(fmt: str = TIME_FULL_FMT): time_stamp_now = time.time() return get_time_str_by_stamp(time_stamp_now, fmt) -def get_time_str_by_stamp(stamp: float, fmt: str = TIME_FMT): +def get_time_str_by_stamp(stamp: float, fmt: str = TIME_FULL_FMT): local_time = time.localtime(stamp) return time.strftime(fmt, local_time) From 3e7d9272160d46c544f1083272e864e455be07d8 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Sat, 10 Jun 2023 20:08:29 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=F0=9F=93=9D=20docs:=20refine=20docs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b88e440c6..2231cdec1 100644 --- a/README.md +++ b/README.md @@ -415,8 +415,8 @@ cat ~/.yutto_alias | yutto tensura-nikki --batch --alias-file - 当前仅支持 `premiered` - 参数 `--metadata-format-premiered` -- 默认值 `%Y-%m-%d` -- 常用值 `%Y-%m-%d %H:%M:%S` +- 默认值 `"%Y-%m-%d"` +- 常用值 `"%Y-%m-%d %H:%M:%S"` #### 严格校验大会员状态有效