From 5d7e88cfc0c6b588f618c63746aa499136a31aa6 Mon Sep 17 00:00:00 2001 From: HerbertHe Date: Thu, 21 Mar 2024 17:41:06 +0800 Subject: [PATCH] :bookmark:v1.6.0 --- package.json | 2 +- src/task/custom/define.ts | 13 ++++++ src/task/custom/handler.ts | 86 ++++++++++++++++++++++++++++++++++---- 3 files changed, 93 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 50b8cc521ae4..ccd6268b93a5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "iptv-sources", - "version": "1.5.6", + "version": "1.6.0", "main": "index.js", "repository": "git@github.com:HerbertHe/iptv-sources.git", "author": "HerbertHe ", diff --git a/src/task/custom/define.ts b/src/task/custom/define.ts index b2e83d1151c8..7fd356251027 100644 --- a/src/task/custom/define.ts +++ b/src/task/custom/define.ts @@ -4,11 +4,24 @@ export interface ICustomRuleAppend { extinf?: string } +export interface ICustomRuleReplacerItem { + pattern: string + type: "string" | "regexp" + flags?: string + target: string +} + +export interface ICustomRuleReplacer { + extinf?: ICustomRuleReplacerItem[] + url?: ICustomRuleReplacerItem[] +} + export interface ICustomRule { upstream: string exclude?: string[] include?: string[] append?: ICustomRuleAppend[] + replacer?: ICustomRuleReplacer } export interface ICustom { diff --git a/src/task/custom/handler.ts b/src/task/custom/handler.ts index 235b13b9ac26..0c3f8d34ee04 100644 --- a/src/task/custom/handler.ts +++ b/src/task/custom/handler.ts @@ -1,7 +1,7 @@ import fs from "fs" import path from "path" -import type { ICustom } from "./define" +import type { ICustom, ICustomRuleReplacerItem } from "./define" import { config_path, m3u_path, write_custom_path } from "../const" import { m3u2txt, trimAny } from "../../utils" @@ -31,6 +31,50 @@ const getFilenames = () => { .filter((f) => f !== "channels") } +const after_replaced = ( + value: string, + replacers?: ICustomRuleReplacerItem[] +) => { + if (!replacers || !Array.isArray(replacers) || replacers.length === 0) + return value + + const replacer = replacers?.find((e) => { + if ( + e.type === "regexp" && + !!e.pattern && + !!e.flags && + !!e.target && + new RegExp(e.pattern, e.flags).test(value) + ) { + return true + } + + if ( + e.type === "string" && + !!e.pattern && + e.target && + value.includes(e.pattern) + ) { + return true + } + + return false + }) + + if (!!replacer && replacer.type === "regexp") { + return value.replace( + new RegExp(replacer.pattern, replacer.flags), + replacer.target + ) + } + + if (!!replacer && replacer.type === "string") { + return value.replace(replacer.pattern, replacer.target) + } + + return value +} + export const runCustomTask = () => { const cfg = loadConfigCustom() @@ -71,17 +115,45 @@ export const runCustomTask = () => { // include 的优先级高于 exclude if (!!rr.include && Array.isArray(rr.include)) { if (rr.include.includes(name)) { - res.push(arr[i]) - res.push(arr[i + 1]) + let _extinf = arr[i] + let _url = arr[i + 1] + if (!!rr.replacer) { + const { + extinf: extinf_replacer, + url: url_replacer, + } = rr.replacer + _extinf = after_replaced(arr[i], extinf_replacer) + _url = after_replaced(arr[i + 1], url_replacer) + } + res.push(_extinf) + res.push(_url) } } else if (!!rr.exclude && Array.isArray(rr.exclude)) { if (!rr.exclude.includes(name)) { - res.push(arr[i]) - res.push(arr[i + 1]) + let _extinf = arr[i] + let _url = arr[i + 1] + if (!!rr.replacer) { + const { + extinf: extinf_replacer, + url: url_replacer, + } = rr.replacer + _extinf = after_replaced(arr[i], extinf_replacer) + _url = after_replaced(arr[i + 1], url_replacer) + } + res.push(_extinf) + res.push(_url) } } else { - res.push(arr[i]) - res.push(arr[i + 1]) + let _extinf = arr[i] + let _url = arr[i + 1] + if (!!rr.replacer) { + const { extinf: extinf_replacer, url: url_replacer } = + rr.replacer + _extinf = after_replaced(arr[i], extinf_replacer) + _url = after_replaced(arr[i + 1], url_replacer) + } + res.push(_extinf) + res.push(_url) } }