mirror of https://github.com/iptv-org/iptv
Merge branch 'create-playlist-cleaner'
commit
311629968c
@ -0,0 +1,90 @@
|
||||
const { file, parser, logger, checker, m3u } = require('../../core')
|
||||
const { program } = require('commander')
|
||||
|
||||
program
|
||||
.argument('[filepath]', 'Path to file to validate')
|
||||
.option('-t, --timeout <timeout>', 'Set timeout for each request', parser.parseNumber, 60000)
|
||||
.option('-d, --delay <delay>', 'Set delay for each request', parser.parseNumber, 0)
|
||||
.option('--debug', 'Enable debug mode')
|
||||
.parse(process.argv)
|
||||
|
||||
const options = program.opts()
|
||||
|
||||
async function main() {
|
||||
const files = program.args.length ? program.args : await file.list('streams/*.m3u')
|
||||
|
||||
for (const filepath of files) {
|
||||
if (!filepath.endsWith('.m3u')) continue
|
||||
logger.info(`${filepath}`)
|
||||
const playlist = await parser.parsePlaylist(filepath)
|
||||
const before = playlist.items.length
|
||||
for (const stream of playlist.items) {
|
||||
if (options.debug) logger.info(stream.url)
|
||||
const [_, status] = stream.raw.match(/status="([a-z]+)"/) || [null, null]
|
||||
stream.status = status
|
||||
if (status === 'error' && /^(http|https)/.test(stream.url) && !/\[.*\]$/.test(stream.name)) {
|
||||
const result = await checkStream(stream)
|
||||
const newStatus = parseStatus(result.error)
|
||||
if (status === newStatus) {
|
||||
stream.remove = true
|
||||
logger.info(`removed "${stream.name}"`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const items = playlist.items
|
||||
.filter(i => !i.remove)
|
||||
.map(item => ({
|
||||
attrs: {
|
||||
'tvg-id': item.tvg.id,
|
||||
status: item.status,
|
||||
'user-agent': item.http['user-agent'] || undefined
|
||||
},
|
||||
title: item.name,
|
||||
url: item.url,
|
||||
vlcOpts: {
|
||||
'http-referrer': item.http.referrer || undefined,
|
||||
'http-user-agent': item.http['user-agent'] || undefined
|
||||
}
|
||||
}))
|
||||
|
||||
if (before !== items.length) {
|
||||
const output = m3u.create(items)
|
||||
await file.create(filepath, output)
|
||||
logger.info(`saved`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
||||
|
||||
async function checkStream(item) {
|
||||
const config = {
|
||||
timeout: options.timeout,
|
||||
delay: options.delay,
|
||||
debug: options.debug
|
||||
}
|
||||
|
||||
const request = {
|
||||
url: item.url,
|
||||
http: {
|
||||
referrer: item.http.referrer,
|
||||
'user-agent': item.http['user-agent']
|
||||
}
|
||||
}
|
||||
|
||||
return checker.check(request, config)
|
||||
}
|
||||
|
||||
function parseStatus(error) {
|
||||
if (!error) return 'online'
|
||||
|
||||
switch (error) {
|
||||
case 'Operation timed out':
|
||||
return 'timeout'
|
||||
case 'Server returned 403 Forbidden (access denied)':
|
||||
return 'blocked'
|
||||
default:
|
||||
return 'error'
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
const m3u = {}
|
||||
|
||||
m3u.create = function (links = [], header = {}) {
|
||||
let output = `#EXTM3U`
|
||||
for (const attr in header) {
|
||||
const value = header[attr]
|
||||
output += ` ${attr}="${value}"`
|
||||
}
|
||||
output += `\n`
|
||||
|
||||
for (const link of links) {
|
||||
output += `#EXTINF:-1`
|
||||
for (const name in link.attrs) {
|
||||
const value = link.attrs[name]
|
||||
if (value !== undefined) {
|
||||
output += ` ${name}="${value}"`
|
||||
}
|
||||
}
|
||||
output += `,${link.title}\n`
|
||||
|
||||
for (const name in link.vlcOpts) {
|
||||
const value = link.vlcOpts[name]
|
||||
if (value !== undefined) {
|
||||
output += `#EXTVLCOPT:${name}=${value}\n`
|
||||
}
|
||||
}
|
||||
|
||||
output += `${link.url}\n`
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
module.exports = m3u
|
@ -1,4 +1,6 @@
|
||||
{"channel":null,"title":"1A Network (720p)","filepath":"tests/__data__/input/streams/unsorted.m3u","url":"https://simultv.s.llnwi.net/n4s4/2ANetwork/interlink.m3u8","http_referrer":null,"user_agent":null,"cluster_id":1,"_id":"ZJejfvbOVTyuf6Gk"}
|
||||
{"channel":null,"title":"Fox Sports 2 Asia (Thai) (720p)","filepath":"tests/__data__/input/streams/us_blocked.m3u","url":"https://example.com/playlist.m3u8","http_referrer":null,"user_agent":null,"cluster_id":1,"_id":"gnjGLZU1CEz79gcp"}
|
||||
{"channel":"ATV.ad","title":"ATV (720p) [Offline]","filepath":"tests/__data__/input/streams/ad.m3u","url":"https://iptv-all.lanesh4d0w.repl.co/andorra/atv","http_referrer":"http://imn.iq","user_agent":"Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148","cluster_id":1,"_id":"9r9qmYRa2kxiirl0"}
|
||||
{"channel":null,"title":"ABC (720p)","filepath":"tests/__data__/input/streams/wrong_id.m3u","url":"https://example.com/playlist2.m3u8","http_referrer":null,"user_agent":null,"cluster_id":1,"_id":"unOCFJtsDCbJupxR"}
|
||||
{"channel":null,"title":"TVN","filepath":"tests/__data__/input/streams/us_blocked.m3u","url":"https://example.com/playlist2.m3u8","http_referrer":null,"user_agent":null,"cluster_id":1,"_id":"TyQaTTYos0fr2q0P"}
|
||||
{"channel":"EverydayHeroes.us","title":"Everyday Heroes (720p)","filepath":"tests/__data__/input/streams/us_blocked.m3u","url":"https://a.jsrdn.com/broadcast/7b1451fa52/+0000/c.m3u8","http_referrer":null,"user_agent":null,"cluster_id":1,"_id":"yNDfQt0ITDrOGGV2"}
|
||||
{"channel":null,"title":"ATV (720p) [Offline]","filepath":"tests/__data__/input/streams/ad.m3u","url":"https://iptv-all.lanesh4d0w.repl.co/andorra/atv","http_referrer":"http://imn.iq","user_agent":"Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148","cluster_id":1,"_id":"asTdyPDWW77mXDLZ"}
|
||||
{"channel":null,"title":"ABC (720p)","filepath":"tests/__data__/input/streams/wrong_id.m3u","url":"https://example.com/playlist2.m3u8","http_referrer":null,"user_agent":null,"cluster_id":1,"_id":"1gBgkVYcwsNJQlso"}
|
||||
{"channel":null,"title":"1A Network (720p)","filepath":"tests/__data__/input/streams/unsorted.m3u","url":"https://simultv.s.llnwi.net/n4s4/2ANetwork/interlink.m3u8","http_referrer":null,"user_agent":null,"cluster_id":1,"_id":"8F6RyHFzpOe20huV"}
|
||||
{"channel":null,"title":"Fox Sports 2 Asia (Thai) (720p)","filepath":"tests/__data__/input/streams/us_blocked.m3u","url":"https://example.com/playlist.m3u8","http_referrer":null,"user_agent":null,"cluster_id":1,"_id":"9DY8CqVcKyp8jqiA"}
|
||||
|
Loading…
Reference in New Issue