Skip to content

Commit

Permalink
🔖v1.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
HerbertHe committed Mar 21, 2024
1 parent 8a621ce commit 5d7e88c
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iptv-sources",
"version": "1.5.6",
"version": "1.6.0",
"main": "index.js",
"repository": "[email protected]:HerbertHe/iptv-sources.git",
"author": "HerbertHe <[email protected]>",
Expand Down
13 changes: 13 additions & 0 deletions src/task/custom/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
86 changes: 79 additions & 7 deletions src/task/custom/handler.ts
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 5d7e88c

Please sign in to comment.