Update filter.js

This commit is contained in:
Aleksandr Statciuk 2021-08-01 07:55:18 +03:00
parent f5bb4299a9
commit 3e2bd0c36b
1 changed files with 16 additions and 43 deletions

View File

@ -1,32 +1,23 @@
const blacklist = require('./blacklist.json')
const parser = require('./parser') const parser = require('./parser')
const utils = require('./utils') const utils = require('./utils')
const blacklist = require('./blacklist.json') const log = require('./log')
async function main() { async function main() {
const playlists = parseIndex() log.start()
log.print(`Parsing 'index.m3u'...`)
const playlists = parser.parseIndex()
for (const playlist of playlists) { for (const playlist of playlists) {
await loadPlaylist(playlist.url).then(removeBlacklisted).then(savePlaylist).then(done) log.print(`\nProcessing '${playlist.url}'...`)
await parser.parsePlaylist(playlist.url).then(removeBlacklisted).then(utils.savePlaylist)
} }
finish() log.finish()
}
function parseIndex() {
console.info(`Parsing 'index.m3u'...`)
let playlists = parser.parseIndex()
console.info(`Found ${playlists.length} playlist(s)\n`)
return playlists
}
async function loadPlaylist(url) {
console.info(`Processing '${url}'...`)
return parser.parsePlaylist(url)
} }
async function removeBlacklisted(playlist) { async function removeBlacklisted(playlist) {
console.info(` Looking for blacklisted channels...`) const channels = playlist.channels.filter(channel => {
playlist.channels = playlist.channels.filter(channel => {
return !blacklist.find(i => { return !blacklist.find(i => {
const channelName = channel.name.toLowerCase() const channelName = channel.name.toLowerCase()
return ( return (
@ -37,31 +28,13 @@ async function removeBlacklisted(playlist) {
}) })
}) })
if (playlist.channels.length !== channels.length) {
log.print(`updated`)
}
playlist.channels = channels
return playlist return playlist
} }
async function savePlaylist(playlist) {
console.info(` Saving playlist...`)
const original = utils.readFile(playlist.url)
const output = playlist.toString({ raw: true })
if (original === output) {
console.info(`No changes have been made.`)
return false
} else {
utils.createFile(playlist.url, output)
console.info(`Playlist has been updated.`)
}
return true
}
async function done() {
console.info(` `)
}
function finish() {
console.info('Done.')
}
main() main()