Update format.js

- added channel url validation
- small refactoring
This commit is contained in:
freearhey 2019-08-08 03:32:11 +03:00
parent ca34679cf8
commit b8f1986282
1 changed files with 17 additions and 11 deletions

View File

@ -1,22 +1,25 @@
const util = require('./util') const util = require('./util')
const debug = false const debug = false
let stats = { let stats = {
countries: 0, total: 0,
channels: 0,
updated: 0, updated: 0,
duplicates: 0 duplicates: 0,
unvalid: 0,
removed: 0
} }
let buffer = {} let buffer = {}
async function main() { async function main() {
console.log(`Parsing 'index.m3u'...`) console.log(`Parsing 'index.m3u'...`)
const playlist = util.parsePlaylist('index.m3u') const playlist = util.parsePlaylist('index.m3u')
let countries = playlist.items let countries = playlist.items
if(debug) { if(debug) {
console.log('Debug mode is turn on') console.log('Debug mode is turn on')
// countries = countries.slice(0, 1) countries = countries.slice(0, 1)
countries = [{ url: 'channels/ru.m3u' }, { url: 'channels/ua.m3u' }] // countries = [{ url: 'channels/ru.m3u' }, { url: 'channels/ua.m3u' }]
} }
for(let country of countries) { for(let country of countries) {
@ -43,11 +46,15 @@ async function main() {
title: item.inf.title title: item.inf.title
}) })
if(!util.checkCache(channel.url)) { if(util.checkCache(channel.url)) {
stats.duplicates++
stats.removed++
} else if(!util.validateUrl(channel.url)) {
stats.unvalid++
stats.removed++
} else {
channels.push(channel) channels.push(channel)
util.addToCache(channel.url) util.addToCache(channel.url)
} else {
stats.duplicates++
} }
} }
@ -101,11 +108,10 @@ async function main() {
util.appendToFile(country.url, channel.toString()) util.appendToFile(country.url, channel.toString())
}) })
stats.countries++ stats.total += channels.length
stats.channels += channels.length
} }
console.log(`Countries: ${stats.countries}. Channels: ${stats.channels}. Updated: ${stats.updated}. Duplicates: ${stats.duplicates}.`) console.log(`Total: ${stats.total}. Duplicates: ${stats.duplicates}. Unvalid: ${stats.unvalid}. Updated: ${stats.updated}. Removed: ${stats.removed}.`)
} }
main() main()