Created
January 31, 2019 10:47
-
-
Save konsumer/76aa10c8da44a4477c7a03002c804336 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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