Update format.js

This commit is contained in:
freearhey 2021-01-30 02:26:20 +03:00
parent 60b6597557
commit eb7b8c55b0
1 changed files with 34 additions and 40 deletions

View File

@ -1,5 +1,6 @@
const { program } = require('commander') const { program } = require('commander')
const helper = require('./helper') const parser = require('./parser')
const utils = require('./utils')
const axios = require('axios') const axios = require('axios')
const ProgressBar = require('progress') const ProgressBar = require('progress')
const https = require('https') const https = require('https')
@ -28,9 +29,10 @@ const instance = axios.create({
let globalBuffer = [] let globalBuffer = []
async function main() { async function main() {
const index = parseIndex() const playlists = parseIndex()
for (const item of index.items) {
await loadPlaylist(item.url) for (const playlist of playlists) {
await loadPlaylist(playlist.url)
.then(addToBuffer) .then(addToBuffer)
.then(sortChannels) .then(sortChannels)
.then(removeDuplicates) .then(removeDuplicates)
@ -40,7 +42,7 @@ async function main() {
.then(done) .then(done)
} }
if (index.items.length) { if (playlists.length) {
await loadPlaylist('channels/unsorted.m3u') await loadPlaylist('channels/unsorted.m3u')
.then(removeUnsortedDuplicates) .then(removeUnsortedDuplicates)
.then(sortChannels) .then(sortChannels)
@ -53,38 +55,30 @@ async function main() {
function parseIndex() { function parseIndex() {
console.info(`Parsing 'index.m3u'...`) console.info(`Parsing 'index.m3u'...`)
const playlist = helper.parsePlaylist('index.m3u') let playlists = parser.parseIndex()
playlist.items = helper playlists = utils
.filterPlaylists(playlist.items, config.country, config.exclude) .filterPlaylists(playlists, config.country, config.exclude)
.filter(i => i.url !== 'channels/unsorted.m3u') .filter(i => i.url !== 'channels/unsorted.m3u')
console.info(`Found ${playlist.items.length} playlist(s)\n`) console.info(`Found ${playlists.length} playlist(s)\n`)
return playlist return playlists
} }
async function loadPlaylist(url) { async function loadPlaylist(url) {
console.info(`Processing '${url}'...`) console.info(`Processing '${url}'...`)
const playlist = helper.parsePlaylist(url) return parser.parsePlaylist(url)
playlist.url = url
playlist.items = playlist.items
.map(item => {
return helper.createChannel(item, playlist)
})
.filter(i => i.url)
return playlist
} }
async function addToBuffer(playlist) { async function addToBuffer(playlist) {
if (playlist.url === 'channels/unsorted.m3u') return playlist if (playlist.url === 'channels/unsorted.m3u') return playlist
globalBuffer = globalBuffer.concat(playlist.items) globalBuffer = globalBuffer.concat(playlist.channels)
return playlist return playlist
} }
async function sortChannels(playlist) { async function sortChannels(playlist) {
console.info(` Sorting channels...`) console.info(` Sorting channels...`)
playlist.items = helper.sortBy(playlist.items, ['name', 'url']) playlist.channels = utils.sortBy(playlist.channels, ['name', 'url'])
return playlist return playlist
} }
@ -92,7 +86,7 @@ async function sortChannels(playlist) {
async function removeDuplicates(playlist) { async function removeDuplicates(playlist) {
console.info(` Looking for duplicates...`) console.info(` Looking for duplicates...`)
let buffer = {} let buffer = {}
const items = playlist.items.filter(i => { const channels = playlist.channels.filter(i => {
const result = typeof buffer[i.url] === 'undefined' const result = typeof buffer[i.url] === 'undefined'
if (result) { if (result) {
buffer[i.url] = true buffer[i.url] = true
@ -101,7 +95,7 @@ async function removeDuplicates(playlist) {
return result return result
}) })
playlist.items = items playlist.channels = channels
return playlist return playlist
} }
@ -109,31 +103,31 @@ async function removeDuplicates(playlist) {
async function detectResolution(playlist) { async function detectResolution(playlist) {
if (!config.resolution) return playlist if (!config.resolution) return playlist
const bar = new ProgressBar(' Detecting resolution: [:bar] :current/:total (:percent) ', { const bar = new ProgressBar(' Detecting resolution: [:bar] :current/:total (:percent) ', {
total: playlist.items.length total: playlist.channels.length
}) })
const results = [] const results = []
for (const item of playlist.items) { for (const channel of playlist.channels) {
bar.tick() bar.tick()
const url = item.url const url = channel.url
const response = await instance const response = await instance
.get(url) .get(url)
.then(helper.sleep(config.delay)) .then(utils.sleep(config.delay))
.catch(err => {}) .catch(err => {})
if (response) { if (response) {
if (response.status === 200) { if (response.status === 200) {
if (/^#EXTM3U/.test(response.data)) { if (/^#EXTM3U/.test(response.data)) {
const resolution = parseResolution(response.data) const resolution = parseResolution(response.data)
if (resolution) { if (resolution) {
item.resolution = resolution channel.resolution = resolution
} }
} }
} }
} }
results.push(item) results.push(channel)
} }
playlist.items = results playlist.channels = results
return playlist return playlist
} }
@ -160,12 +154,12 @@ async function updateFromEPG(playlist) {
console.info(` Adding data from '${tvgUrl}'...`) console.info(` Adding data from '${tvgUrl}'...`)
return helper return utils
.parseEPG(tvgUrl) .parseEPG(tvgUrl)
.then(epg => { .then(epg => {
if (!epg) return playlist if (!epg) return playlist
playlist.items.map(channel => { playlist.channels.map(channel => {
if (!channel.tvg.id) return channel if (!channel.tvg.id) return channel
const epgItem = epg.channels[channel.tvg.id] const epgItem = epg.channels[channel.tvg.id]
if (!epgItem) return channel if (!epgItem) return channel
@ -173,7 +167,7 @@ async function updateFromEPG(playlist) {
channel.tvg.name = epgItem.name[0].value channel.tvg.name = epgItem.name[0].value
} }
if (!channel.languages.length && epgItem.name.length && epgItem.name[0].lang) { if (!channel.languages.length && epgItem.name.length && epgItem.name[0].lang) {
channel.languages = helper.parseLanguages(epgItem.name[0].lang) channel.languages = utils.parseLanguages(epgItem.name[0].lang)
} }
if (!channel.logo && epgItem.icon.length) { if (!channel.logo && epgItem.icon.length) {
channel.logo = epgItem.icon[0] channel.logo = epgItem.icon[0]
@ -190,25 +184,25 @@ async function updateFromEPG(playlist) {
async function removeUnsortedDuplicates(playlist) { async function removeUnsortedDuplicates(playlist) {
console.info(` Looking for duplicates...`) console.info(` Looking for duplicates...`)
const urls = globalBuffer.map(i => i.url) const urls = globalBuffer.map(i => i.url)
const items = playlist.items.filter(i => !urls.includes(i.url)) const channels = playlist.channels.filter(i => !urls.includes(i.url))
if (items.length === playlist.items.length) return playlist if (channels.length === playlist.channels.length) return playlist
playlist.items = items playlist.channels = channels
return playlist return playlist
} }
async function updatePlaylist(playlist) { async function updatePlaylist(playlist) {
const original = helper.readFile(playlist.url) const original = utils.readFile(playlist.url)
let output = playlist.getHeader() let output = playlist.getHeader()
for (let channel of playlist.items) { for (let channel of playlist.channels) {
output += channel.toShortString() output += channel.toString(true)
} }
if (original === output) { if (original === output) {
console.info(`No changes have been made.`) console.info(`No changes have been made.`)
return false return false
} else { } else {
helper.createFile(playlist.url, output) utils.createFile(playlist.url, output)
console.info(`Playlist has been updated.`) console.info(`Playlist has been updated.`)
} }