增加拼音排序作为全局文件排序函数的第五级排序键

This commit is contained in:
x1ao4 2025-07-01 16:59:46 +08:00
parent 544d24b145
commit 2e90f9fbac
3 changed files with 34 additions and 9 deletions

View File

@ -30,6 +30,19 @@ function sortFileByName(file) {
let file_name_without_ext = filename.replace(/\.[^/.]+$/, '');
let date_value = Infinity, episode_value = Infinity, segment_value = 0;
// 生成拼音排序键(第五级排序)
let pinyin_sort_key;
try {
// 尝试使用 pinyinPro 库进行拼音转换
if (typeof pinyinPro !== 'undefined') {
pinyin_sort_key = pinyinPro.pinyin(filename, { toneType: 'none', type: 'string' }).toLowerCase();
} else {
pinyin_sort_key = filename.toLowerCase();
}
} catch (e) {
pinyin_sort_key = filename.toLowerCase();
}
// 1. 日期提取
let match;
// YYYY-MM-DD
@ -142,7 +155,7 @@ function sortFileByName(file) {
else if (/[中][集期话部篇]?|[集期话部篇]中/.test(filename)) segment_value = 2;
else if (/[下][集期话部篇]?|[集期话部篇]下/.test(filename)) segment_value = 3;
return [date_value, episode_value, segment_value, update_time];
return [date_value, episode_value, segment_value, update_time, pinyin_sort_key];
}
// 用法:

View File

@ -14,15 +14,19 @@ except ImportError:
def sort_file_by_name(file):
if isinstance(file, dict):
filename = file.get("file_name", "")
update_time = file.get("updated_at", 0)
else:
filename = file
update_time = 0
# 简单排序,主要通过文件名进行(使用拼音排序)
try:
from pypinyin import lazy_pinyin, Style
pinyin_list = lazy_pinyin(filename, style=Style.NORMAL, errors='ignore')
return ''.join(pinyin_list).lower()
pinyin_sort_key = ''.join(pinyin_list).lower()
except ImportError:
return filename.lower()
pinyin_sort_key = filename.lower()
# 返回五级排序元组:(日期, 期数, 上中下, 修改时间, 拼音排序)
return (float('inf'), float('inf'), 0, update_time, pinyin_sort_key)
def get_filename_pinyin_sort_key(filename):
try:

View File

@ -42,19 +42,27 @@ def sort_file_by_name(file):
通用的文件排序函数用于根据文件名智能排序
支持多种格式的日期期数集数等提取和排序
使用多级排序键按日期期数上中下顺序排序
如果以上均无法提取则使用文件更新时间作为最后排序依据
如果以上均无法提取则使用文件更新时间和拼音排序作为最后排序依据
"""
if isinstance(file, dict) and file.get("dir", False): # 跳过文件夹
return (float('inf'), float('inf'), float('inf'), 0)
return (float('inf'), float('inf'), float('inf'), 0, "")
# 获取文件名,支持字符串或文件对象
if isinstance(file, dict):
filename = file.get("file_name", "")
# 获取更新时间作为最后排序依据
# 获取更新时间作为第四级排序依据
update_time = file.get("updated_at", 0)
else:
filename = file
update_time = 0
# 导入拼音排序工具用于第五级排序
try:
from app.utils.pinyin_sort import get_filename_pinyin_sort_key
pinyin_sort_key = get_filename_pinyin_sort_key(filename)
except ImportError:
# 如果导入失败,使用简单的小写排序作为备用
pinyin_sort_key = filename.lower()
# 提取文件名,不含扩展名
file_name_without_ext = os.path.splitext(filename)[0]
@ -212,8 +220,8 @@ def sort_file_by_name(file):
elif re.search(r'下[集期话部篇]?|[集期话部篇]下', filename):
segment_value = 3
# 返回多级排序元组,加入更新时间作为第四级排序键
return (date_value, episode_value, segment_value, update_time)
# 返回多级排序元组,加入更新时间作为第四级排序键,拼音排序作为第五级排序键
return (date_value, episode_value, segment_value, update_time, pinyin_sort_key)
# 全局的剧集编号提取函数