feat : support rollback

This commit is contained in:
HerbertHe 2023-12-17 14:42:07 +08:00
parent 7e15681617
commit 450cce413a
5 changed files with 52 additions and 17 deletions

View File

@ -12,8 +12,8 @@ Sources are from:
- [YanG-1989/m3u](https://github.com/YanG-1989/m3u)
- [fanmingming/live](https://github.com/fanmingming/live)
| channel | url | list | count |
| ------- | --- | ---- | ----- |
| channel | url | list | count | isRollback |
| ------- | --- | ---- | ----- | ---------- |
<!-- channels_here -->
See <https://m3u.ibert.me> to get more.

View File

@ -11,7 +11,7 @@ export type TChannelsSources = IChannelSource[]
export interface IChannel {
name: string
m3u: string
count: number
count: number | undefined
}
export interface IChannelsResult {
@ -19,14 +19,17 @@ export interface IChannelsResult {
updated_at: number
}
export const updateChannelsJson = (sources: TChannelsSources, cs: number[]) => {
export const updateChannelsJson = (
sources: TChannelsSources,
res: Array<[string, number | undefined]>
) => {
const json_p = path.resolve("m3u", "channels.json")
const result: IChannelsResult = {
channels: sources.map((source, idx) => ({
name: source.name,
m3u: `https://m3u.ibert.me/${source.f_name}.m3u`,
count: cs[idx],
count: res[idx][1],
})),
updated_at: new Date().getTime(),
}

View File

@ -1,7 +1,9 @@
import { count } from "console"
import { updateChannelsJson } from "./channels"
import { cleanFiles, getM3u, writeM3u, writeM3uToTxt } from "./file"
import { updateChannelList, updateReadme } from "./readme"
import { sources, filter } from "./sources"
import { updateByRollback } from "./rollback"
cleanFiles()
@ -17,16 +19,28 @@ Promise.allSettled(
writeM3u(sr.f_name, m3u)
writeM3uToTxt(sr.name, sr.f_name, m3u)
updateChannelList(sr.name, sr.f_name, m3u)
return count
return ["normal", count]
} else {
return undefined
// rollback
const res = await updateByRollback(sr.f_name, sr.filter)
if (!!res) {
const [m3u, count] = res
writeM3u(sr.f_name, m3u)
writeM3uToTxt(sr.name, sr.f_name, m3u)
updateChannelList(sr.name, sr.f_name, m3u, true)
return ["rollback", count]
} else {
// rollback failed
return ["rollback", undefined]
}
}
})
)
.then((counts) => {
const cs = (<any>counts).map(({ value }) => value)
updateChannelsJson(sources, cs)
updateReadme(sources, cs)
.then((result) => {
const res = (<any>result).map(({ value }) => value)
updateChannelsJson(sources, res)
updateReadme(sources, res)
})
.catch((err) => {
console.error(err)

View File

@ -14,7 +14,8 @@ export type TREADMESources = IREADMESource[]
export const updateChannelList = (
name: string,
f_name: string,
m3u: string
m3u: string,
rollback: boolean = false
) => {
const list_temp_p = path.join(path.resolve(), "LIST.temp.md")
const list = fs.readFileSync(list_temp_p, "utf8").toString()
@ -32,7 +33,9 @@ export const updateChannelList = (
const after = list
.replace(
"<!-- list_title_here -->",
`# List for **${name}**\n\n> M3U: <https://m3u.ibert.me/${f_name}.m3u>, TXT: <https://m3u.ibert.me/txt/${f_name}.txt>`
`# List for **${name}**${
rollback ? "(Rollback)" : ""
}\n\n> M3U: <https://m3u.ibert.me/${f_name}.m3u>, TXT: <https://m3u.ibert.me/txt/${f_name}.txt>`
)
.replace(
"<!-- channels_here -->",
@ -58,7 +61,7 @@ export const updateChannelList = (
export const updateReadme = (
sources: TREADMESources,
counts: Array<number | undefined>
res: Array<[string, number | undefined]>
) => {
const readme_temp_p = path.join(path.resolve(), "README.temp.md")
const readme = fs.readFileSync(readme_temp_p, "utf8").toString()
@ -75,10 +78,10 @@ export const updateReadme = (
}.txt> | [List for ${s.name}](https://m3u.ibert.me/list/${
s.f_name
}.list) | ${
counts[idx] === undefined
res[idx][1] === undefined
? "update failed"
: counts[idx]
} |`
: res[idx][1]
} | ${res[idx][0] === "rollback" ? "✅" : "-"} |`
)
.join("\n")}\n\nUpdated at **${new Date()}**`
)

15
src/rollback.ts Normal file
View File

@ -0,0 +1,15 @@
import { filter } from "./sources"
export const updateByRollback = async (
f_name: string,
sr_filter?: (raw: string) => [string, number]
): Promise<[string, number] | undefined> => {
const res = await fetch(`https://m3u.ibert.me/${f_name}.m3u`)
const status = res.status
if (/^[2]/.test(status.toString())) {
const text = await res.text()
return !!sr_filter ? sr_filter(text as string) : filter(text as string)
} else {
return undefined
}
}