Files
epg/scripts/update-codes.js

90 lines
2.5 KiB
JavaScript
Raw Normal View History

2021-03-28 02:09:46 +03:00
const fs = require('fs')
const path = require('path')
const convert = require('xml-js')
2021-09-03 22:57:22 +03:00
const axios = require('axios')
2021-09-04 21:50:43 +03:00
const countries = require('./countries.json')
2021-10-07 22:59:31 +03:00
const file = require('./file.js')
2021-09-03 22:57:22 +03:00
async function main() {
2021-03-28 02:09:46 +03:00
console.log('Starting...')
2021-09-05 02:29:46 +03:00
2021-10-12 21:38:37 +03:00
const files = await file.list('.gh-pages/guides/**/*.xml')
2021-10-07 22:59:31 +03:00
2021-09-05 02:29:46 +03:00
let codes = {}
2021-09-04 06:58:36 +03:00
for (const filename of files) {
2021-10-07 22:59:31 +03:00
const url = filename.replace('.gh-pages', 'https://iptv-org.github.io/epg')
const channels = parseChannels(file.read(filename))
2021-09-04 06:58:36 +03:00
channels.forEach(channel => {
2021-09-16 01:37:33 +03:00
if (!codes[channel.tvg_id]) {
channel.guides = [url]
codes[channel.tvg_id] = channel
} else {
codes[channel.tvg_id].guides.push(url)
2021-09-04 06:58:36 +03:00
}
2021-03-28 02:09:46 +03:00
})
2021-09-04 06:58:36 +03:00
}
2021-03-28 02:09:46 +03:00
2021-09-04 06:58:36 +03:00
const sorted = Object.values(codes).sort((a, b) => {
if (a.display_name.toLowerCase() < b.display_name.toLowerCase()) return -1
if (a.display_name.toLowerCase() > b.display_name.toLowerCase()) return 1
return 0
2021-03-28 02:09:46 +03:00
})
2021-09-04 21:50:43 +03:00
writeToFile('.gh-pages/codes.json', convertToJSON(sorted))
const _items = {}
countries.forEach(country => {
_items[country.code] = {
...country,
2021-09-05 02:05:09 +03:00
expanded: false,
2021-09-04 21:50:43 +03:00
channels: []
}
})
sorted.forEach(channel => {
const item = _items[channel.country]
if (item) {
channel.hash = `${channel.display_name}:${channel.tvg_id}`.toLowerCase()
item.channels.push(channel)
}
})
2021-09-05 20:58:44 +03:00
writeToFile('.gh-pages/items.json', convertToJSON(_items))
2021-09-04 21:50:43 +03:00
2021-09-04 06:58:36 +03:00
console.log('Done')
2021-03-28 02:09:46 +03:00
}
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)
}
2021-09-04 05:01:56 +03:00
function convertToJSON(arr) {
2021-09-05 02:35:54 +03:00
return JSON.stringify(arr)
2021-09-04 05:01:56 +03:00
}
2021-10-07 22:59:31 +03:00
function parseChannels(xml) {
const result = convert.xml2js(xml)
2021-09-04 06:58:36 +03:00
const tv = result.elements.find(el => el.name === 'tv')
2021-03-28 02:09:46 +03:00
2021-09-04 06:58:36 +03:00
return tv.elements
2021-03-28 02:09:46 +03:00
.filter(el => el.name === 'channel')
2021-09-04 06:58:36 +03:00
.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]
2021-03-28 02:09:46 +03:00
2021-09-05 02:29:46 +03:00
return { tvg_id, display_name, logo, country }
2021-03-28 02:09:46 +03:00
})
}
main()