Update remove-duplicates.js

This commit is contained in:
Aleksandr Statciuk 2021-08-01 07:21:31 +03:00
parent 151396503b
commit f5bb4299a9
1 changed files with 10 additions and 25 deletions

View File

@ -4,37 +4,31 @@ const utils = require('./utils')
let globalBuffer = [] let globalBuffer = []
async function main() { async function main() {
const playlists = parseIndex() utils.log('Starting...\n')
console.time('\nDone in')
const playlists = parseIndex()
for (const playlist of playlists) { for (const playlist of playlists) {
await loadPlaylist(playlist.url) await loadPlaylist(playlist.url).then(addToBuffer).then(removeDuplicates).then(savePlaylist)
.then(addToBuffer)
.then(removeDuplicates)
.then(savePlaylist)
.then(done)
} }
if (playlists.length) { if (playlists.length) {
await loadPlaylist('channels/unsorted.m3u') await loadPlaylist('channels/unsorted.m3u').then(removeUnsortedDuplicates).then(savePlaylist)
.then(removeUnsortedDuplicates)
.then(savePlaylist)
.then(done)
} }
finish() finish()
} }
function parseIndex() { function parseIndex() {
console.info(`Parsing 'index.m3u'...`) utils.log(`Parsing 'index.m3u'...`)
let playlists = parser.parseIndex() let playlists = parser.parseIndex()
playlists = playlists.filter(i => i.url !== 'channels/unsorted.m3u') playlists = playlists.filter(i => i.url !== 'channels/unsorted.m3u')
console.info(`Found ${playlists.length} playlist(s)\n`)
return playlists return playlists
} }
async function loadPlaylist(url) { async function loadPlaylist(url) {
console.info(`Processing '${url}'...`) utils.log(`\nProcessing '${url}'...`)
return parser.parsePlaylist(url) return parser.parsePlaylist(url)
} }
@ -46,9 +40,8 @@ async function addToBuffer(playlist) {
} }
async function removeDuplicates(playlist) { async function removeDuplicates(playlist) {
console.info(` Looking for duplicates...`)
let buffer = {} let buffer = {}
const channels = playlist.channels.filter(i => { playlist.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) {
@ -58,13 +51,10 @@ async function removeDuplicates(playlist) {
return result return result
}) })
playlist.channels = channels
return playlist return playlist
} }
async function removeUnsortedDuplicates(playlist) { async function removeUnsortedDuplicates(playlist) {
console.info(` Looking for duplicates...`)
// locally // locally
let buffer = {} let buffer = {}
let channels = playlist.channels.filter(i => { let channels = playlist.channels.filter(i => {
@ -89,22 +79,17 @@ async function savePlaylist(playlist) {
const output = playlist.toString({ raw: true }) const output = playlist.toString({ raw: true })
if (original === output) { if (original === output) {
console.info(`No changes have been made.`)
return false return false
} else { } else {
utils.createFile(playlist.url, output) utils.createFile(playlist.url, output)
console.info(`Playlist has been updated.`) utils.log(`updated`)
} }
return true return true
} }
async function done() {
console.info(` `)
}
function finish() { function finish() {
console.info('Done.') console.timeEnd('\nDone in')
} }
main() main()