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
b344682046
commit
e3e0ef3b9b
2 changed files with 6 additions and 82 deletions
|
@ -1,25 +1,21 @@
|
||||||
const { db, logger, file, xml } = require('../core')
|
const { db, logger, file, xml } = require('../core')
|
||||||
const _ = require('lodash')
|
const _ = require('lodash')
|
||||||
|
|
||||||
let channels = []
|
|
||||||
let programs = []
|
|
||||||
let sources = {}
|
|
||||||
|
|
||||||
const DB_DIR = process.env.DB_DIR || 'scripts/database'
|
const DB_DIR = process.env.DB_DIR || 'scripts/database'
|
||||||
const API_DIR = process.env.API_DIR || '.gh-pages/api'
|
const API_DIR = process.env.API_DIR || '.gh-pages/api'
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
await setUp()
|
|
||||||
|
|
||||||
await generateChannelsJson()
|
await generateChannelsJson()
|
||||||
await generateProgramsJson()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
||||||
async function setUp() {
|
async function generateChannelsJson() {
|
||||||
channels = await loadChannels()
|
logger.info('Generating channels.json...')
|
||||||
programs = await loadPrograms()
|
|
||||||
|
const channels = await loadChannels()
|
||||||
|
|
||||||
|
await file.create(`${API_DIR}/channels.json`, JSON.stringify(channels))
|
||||||
}
|
}
|
||||||
|
|
||||||
async function loadChannels() {
|
async function loadChannels() {
|
||||||
|
@ -44,64 +40,3 @@ async function loadChannels() {
|
||||||
|
|
||||||
return Object.values(output)
|
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))
|
|
||||||
}
|
|
||||||
|
|
|
@ -10,10 +10,6 @@ beforeEach(() => {
|
||||||
'tests/__data__/input/database/channels.db',
|
'tests/__data__/input/database/channels.db',
|
||||||
'tests/__data__/temp/database/channels.db'
|
'tests/__data__/temp/database/channels.db'
|
||||||
)
|
)
|
||||||
fs.copyFileSync(
|
|
||||||
'tests/__data__/input/database/programs.db',
|
|
||||||
'tests/__data__/temp/database/programs.db'
|
|
||||||
)
|
|
||||||
|
|
||||||
execSync(
|
execSync(
|
||||||
'DB_DIR=tests/__data__/temp/database API_DIR=tests/__data__/output/api node scripts/commands/update-api.js',
|
'DB_DIR=tests/__data__/temp/database API_DIR=tests/__data__/output/api node scripts/commands/update-api.js',
|
||||||
|
@ -32,13 +28,6 @@ it('can generate channels.json', () => {
|
||||||
expect(output).toBe(expected)
|
expect(output).toBe(expected)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('can generate programs.json', () => {
|
|
||||||
const output = content('tests/__data__/output/api/programs.json')
|
|
||||||
const expected = content('tests/__data__/expected/api/programs.json')
|
|
||||||
|
|
||||||
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'
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue