mirror of
https://github.com/iptv-org/epg.git
synced 2025-05-09 16:40:07 -04:00
Update update-guides.js
This commit is contained in:
parent
e3e0ef3b9b
commit
5a19880855
5 changed files with 67 additions and 211 deletions
|
@ -1,81 +1,41 @@
|
|||
const { db, logger, file, xml } = require('../core')
|
||||
const _ = require('lodash')
|
||||
|
||||
let sources = {}
|
||||
|
||||
const DB_DIR = process.env.DB_DIR || 'scripts/database'
|
||||
const PUBLIC_DIR = process.env.PUBLIC_DIR || '.gh-pages'
|
||||
|
||||
async function main() {
|
||||
await generateEpgXML()
|
||||
await generateGuides()
|
||||
}
|
||||
|
||||
main()
|
||||
|
||||
async function generateEpgXML() {
|
||||
logger.info(`Generating epg.xml...`)
|
||||
|
||||
const channels = await loadChannels()
|
||||
const programs = await loadPrograms()
|
||||
|
||||
const output = {}
|
||||
const filteredChannels = Object.keys(programs)
|
||||
output.channels = _.flatten(Object.values(channels))
|
||||
.filter(c => filteredChannels.includes(c.id))
|
||||
.map(c => {
|
||||
c.site = sources[c.id]
|
||||
return c
|
||||
})
|
||||
output.programs = _.flatten(Object.values(programs))
|
||||
|
||||
await file.create(`${PUBLIC_DIR}/epg.xml`, xml.create(output))
|
||||
}
|
||||
|
||||
async function generateGuides() {
|
||||
logger.info(`Generating guides/...`)
|
||||
|
||||
let channels = await db.channels.find({}).sort({ xmltv_id: 1 })
|
||||
const programs = await db.programs.find({}).sort({ channel: 1, start: 1 })
|
||||
const grouped = _.groupBy(programs, i => `${i.country.toLowerCase()}/${i.site}`)
|
||||
const channels = await db.channels.find({}).sort({ xmltv_id: 1 })
|
||||
const programs = await loadPrograms()
|
||||
const grouped = _.groupBy(programs, i => `${i.gid}/${i.site}.epg.xml`)
|
||||
|
||||
for (let relativePath in grouped) {
|
||||
const filepath = `${PUBLIC_DIR}/guides/${relativePath}`
|
||||
const groupProgs = grouped[relativePath]
|
||||
const groupChannels = Object.keys(_.groupBy(groupProgs, i => `${i.site}_${i.channel}`)).map(
|
||||
key => {
|
||||
let [site, channel] = key.split('_')
|
||||
|
||||
return channels.find(i => i.xmltv_id === channel && i.site === site)
|
||||
}
|
||||
)
|
||||
|
||||
for (let groupId in grouped) {
|
||||
const filepath = `${PUBLIC_DIR}/guides/${groupId}.epg.xml`
|
||||
const groupProgs = grouped[groupId]
|
||||
const groupChannels = Object.keys(_.groupBy(groupProgs, 'channel')).map(key => {
|
||||
let [_, site] = groupId.split('/')
|
||||
return channels.find(i => i.xmltv_id === key && i.site === site)
|
||||
})
|
||||
const output = xml.create({ channels: groupChannels, programs: groupProgs })
|
||||
|
||||
await file.create(filepath, output)
|
||||
}
|
||||
}
|
||||
|
||||
async function loadChannels() {
|
||||
let items = await db.channels.find({}).sort({ xmltv_id: 1 })
|
||||
|
||||
let output = {}
|
||||
items.forEach(item => {
|
||||
if (!output[item.xmltv_id]) {
|
||||
output[item.xmltv_id] = {
|
||||
id: item.xmltv_id,
|
||||
name: [item.name],
|
||||
logo: item.logo || null,
|
||||
country: item.country
|
||||
}
|
||||
} else {
|
||||
output[item.xmltv_id].logo = output[item.xmltv_id].logo || item.logo
|
||||
output[item.xmltv_id].name.push(item.name)
|
||||
}
|
||||
|
||||
output[item.xmltv_id].name = _.uniq(output[item.xmltv_id].name)
|
||||
})
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
async function loadPrograms() {
|
||||
let programs = await db.programs.find({})
|
||||
let programs = await db.programs.find({}).sort({ channel: 1, start: 1 })
|
||||
|
||||
programs = programs.map(program => {
|
||||
return {
|
||||
|
@ -89,52 +49,10 @@ async function loadPrograms() {
|
|||
stop: program.stop,
|
||||
site: program.site,
|
||||
country: program.country,
|
||||
gid: program.gid,
|
||||
_id: program._id
|
||||
}
|
||||
})
|
||||
|
||||
programs = _.sortBy(programs, ['channel', 'start'])
|
||||
programs = _.groupBy(programs, 'channel')
|
||||
|
||||
// for (let channel in items) {
|
||||
// let channelPrograms = items[channel]
|
||||
// channelPrograms = Object.values(_.groupBy(channelPrograms, i => i.site))[0]
|
||||
// let slots = _.groupBy(channelPrograms, i => `${i.start}_${i.stop}`)
|
||||
|
||||
// for (let slotId in slots) {
|
||||
// let program = {
|
||||
// channel,
|
||||
// title: [],
|
||||
// description: [],
|
||||
// categories: [],
|
||||
// image: null,
|
||||
// start: null,
|
||||
// stop: null
|
||||
// }
|
||||
|
||||
// slots[slotId].forEach(item => {
|
||||
// if (item.title) program.title.push({ lang: item.lang, value: item.title })
|
||||
// if (item.description)
|
||||
// program.description.push({
|
||||
// lang: item.lang,
|
||||
// value: item.description
|
||||
// })
|
||||
// if (item.category) program.categories.push({ lang: item.lang, value: item.category })
|
||||
// program.image = program.image || item.icon
|
||||
// program.start = item.start
|
||||
// program.stop = item.stop
|
||||
// sources[channel] = item.site
|
||||
// })
|
||||
|
||||
// program.title = _.uniqBy(program.title, 'lang')
|
||||
// program.description = _.uniqBy(program.description, 'lang')
|
||||
// program.categories = _.uniqBy(program.categories, 'lang')
|
||||
|
||||
// slots[slotId] = program
|
||||
// }
|
||||
|
||||
// items[channel] = Object.values(slots)
|
||||
// }
|
||||
|
||||
return programs
|
||||
}
|
||||
|
|
|
@ -7,13 +7,9 @@ const xml = {}
|
|||
xml.create = function ({ channels, programs }) {
|
||||
let output = `<?xml version="1.0" encoding="UTF-8" ?><tv>\n`
|
||||
for (let channel of channels) {
|
||||
output += `<channel id="${escapeString(channel.id)}">`
|
||||
channel.name.forEach(name => {
|
||||
output += `<display-name>${escapeString(name)}</display-name>`
|
||||
})
|
||||
if (channel.logo) {
|
||||
output += `<icon src="${escapeString(channel.logo)}"/>`
|
||||
}
|
||||
output += `<channel id="${escapeString(channel.xmltv_id)}">`
|
||||
output += `<display-name>${escapeString(channel.name)}</display-name>`
|
||||
if (channel.logo) output += `<icon src="${escapeString(channel.logo)}"/>`
|
||||
if (channel.site) output += `<url>https://${channel.site}</url>`
|
||||
output += `</channel>\n`
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue