Replies: 7 comments
-
一种很容易想到的方式,就是使用正则表达式来进行剧集解析。 function parseEpisode(title: string): string | null {
const pattern = /(?![^ []])(第|未删减|_)?(?<episode>\d+)(话|話|集|v2|先行版)?(?![^ \]])/;
const match = title.match(pattern);
if (match?.groups?.episode) {
const episode = match.groups.episode;
console.log(`Successfully parsed episode ${episode} from ${title}`);
return /^\d+$/.test(episode) ? String(parseInt(episode, 10)) : episode;
}
return null;
} 然而正则表达式太过于令人讨厌,始终会担心有什么新的规则没有适配。 |
Beta Was this translation helpful? Give feedback.
-
另一种方式,则是利用不同集数之间相同的命名格式来入手,进行剧集解析。 import * as path from "path";
export function parseEpisode(fileNames: string[], start: number): string[] {
const backup = [...fileNames];
const indexs = Array.from({ length: fileNames.length }, (_, index) => index + start);
const episodes: string[] = [];
for (let i = indexs.length - 1; i >= 0; i--) {
const index = indexs[i];
const episode = fileNames
.map((fileName) => ({
fileName,
count: path.basename(fileName).split(index.toString()).length - 1,
}))
.reduce((a, b) => (a.count > b.count ? a : b)).fileName;
fileNames.splice(fileNames.indexOf(episode), 1);
episodes.push(episode);
}
fileNames.push(...backup);
return episodes.reverse();
} 下面着重分析这种方法。 |
Beta Was this translation helpful? Give feedback.
-
一般情况下,当所下载剧集不包含全部集数时,这种方法当然可以正常工作。 |
Beta Was this translation helpful? Give feedback.
-
补充解释:当已下载剧集至少有两集时,当然可以正常工作。而当只有一集被下载,那么它将固定被视为第一集,此时不需要进行任何解析。 |
Beta Was this translation helpful? Give feedback.
-
考虑剧集文件命名中的其他数字:可以发现,这种方法完全不需要担忧额外的数字,只要它们是相同地出现在每一集的命名中。 |
Beta Was this translation helpful? Give feedback.
-
考虑当每一集的命名格式不同时:如果有某一集的命名中多出“v2”的字样,可以发现,它即使引起错误,也只能在匹配第二集时引起错误。也就是说,当第一集的命名中存在一个额外的“v2”,而第二集没有时,这两集就可能被混淆。解决这个问题的方式是,在这时比较文件命名长短,更短的作为第二集。 |
Beta Was this translation helpful? Give feedback.
-
然而,有一个问题悬而未决:当剧集为续集时,应该如何进行剧集解析?举个例子,当某动画的第二季开播,其第一集在文件命名上既可能是第一集,也可能是第十三集(因为第一季已经有十二集了)。这种不统一导致很难轻易地用这个方法解析好剧集。 |
Beta Was this translation helpful? Give feedback.
-
用于探讨剧集解析的方法。
Beta Was this translation helpful? Give feedback.
All reactions