Update update-codes.js

This commit is contained in:
Aleksandr Statciuk 2021-10-13 05:23:12 +03:00
parent 7640aaaacc
commit fa4fd2fa89

View file

@ -1,9 +1,6 @@
const fs = require('fs')
const path = require('path')
const convert = require('xml-js')
const axios = require('axios')
const countries = require('./countries.json') const countries = require('./countries.json')
const file = require('./file.js') const file = require('./file.js')
const parser = require('epg-parser')
async function main() { async function main() {
console.log('Starting...') console.log('Starting...')
@ -13,13 +10,19 @@ async function main() {
let codes = {} let codes = {}
for (const filename of files) { for (const filename of files) {
const url = filename.replace('.gh-pages', 'https://iptv-org.github.io/epg') const url = filename.replace('.gh-pages', 'https://iptv-org.github.io/epg')
const channels = parseChannels(file.read(filename)) const epg = file.read(filename)
channels.forEach(channel => { const parsed = parser.parse(epg)
if (!codes[channel.tvg_id]) { parsed.channels.forEach(channel => {
if (!codes[channel.id]) {
channel.guides = [url] channel.guides = [url]
codes[channel.tvg_id] = channel codes[channel.id] = {
tvg_id: channel.id,
display_name: channel.name[0].value,
logo: channel.icon[0],
country: channel.id.split('.')[1]
}
} else { } else {
codes[channel.tvg_id].guides.push(url) codes[channel.id].guides.push(url)
} }
}) })
} }
@ -29,7 +32,8 @@ async function main() {
if (a.display_name.toLowerCase() > b.display_name.toLowerCase()) return 1 if (a.display_name.toLowerCase() > b.display_name.toLowerCase()) return 1
return 0 return 0
}) })
writeToFile('.gh-pages/codes.json', convertToJSON(sorted)) console.log(`Saving '.gh-pages/codes.json'...`)
file.write('.gh-pages/codes.json', JSON.stringify(sorted))
const _items = {} const _items = {}
countries.forEach(country => { countries.forEach(country => {
@ -47,43 +51,10 @@ async function main() {
item.channels.push(channel) item.channels.push(channel)
} }
}) })
writeToFile('.gh-pages/items.json', convertToJSON(_items)) console.log(`Saving '.gh-pages/items.json'...`)
file.write('.gh-pages/items.json', JSON.stringify(_items))
console.log('Done') console.log('Done')
} }
function writeToFile(filename, data) {
console.log(`Saving all codes to '${filename}'...`)
const dir = path.resolve(path.dirname(filename))
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true })
}
fs.writeFileSync(path.resolve(filename), data)
}
function convertToJSON(arr) {
return JSON.stringify(arr)
}
function parseChannels(xml) {
const result = convert.xml2js(xml)
const tv = result.elements.find(el => el.name === 'tv')
return tv.elements
.filter(el => el.name === 'channel')
.map(channel => {
const tvg_id = (channel.attributes || { id: '' }).id
const logo = (channel.elements.find(el => el.name === 'icon') || { attributes: { src: '' } })
.attributes.src
const displayName = channel.elements.find(el => el.name === 'display-name')
const display_name = displayName
? displayName.elements.find(el => el.type === 'text').text
: null
const country = tvg_id.split('.')[1]
return { tvg_id, display_name, logo, country }
})
}
main() main()