mirror of
https://github.com/iptv-org/epg.git
synced 2025-05-10 00:50:09 -04:00
Update update-api.js
This commit is contained in:
parent
2b03f23b46
commit
dc07784bf9
2 changed files with 76 additions and 67 deletions
|
@ -5,27 +5,39 @@ const DB_DIR = process.env.DB_DIR || 'scripts/database'
|
|||
const API_DIR = process.env.API_DIR || '.gh-pages/api'
|
||||
|
||||
async function main() {
|
||||
await saveToGuidesJson(await loadGuides())
|
||||
await saveToProgramsJson(await loadPrograms())
|
||||
await loadQueue()
|
||||
|
||||
const programs = await loadPrograms()
|
||||
const guides = await loadGuides(programs)
|
||||
|
||||
await saveToGuidesJson(guides)
|
||||
await saveToProgramsJson(programs)
|
||||
}
|
||||
|
||||
main()
|
||||
|
||||
async function loadGuides() {
|
||||
logger.info('Loading guides from database...')
|
||||
async function loadQueue() {
|
||||
logger.info('Loading queue...')
|
||||
|
||||
await db.queue.load()
|
||||
}
|
||||
|
||||
async function loadGuides(programs = []) {
|
||||
logger.info('Loading guides from database...')
|
||||
|
||||
programs = _.groupBy(programs, i => i._qid)
|
||||
|
||||
const queue = await db.queue.find({}).sort({ xmltv_id: 1 })
|
||||
|
||||
const output = []
|
||||
for (const item of queue) {
|
||||
item.groups.forEach(group => {
|
||||
if (!item.error) {
|
||||
const channelPrograms = programs[item._id]
|
||||
if (!item.error && channelPrograms) {
|
||||
output.push({
|
||||
channel: item.xmltv_id,
|
||||
channel: item.channel.xmltv_id,
|
||||
site: item.site,
|
||||
lang: item.lang,
|
||||
lang: item.channel.lang,
|
||||
url: `https://iptv-org.github.io/epg/guides/${group}.epg.xml`
|
||||
})
|
||||
}
|
||||
|
@ -37,10 +49,19 @@ async function loadGuides() {
|
|||
|
||||
async function loadPrograms() {
|
||||
logger.info('Loading programs from database...')
|
||||
|
||||
await db.programs.load()
|
||||
return await db.programs.find({})
|
||||
}
|
||||
|
||||
let programs = await db.programs.find({})
|
||||
async function saveToGuidesJson(guides = []) {
|
||||
const guidesPath = `${API_DIR}/guides.json`
|
||||
logger.info(`Saving to "${guidesPath}"...`)
|
||||
await file.create(guidesPath, JSON.stringify(guides))
|
||||
}
|
||||
|
||||
async function saveToProgramsJson(programs = []) {
|
||||
const programsPath = `${API_DIR}/programs.json`
|
||||
logger.info(`Saving to "${programsPath}"...`)
|
||||
|
||||
programs = programs.map(item => {
|
||||
const categories = Array.isArray(item.category) ? item.category : [item.category]
|
||||
|
@ -62,17 +83,5 @@ async function loadPrograms() {
|
|||
|
||||
programs = _.sortBy(programs, ['channel', 'site', 'start'])
|
||||
|
||||
return programs
|
||||
}
|
||||
|
||||
async function saveToGuidesJson(guides = []) {
|
||||
const guidesPath = `${API_DIR}/guides.json`
|
||||
logger.info(`Saving to "${guidesPath}"...`)
|
||||
await file.create(guidesPath, JSON.stringify(guides))
|
||||
}
|
||||
|
||||
async function saveToProgramsJson(programs = []) {
|
||||
const programsPath = `${API_DIR}/programs.json`
|
||||
logger.info(`Saving to "${programsPath}"...`)
|
||||
await file.create(programsPath, JSON.stringify(programs))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue