91 lines
3.2 KiB
TypeScript
Raw Normal View History

2022-11-27 20:22:42 +08:00
import RuleInterface from "./RuleInterface";
import FileObj from "../../vo/FileObj";
import path from 'path';
2023-04-09 21:50:37 +08:00
import {getSeason} from "../../../util/MediaUtil";
2022-11-27 20:22:42 +08:00
let pattern = new RegExp(/s(eason)?(\d+)/);
2023-03-07 00:06:59 +08:00
let eNumPatternArr = [new RegExp(/ep?(\d+)/), new RegExp(/[\(\[](\d+)[\)\]]/), new RegExp(/[\.-](\d+)/), new RegExp(/(\d+)/)];
2022-12-04 12:48:08 +08:00
let resolutionPattern = new RegExp(/(\d{3,}[pP])/);
2022-12-02 17:37:41 +08:00
let resolutionArr = ['1k', '1K', '2k', '2K', '4k', '4K', '8k', '8K'];
2022-11-27 20:22:42 +08:00
let charSet = new Set([' ', '[', '.', '(', '']);
export default class InsertRule implements RuleInterface {
2023-03-07 00:06:59 +08:00
/**
* seasonname/
*/
type: string;
/**
*
*/
frontAdd: string;
/**
*
*/
endAdd: string;
eNumWidth: number;
2022-11-27 20:22:42 +08:00
2023-03-07 00:06:59 +08:00
constructor(data: any) {
this.type = data.type;
this.frontAdd = data.frontAdd;
this.endAdd = data.endAdd;
2023-03-08 21:24:08 +08:00
this.eNumWidth = data.eNumWidth;
2023-03-07 00:06:59 +08:00
}
2022-11-27 20:22:42 +08:00
2023-03-07 00:06:59 +08:00
deal(file: FileObj): void {
//识别到的内容
let getStr = null;
2023-04-09 21:50:37 +08:00
let season = getSeason(path.basename(file.path));
2023-03-07 00:06:59 +08:00
if (this.type === 'season') {
2023-04-09 21:50:37 +08:00
getStr = season;
2023-03-07 00:06:59 +08:00
} else if (this.type === 'name') {
let originName = null;
2023-04-09 21:50:37 +08:00
if (season && season.length > 0) {
2023-03-07 00:06:59 +08:00
//说明是剧集,取父文件夹的父文件夹名称
originName = path.basename(path.resolve(file.path, '..'));
} else {
//说明是电影
originName = path.basename(file.path);
}
getStr = '';
for (let i = 0; i < originName.length; i++) {
let char = originName.charAt(i);
if (charSet.has(char)) {
break;
}
getStr += char;
}
} else if (this.type === 'eNum') {
let lowName = file.originName.toLocaleLowerCase().replace(/ /g, '')
2023-06-25 22:10:12 +08:00
.replace(/\d+[kp]/g, '')//去除4k,1080p等
2023-03-07 00:06:59 +08:00
.replace(/[xh]\d+/g, '')//去除x264,h264等 ;
for (let i in eNumPatternArr) {
let patternRes = lowName.match(eNumPatternArr[i]);
if (patternRes && patternRes.length > 1) {
getStr = patternRes[1];
for (let i = 0; i < this.eNumWidth - getStr.length; i++) {
getStr = '0' + getStr;
}
break;
}
}
} else if (this.type === 'resolution') {
let res = file.originName.match(resolutionPattern);
if (res && res.length > 1) {
getStr = res[1];
} else {
for (let i = 0; i < resolutionArr.length; i++) {
if (file.originName.indexOf(resolutionArr[i]) > -1) {
getStr = resolutionArr[i];
break;
}
}
}
}
if (getStr && getStr.length > 0) {
file.realName = file.realName + this.frontAdd + getStr + this.endAdd;
file.name = file.realName + file.expandName;
}
}
2022-11-27 20:22:42 +08:00
}