mirror of
https://github.com/iptv-org/epg.git
synced 2025-05-10 00:50:09 -04:00
wip
This commit is contained in:
parent
06dc448456
commit
f8981e478e
4 changed files with 169 additions and 40 deletions
109
scripts/commands/update-api.js
Normal file
109
scripts/commands/update-api.js
Normal file
|
@ -0,0 +1,109 @@
|
||||||
|
const { db, logger, file, xml } = require('../core')
|
||||||
|
const _ = require('lodash')
|
||||||
|
|
||||||
|
let channels = []
|
||||||
|
let programs = []
|
||||||
|
let sources = {}
|
||||||
|
|
||||||
|
const DB_DIR = process.env.DB_DIR || 'scripts/database'
|
||||||
|
const API_DIR = process.env.API_DIR || '.gh-pages/api'
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
await setUp()
|
||||||
|
|
||||||
|
await generateChannelsJson()
|
||||||
|
await generateProgramsJson()
|
||||||
|
}
|
||||||
|
|
||||||
|
main()
|
||||||
|
|
||||||
|
async function setUp() {
|
||||||
|
channels = await loadChannels()
|
||||||
|
programs = await loadPrograms()
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadChannels() {
|
||||||
|
let items = await db.channels.find({}).sort({ xmltv_id: 1 })
|
||||||
|
|
||||||
|
let output = {}
|
||||||
|
items.forEach(item => {
|
||||||
|
if (!output[item.xmltv_id]) {
|
||||||
|
const countryCode = item.xmltv_id.split('.')[1]
|
||||||
|
|
||||||
|
output[item.xmltv_id] = {
|
||||||
|
id: item.xmltv_id,
|
||||||
|
name: [item.name],
|
||||||
|
logo: item.logo || null,
|
||||||
|
country: countryCode ? countryCode.toUpperCase() : null
|
||||||
|
}
|
||||||
|
} 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 Object.values(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadPrograms() {
|
||||||
|
let items = await db.programs.find({})
|
||||||
|
|
||||||
|
items = _.sortBy(items, ['channel', 'start'])
|
||||||
|
items = _.groupBy(items, '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 items
|
||||||
|
}
|
||||||
|
|
||||||
|
async function generateChannelsJson() {
|
||||||
|
logger.info('Generating channels.json...')
|
||||||
|
|
||||||
|
await file.create(`${API_DIR}/channels.json`, JSON.stringify(channels))
|
||||||
|
}
|
||||||
|
|
||||||
|
async function generateProgramsJson() {
|
||||||
|
logger.info('Generating programs.json...')
|
||||||
|
|
||||||
|
await file.create(`${API_DIR}/programs.json`, JSON.stringify(programs))
|
||||||
|
}
|
|
@ -6,18 +6,32 @@ let programs = []
|
||||||
let sources = {}
|
let sources = {}
|
||||||
|
|
||||||
const DB_DIR = process.env.DB_DIR || 'scripts/database'
|
const DB_DIR = process.env.DB_DIR || 'scripts/database'
|
||||||
const PUBLIC_DIR = process.env.PUBLIC_DIR || '.gh-pages'
|
const GUIDES_DIR = process.env.GUIDES_DIR || '.gh-pages/guides'
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
await setUp()
|
await setUp()
|
||||||
|
|
||||||
await generateChannelsJson()
|
await generateMainXML()
|
||||||
await generateProgramsJson()
|
|
||||||
await generateEpgXML()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
async function generateMainXML() {
|
||||||
|
logger.info(`Generating epg.xml...`)
|
||||||
|
|
||||||
|
const output = {}
|
||||||
|
const filteredChannels = Object.keys(programs)
|
||||||
|
output.channels = 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(`${GUIDES_DIR}/epg.xml`, xml.create(output))
|
||||||
|
}
|
||||||
|
|
||||||
async function setUp() {
|
async function setUp() {
|
||||||
channels = await loadChannels()
|
channels = await loadChannels()
|
||||||
programs = await loadPrograms()
|
programs = await loadPrograms()
|
||||||
|
@ -96,31 +110,3 @@ async function loadPrograms() {
|
||||||
|
|
||||||
return items
|
return items
|
||||||
}
|
}
|
||||||
|
|
||||||
async function generateChannelsJson() {
|
|
||||||
logger.info('Generating channels.json...')
|
|
||||||
|
|
||||||
await file.create(`${PUBLIC_DIR}/api/channels.json`, JSON.stringify(channels))
|
|
||||||
}
|
|
||||||
|
|
||||||
async function generateProgramsJson() {
|
|
||||||
logger.info('Generating programs.json...')
|
|
||||||
|
|
||||||
await file.create(`${PUBLIC_DIR}/api/programs.json`, JSON.stringify(programs))
|
|
||||||
}
|
|
||||||
|
|
||||||
async function generateEpgXML() {
|
|
||||||
logger.info(`Generating epg.xml...`)
|
|
||||||
|
|
||||||
const output = {}
|
|
||||||
const filteredChannels = Object.keys(programs)
|
|
||||||
output.channels = 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}/guides/epg.xml`, xml.create(output))
|
|
||||||
}
|
|
|
@ -16,7 +16,7 @@ beforeEach(() => {
|
||||||
)
|
)
|
||||||
|
|
||||||
execSync(
|
execSync(
|
||||||
'DB_DIR=tests/__data__/temp/database PUBLIC_DIR=tests/__data__/output node scripts/commands/generate-guides.js',
|
'DB_DIR=tests/__data__/temp/database API_DIR=tests/__data__/output/api node scripts/commands/update-api.js',
|
||||||
{ encoding: 'utf8' }
|
{ encoding: 'utf8' }
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
@ -39,13 +39,6 @@ it('can generate programs.json', () => {
|
||||||
expect(output).toBe(expected)
|
expect(output).toBe(expected)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('can generate epg.xml', () => {
|
|
||||||
const output = content('tests/__data__/output/guides/epg.xml')
|
|
||||||
const expected = content('tests/__data__/expected/guides/epg.xml')
|
|
||||||
|
|
||||||
expect(output).toBe(expected)
|
|
||||||
})
|
|
||||||
|
|
||||||
function content(filepath) {
|
function content(filepath) {
|
||||||
const data = fs.readFileSync(path.resolve(filepath), {
|
const data = fs.readFileSync(path.resolve(filepath), {
|
||||||
encoding: 'utf8'
|
encoding: 'utf8'
|
41
tests/commands/update-guides.test.js
Normal file
41
tests/commands/update-guides.test.js
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
const fs = require('fs')
|
||||||
|
const path = require('path')
|
||||||
|
const { execSync } = require('child_process')
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
fs.rmdirSync('tests/__data__/output', { recursive: true })
|
||||||
|
fs.mkdirSync('tests/__data__/output')
|
||||||
|
fs.mkdirSync('tests/__data__/temp/database', { recursive: true })
|
||||||
|
fs.copyFileSync(
|
||||||
|
'tests/__data__/input/database/channels.db',
|
||||||
|
'tests/__data__/temp/database/channels.db'
|
||||||
|
)
|
||||||
|
fs.copyFileSync(
|
||||||
|
'tests/__data__/input/database/programs.db',
|
||||||
|
'tests/__data__/temp/database/programs.db'
|
||||||
|
)
|
||||||
|
|
||||||
|
execSync(
|
||||||
|
'DB_DIR=tests/__data__/temp/database GUIDES_DIR=tests/__data__/output/guides node scripts/commands/update-guides.js',
|
||||||
|
{ encoding: 'utf8' }
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
fs.rmdirSync('tests/__data__/temp', { recursive: true })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('can generate epg.xml', () => {
|
||||||
|
const output = content('tests/__data__/output/guides/epg.xml')
|
||||||
|
const expected = content('tests/__data__/expected/guides/epg.xml')
|
||||||
|
|
||||||
|
expect(output).toBe(expected)
|
||||||
|
})
|
||||||
|
|
||||||
|
function content(filepath) {
|
||||||
|
const data = fs.readFileSync(path.resolve(filepath), {
|
||||||
|
encoding: 'utf8'
|
||||||
|
})
|
||||||
|
|
||||||
|
return JSON.stringify(data)
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue