Skip to content

Instantly share code, notes, and snippets.

@konsumer
Created January 31, 2019 10:47
Show Gist options
  • Save konsumer/76aa10c8da44a4477c7a03002c804336 to your computer and use it in GitHub Desktop.
Save konsumer/76aa10c8da44a4477c7a03002c804336 to your computer and use it in GitHub Desktop.
const fetch = require('node-fetch')
if (process.argv.length < 3) {
console.error(`Usage: node genfilter.js <URL>`)
process.exit(1)
}
const escapeRegExp = (str) => str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&')
const infoRegex = /([^\s="]+)=(?:"(.*?)"|(\d+))(?:,([.*^,]))?/g
// simple m3u_plus parser
const getPlaylist = async (url) => {
const body = (await (await fetch(url)).text()).split('\n').slice(1)
const out = []
body.forEach((str, i) => {
if (i % 2 === 0) {
let m
const innerOut = {}
while ((m = infoRegex.exec(str)) !== null) {
if (m.index === infoRegex.lastIndex) {
infoRegex.lastIndex++
}
innerOut[m[1].replace('tvg-', '').replace('group-title', 'group')] = m[2]
}
out.push(innerOut)
} else {
out[Math.floor(i / 2)].url = str.trim()
}
})
return out
}
const run = async () => {
const url = process.argv[2]
const p = await getPlaylist(url)
const filter = p.map((pl, i) => {
return {
channel: i + 1,
name: escapeRegExp(pl.name)
}
})
console.log(JSON.stringify(filter, null, 2))
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment