From 01321f6f11e47e0bb142789390038e32075f84ca Mon Sep 17 00:00:00 2001 From: JohnserfSeed Date: Fri, 9 Feb 2024 22:35:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E9=9D=9Ewindows?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E4=B8=8B=E5=88=9B=E5=BB=BA=E9=95=BF=E4=B8=AD?= =?UTF-8?q?=E6=96=87=E5=90=8D=E6=96=87=E4=BB=B6=E5=87=BA=E9=94=99=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98(#34)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #34 https://github.com/Johnserf-Seed/TikTokDownload/issues/653 https://github.com/Johnserf-Seed/TikTokDownload/issues/636 https://github.com/Johnserf-Seed/TikTokDownload/issues/483 --- f2/apps/douyin/utils.py | 53 +++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/f2/apps/douyin/utils.py b/f2/apps/douyin/utils.py index cbcc82e9..d6c0acfe 100644 --- a/f2/apps/douyin/utils.py +++ b/f2/apps/douyin/utils.py @@ -21,6 +21,7 @@ gen_random_str, get_timestamp, extract_valid_urls, + split_filename, ) from f2.exceptions.api_exceptions import ( APIError, @@ -484,7 +485,7 @@ def format_file_name( naming_template: str, aweme_data: dict = {}, custom_fields: dict = {}, - desc_length_limit: int = 200, + desc_length_limit: int = None, ) -> str: """ 根据配置文件的全局格式化文件名 @@ -501,41 +502,41 @@ def format_file_name( (Windows file name length limit is 255 characters, 32,767 characters after long file name support is enabled) Unix 文件名长度限制为 255 个字符 (Unix file name length limit is 255 characters) - 取去除后的20个字符, 加上后缀, 一般不会超过255个字符 - (Take the removed 20 characters, add the suffix, and generally not exceed 255 characters) + 取去除后的50个字符, 加上后缀, 一般不会超过255个字符 + (Take the removed 50 characters, add the suffix, and generally not exceed 255 characters) + 详细信息请参考: https://en.wikipedia.org/wiki/Filename#Length + (For more information, please refer to: https://en.wikipedia.org/wiki/Filename#Length) Returns: str: 格式化的文件名 (Formatted file name) """ - # 如果字典中没有对应的键,则使用用户自定义的字段 - # (If there is no corresponding key in the dictionary, use the user-defined field) + + # 为不同系统设置不同的文件名长度限制 + os_limit = { + "win32": 200, + "cygwin": 60, + "darwin": 60, + "linux": 60, + } + fields = { - "create": "", - "nickname": "", - "aweme_id": "", - "desc": "", - "uid": "", + "create": aweme_data.get("create_time", ""), # 长度固定19 + "nickname": aweme_data.get("nickname", ""), # 最长30 + "aweme_id": aweme_data.get("aweme_id", ""), # 长度固定19 + "desc": split_filename( + aweme_data.get("desc", ""), os_limit, desc_length_limit + ),# 分割 'desc' 字段 + "uid": aweme_data.get("uid", ""), # 固定11 } if custom_fields: + # 更新自定义字段 fields.update(custom_fields) - else: - fields = { - "create": aweme_data.get("create_time", ""), - "nickname": aweme_data.get("nickname", ""), - "aweme_id": aweme_data.get("aweme_id", ""), - "desc": aweme_data.get("desc", ""), - "uid": aweme_data.get("uid", ""), - } - - # 处理 'desc' 字段的长度限制 - if desc_length_limit is not None and len(fields["desc"]) > desc_length_limit: - half_limit = desc_length_limit // 2 - 6 - fields["desc"] = ( - f"{fields['desc'][:half_limit]}......{fields['desc'][-half_limit:]}" - ) - return naming_template.format(**fields) + try: + return naming_template.format(**fields) + except KeyError as e: + raise KeyError(_("文件名模板字段 {0} 不存在,请检查".format(e))) def create_user_folder(kwargs: dict, nickname: Union[str, int]) -> Path: