Files
epg/scripts/commands/update-api.js

47 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-01-10 00:34:05 +03:00
const { db, logger, file, xml } = require('../core')
2022-01-09 16:09:19 +03:00
const _ = require('lodash')
2022-01-09 20:14:41 +03:00
const DB_DIR = process.env.DB_DIR || 'scripts/database'
2022-01-10 18:27:22 +03:00
const API_DIR = process.env.API_DIR || '.gh-pages/api'
2022-01-09 16:09:19 +03:00
async function main() {
2022-01-09 20:14:41 +03:00
await generateChannelsJson()
2022-01-09 16:09:19 +03:00
}
main()
2022-01-14 16:44:06 +03:00
async function generateChannelsJson() {
logger.info('Generating channels.json...')
const channels = await loadChannels()
await file.create(`${API_DIR}/channels.json`, JSON.stringify(channels))
2022-01-09 20:14:41 +03:00
}
2022-01-09 22:03:45 +03:00
async function loadChannels() {
let items = await db.channels.find({}).sort({ xmltv_id: 1 })
2022-01-09 18:15:38 +03:00
2022-01-09 21:43:57 +03:00
let output = {}
2022-01-10 00:34:05 +03:00
items.forEach(item => {
if (!output[item.xmltv_id]) {
output[item.xmltv_id] = {
id: item.xmltv_id,
2022-01-15 03:04:15 +03:00
name: [],
2022-01-10 00:34:05 +03:00
logo: item.logo || null,
2022-01-15 03:04:15 +03:00
country: item.country,
guides: []
2022-01-09 18:15:38 +03:00
}
} else {
2022-01-10 00:34:05 +03:00
output[item.xmltv_id].logo = output[item.xmltv_id].logo || item.logo
2022-01-09 18:15:38 +03:00
}
2022-01-10 00:34:05 +03:00
2022-01-15 03:04:15 +03:00
output[item.xmltv_id].name.push(item.name)
2022-01-10 00:34:05 +03:00
output[item.xmltv_id].name = _.uniq(output[item.xmltv_id].name)
2022-01-15 03:04:15 +03:00
output[item.xmltv_id].guides.push(
`https://iptv-org.github.io/epg/guides/${item.gid}/${item.site}.epg.xml`
)
2022-01-09 18:15:38 +03:00
})
2022-01-09 22:03:45 +03:00
return Object.values(output)
2022-01-09 16:09:19 +03:00
}