Update update-codes.js

This commit is contained in:
Aleksandr Statciuk 2021-09-03 22:57:22 +03:00
parent 6108d6b645
commit 62c69407b8

View file

@ -2,8 +2,11 @@ const fs = require('fs')
const glob = require('glob')
const path = require('path')
const convert = require('xml-js')
const axios = require('axios')
async function main() {
const iptvChannels = await axios.get('https://iptv-org.github.io/iptv/channels.json').then(response => response.data).catch(console.log)
function main() {
let codes = {}
console.log('Starting...')
glob('sites/**/*.xml', null, function (er, files) {
@ -11,17 +14,25 @@ function main() {
const channels = parseChannels(filename)
channels.forEach(channel => {
if (!codes[channel.xmltv_id + channel.name]) {
let logo = channel.logo || ''
if(!logo) {
let ch = iptvChannels.find(ch => ch.tvg.id === channel.xmltv_id)
if(ch && ch.logo) logo = ch.logo
}
codes[channel.xmltv_id + channel.name] = {
name: channel.name,
code: channel.xmltv_id
display_name: channel.name,
tvg_id: channel.xmltv_id,
country: channel.xmltv_id.split('.')[1],
logo,
}
}
})
})
const sorted = Object.values(codes).sort((a, b) => {
if (a.name.toLowerCase() < b.name.toLowerCase()) return -1
if (a.name.toLowerCase() > b.name.toLowerCase()) return 1
if (a.display_name.toLowerCase() < b.display_name.toLowerCase()) return -1
if (a.display_name.toLowerCase() > b.display_name.toLowerCase()) return 1
return 0
})
writeToFile('codes.csv', convertToCSV(sorted))
@ -40,9 +51,9 @@ function writeToFile(filename, data) {
}
function convertToCSV(arr) {
let string = 'Channel Name,EPG Code (tvg-id)\n'
let string = 'display_name,tvg_id,country,logo\n'
for (const item of arr) {
string += `${item.name},${item.code}\n`
string += `${item.display_name},${item.tvg_id},${item.country},${item.logo}\n`
}
return string