Update remove-duplicates.js

This commit is contained in:
Aleksandr Statciuk 2021-08-01 08:04:03 +03:00
parent 3e2bd0c36b
commit 4380a4fac4
1 changed files with 27 additions and 40 deletions

View File

@ -1,35 +1,32 @@
const parser = require('./parser') const parser = require('./parser')
const utils = require('./utils') const utils = require('./utils')
const log = require('./log')
let globalBuffer = [] let globalBuffer = []
async function main() { async function main() {
utils.log('Starting...\n') log.start()
console.time('\nDone in')
const playlists = parseIndex() log.print(`Parsing 'index.m3u'...`)
const playlists = parser.parseIndex().filter(i => i.url !== 'channels/unsorted.m3u')
for (const playlist of playlists) { for (const playlist of playlists) {
await loadPlaylist(playlist.url).then(addToBuffer).then(removeDuplicates).then(savePlaylist) log.print(`\nProcessing '${playlist.url}'...`)
await parser
.parsePlaylist(playlist.url)
.then(addToBuffer)
.then(removeDuplicates)
.then(utils.savePlaylist)
} }
if (playlists.length) { if (playlists.length) {
await loadPlaylist('channels/unsorted.m3u').then(removeUnsortedDuplicates).then(savePlaylist) log.print(`\nProcessing 'channels/unsorted.m3u'...`)
await parser
.parsePlaylist('channels/unsorted.m3u')
.then(removeUnsortedDuplicates)
.then(utils.savePlaylist)
} }
finish() log.finish()
}
function parseIndex() {
utils.log(`Parsing 'index.m3u'...`)
let playlists = parser.parseIndex()
playlists = playlists.filter(i => i.url !== 'channels/unsorted.m3u')
return playlists
}
async function loadPlaylist(url) {
utils.log(`\nProcessing '${url}'...`)
return parser.parsePlaylist(url)
} }
async function addToBuffer(playlist) { async function addToBuffer(playlist) {
@ -41,7 +38,7 @@ async function addToBuffer(playlist) {
async function removeDuplicates(playlist) { async function removeDuplicates(playlist) {
let buffer = {} let buffer = {}
playlist.channels = playlist.channels.filter(i => { const channels = playlist.channels.filter(i => {
const url = utils.removeProtocol(i.url) const url = utils.removeProtocol(i.url)
const result = typeof buffer[url] === 'undefined' const result = typeof buffer[url] === 'undefined'
if (result) { if (result) {
@ -51,6 +48,11 @@ async function removeDuplicates(playlist) {
return result return result
}) })
if (playlist.channels.length !== channels.length) {
log.print('updated')
playlist.channels = channels
}
return playlist return playlist
} }
@ -64,32 +66,17 @@ async function removeUnsortedDuplicates(playlist) {
return result return result
}) })
// globally // globally
const urls = globalBuffer.map(i => utils.removeProtocol(i.url)) const urls = globalBuffer.map(i => utils.removeProtocol(i.url))
channels = channels.filter(i => !urls.includes(utils.removeProtocol(i.url))) channels = channels.filter(i => !urls.includes(utils.removeProtocol(i.url)))
if (channels.length === playlist.channels.length) return playlist
if (channels.length !== playlist.channels.length) {
log.print('updated')
playlist.channels = channels playlist.channels = channels
}
return playlist return playlist
} }
async function savePlaylist(playlist) {
const original = utils.readFile(playlist.url)
const output = playlist.toString({ raw: true })
if (original === output) {
return false
} else {
utils.createFile(playlist.url, output)
utils.log(`updated`)
}
return true
}
function finish() {
console.timeEnd('\nDone in')
}
main() main()