Small refactoring

This commit is contained in:
freearhey 2019-08-07 16:51:34 +03:00
parent 10ff2eac6a
commit 34efa18658
2 changed files with 89 additions and 84 deletions

View File

@ -1,7 +1,6 @@
const util = require('./util') const util = require('./util')
const debug = true const debug = false
const verbal = false
let stats = { let stats = {
countries: 0, countries: 0,
channels: 0, channels: 0,
@ -10,20 +9,22 @@ let stats = {
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/au.m3u' }] // countries = [{ url: 'channels/au.m3u' }]
} }
for(let country of countries) { for(let country of countries) {
console.log(`Parsing ${country.url}...`) console.log(`Parsing '${country.url}'...`)
const playlist = util.parsePlaylist(country.url) const playlist = util.parsePlaylist(country.url)
if(debug) {
console.log(`Creating channels list...`) console.log(`Creating channels list...`)
}
let channels = [] let channels = []
for(let item of playlist.items) { for(let item of playlist.items) {
let channel = util.createChannel({ let channel = util.createChannel({
@ -39,24 +40,25 @@ async function main() {
const epgUrl = playlist.attrs['x-tvg-url'] const epgUrl = playlist.attrs['x-tvg-url']
if(epgUrl && !buffer[epgUrl]) { if(epgUrl && !buffer[epgUrl]) {
console.log(`Loading ${epgUrl}...`) console.log(`Loading '${epgUrl}'...`)
const epg = await util.loadEPG(epgUrl) const epg = await util.loadEPG(epgUrl)
console.log(`Adding ${epgUrl} to buffer...`) console.log(`Adding '${epgUrl}' to buffer...`)
buffer[epgUrl] = epg buffer[epgUrl] = epg
} }
if(buffer[epgUrl]) {
console.log(`Fills in missing channel's data...`) console.log(`Fills in missing channel's data...`)
for(let channel of channels) { for(let channel of channels) {
let channelId = channel.id let channelId = channel.id
if(!channelId) continue if(!channelId) continue
let c = buffer[epgUrl] ? buffer[epgUrl].channels[channelId] : null let c = buffer[epgUrl].channels[channelId]
if(!c) continue if(!c) continue
let updated = false let updated = false
if(!channel.name && c.names[0]) { if(!channel.name && c.names[0]) {
channel.name = c.names[0] channel.name = c.names[0]
updated = true updated = true
if(verbal) { if(debug) {
console.log(`Added name '${c.names[0]}' to '${channel.id}'`) console.log(`Added name '${c.names[0]}' to '${channel.id}'`)
} }
} }
@ -64,7 +66,7 @@ async function main() {
if(!channel.logo && c.icon) { if(!channel.logo && c.icon) {
channel.logo = c.icon channel.logo = c.icon
updated = true updated = true
if(verbal) { if(debug) {
console.log(`Added logo '${c.icon}' to '${channel.id}'`) console.log(`Added logo '${c.icon}' to '${channel.id}'`)
} }
} }
@ -73,11 +75,14 @@ async function main() {
stats.updated++ stats.updated++
} }
} }
}
if(debug) {
console.log(`Sorting channels...`) console.log(`Sorting channels...`)
}
channels = util.sortByTitle(channels) channels = util.sortByTitle(channels)
console.log(`Writing result to file...`) console.log(`Updating '${country.url}'...`)
util.createFile(country.url, playlist.getHeader()) util.createFile(country.url, playlist.getHeader())
channels.forEach(channel => { channels.forEach(channel => {
util.appendToFile(country.url, channel.toString()) util.appendToFile(country.url, channel.toString())

View File

@ -52,27 +52,41 @@ class Channel {
} }
} }
function getGzipped(url) { function parsePlaylist(filename) {
return new Promise((resolve, reject) => { const parser = new M3U8FileParser()
var buffer = [] const content = readFile(filename)
https.get(url, function(res) { parser.read(content)
var gunzip = zlib.createGunzip() let results = parser.getResult()
res.pipe(gunzip) let contentMatches = content.match(/^.+(?=#|\n|\r)/g)
gunzip.on('data', function(data) { let head = contentMatches.length ? contentMatches[0] : null
buffer.push(data.toString()) let attrs = {}
}).on("end", function() { if(head) {
resolve(buffer.join("")) const parts = head.split(' ').filter(p => p !== '#EXTM3U').filter(p => p)
}).on("error", function(e) {
reject(e) for(const attr of parts) {
}) let attrParts = attr.split('=')
}).on('error', function(e) {
reject(e) attrs[attrParts[0]] = attrParts[1].replace(/\"/g, '')
}) }
}
results.attrs = attrs
return new Playlist({
attrs: results.attrs,
items: results.segments
}) })
} }
function readFile(filename) { function createChannel(data) {
return fs.readFileSync(path.resolve(__dirname) + `/../${filename}`, { encoding: "utf8" }) return new Channel({
id: data.id,
name: data.name,
logo: data.logo,
group: data.group,
url: data.url,
title: data.title
})
} }
async function loadEPG(url) { async function loadEPG(url) {
@ -105,40 +119,22 @@ async function loadEPG(url) {
}) })
} }
function createChannel(data) { function getGzipped(url) {
return new Channel({ return new Promise((resolve, reject) => {
id: data.id, var buffer = []
name: data.name, https.get(url, function(res) {
logo: data.logo, var gunzip = zlib.createGunzip()
group: data.group, res.pipe(gunzip)
url: data.url, gunzip.on('data', function(data) {
title: data.title buffer.push(data.toString())
}).on("end", function() {
resolve(buffer.join(""))
}).on("error", function(e) {
reject(e)
})
}).on('error', function(e) {
reject(e)
}) })
}
function parsePlaylist(filename) {
const parser = new M3U8FileParser()
const content = readFile(filename)
parser.read(content)
let results = parser.getResult()
let contentMatches = content.match(/^.+(?=#|\n|\r)/g)
let head = contentMatches.length ? contentMatches[0] : null
let attrs = {}
if(head) {
const parts = head.split(' ').filter(p => p !== '#EXTM3U').filter(p => p)
for(const attr of parts) {
let attrParts = attr.split('=')
attrs[attrParts[0]] = attrParts[1].replace(/\"/g, '')
}
}
results.attrs = attrs
return new Playlist({
attrs: results.attrs,
items: results.segments
}) })
} }
@ -159,6 +155,10 @@ function sortByTitle(arr) {
return arr.sort(byTitle) return arr.sort(byTitle)
} }
function readFile(filename) {
return fs.readFileSync(path.resolve(__dirname) + `/../${filename}`, { encoding: "utf8" })
}
function appendToFile(filename, data) { function appendToFile(filename, data) {
fs.appendFileSync(path.resolve(__dirname) + '/../' + filename, data) fs.appendFileSync(path.resolve(__dirname) + '/../' + filename, data)
} }