mirror of
https://github.com/iptv-org/epg.git
synced 2025-05-09 16:40:07 -04:00
Update create-queue.js
This commit is contained in:
parent
62e79d982c
commit
ca3913f357
5 changed files with 47 additions and 15 deletions
|
@ -1,4 +1,4 @@
|
|||
const { db, file, parser, logger } = require('../core')
|
||||
const { db, file, parser, logger, date } = require('../core')
|
||||
const { program } = require('commander')
|
||||
const { shuffle } = require('lodash')
|
||||
|
||||
|
@ -9,6 +9,7 @@ const options = program
|
|||
parser.parseNumber,
|
||||
256
|
||||
)
|
||||
.option('--days <days>', 'Number of days for which to grab the program', parser.parseNumber, 1)
|
||||
.option('--channels <channels>', 'Set path to channels.xml file', 'sites/**/*.channels.xml')
|
||||
.parse(process.argv)
|
||||
.opts()
|
||||
|
@ -30,6 +31,8 @@ async function createQueue() {
|
|||
let queue = {}
|
||||
|
||||
const files = await file.list(options.channels)
|
||||
const utcDate = date.getUTC()
|
||||
const dates = Array.from({ length: options.days }, (_, i) => utcDate.add(i, 'd'))
|
||||
for (const filepath of files) {
|
||||
const dir = file.dirname(filepath)
|
||||
const { site, channels: items } = await parser.parseChannels(filepath)
|
||||
|
@ -42,23 +45,32 @@ async function createQueue() {
|
|||
const groupId = `${region}/${site}`
|
||||
for (const item of items) {
|
||||
if (!item.site || !item.site_id || !item.xmltv_id) continue
|
||||
const key = `${item.site}:${item.site_id}`
|
||||
if (!queue[key]) {
|
||||
item.configPath = configPath
|
||||
item.groups = []
|
||||
for (const d of dates) {
|
||||
const dString = d.toJSON()
|
||||
const key = `${item.site}:${item.site_id}:${dString}`
|
||||
console.log(key)
|
||||
if (!queue[key]) {
|
||||
queue[key] = {
|
||||
lang: item.lang,
|
||||
xmltv_id: item.xmltv_id,
|
||||
site_id: item.site_id,
|
||||
site: item.site,
|
||||
date: dString,
|
||||
configPath: item.configPath,
|
||||
groups: []
|
||||
}
|
||||
}
|
||||
|
||||
queue[key] = item
|
||||
}
|
||||
|
||||
if (!queue[key].groups.includes(groupId)) {
|
||||
queue[key].groups.push(groupId)
|
||||
if (!queue[key].groups.includes(groupId)) {
|
||||
queue[key].groups.push(groupId)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
queue = Object.values(queue)
|
||||
|
||||
logger.info(`Found ${queue.length} items`)
|
||||
logger.info(`Added ${queue.length} items`)
|
||||
|
||||
return queue
|
||||
}
|
||||
|
|
13
scripts/core/date.js
Normal file
13
scripts/core/date.js
Normal file
|
@ -0,0 +1,13 @@
|
|||
const dayjs = require('dayjs')
|
||||
const utc = require('dayjs/plugin/utc')
|
||||
dayjs.extend(utc)
|
||||
|
||||
const date = {}
|
||||
|
||||
date.getUTC = function (d = null) {
|
||||
if (typeof d === 'string') return dayjs.utc(d).startOf('d')
|
||||
|
||||
return dayjs.utc().startOf('d')
|
||||
}
|
||||
|
||||
module.exports = date
|
|
@ -5,3 +5,4 @@ exports.parser = require('./parser')
|
|||
exports.timer = require('./timer')
|
||||
exports.markdown = require('./markdown')
|
||||
exports.api = require('./api')
|
||||
exports.date = require('./date')
|
||||
|
|
|
@ -1,2 +1,4 @@
|
|||
{"lang":"en","xmltv_id":"CNNInternationalEurope2.us","site_id":"141","name":"CNN International Europe 2","site":"example.com","configPath":"tests/__data__/input/sites/example.com.config.js","groups":["ca-nl/example.com"],"cluster_id":1,"_id":"Q1l5y46bT530JS2z"}
|
||||
{"lang":"ru","xmltv_id":"CNNInternationalEurope.us","site_id":"140","name":"CNN International Europe","site":"example.com","configPath":"tests/__data__/input/sites/example.com.config.js","groups":["ca-nl/example.com"],"cluster_id":1,"_id":"auE4QTbPiCEcHyf5"}
|
||||
{"lang":"en","xmltv_id":"CNNInternationalEurope2.us","site_id":"141","site":"example.com","date":"2022-01-30T00:00:00.000Z","groups":["ca-nl/example.com"],"cluster_id":1,"_id":"c4pT2p7Q4aBVh13M"}
|
||||
{"lang":"en","xmltv_id":"CNNInternationalEurope2.us","site_id":"141","site":"example.com","date":"2022-01-31T00:00:00.000Z","groups":["ca-nl/example.com"],"cluster_id":1,"_id":"POYAcMssTAgZu4Yk"}
|
||||
{"lang":"ru","xmltv_id":"CNNInternationalEurope.us","site_id":"140","site":"example.com","date":"2022-01-30T00:00:00.000Z","groups":["ca-nl/example.com"],"cluster_id":1,"_id":"TYDwYLsrkmPtTLT2"}
|
||||
{"lang":"ru","xmltv_id":"CNNInternationalEurope.us","site_id":"140","site":"example.com","date":"2022-01-31T00:00:00.000Z","groups":["ca-nl/example.com"],"cluster_id":1,"_id":"98cKRthEhMmKEnwx"}
|
||||
|
|
|
@ -7,7 +7,7 @@ beforeEach(() => {
|
|||
fs.mkdirSync('tests/__data__/output')
|
||||
|
||||
const stdout = execSync(
|
||||
'DB_DIR=tests/__data__/output/database node scripts/commands/create-queue.js --channels=tests/__data__/input/sites/*.channels.xml --max-clusters=1',
|
||||
'DB_DIR=tests/__data__/output/database node scripts/commands/create-queue.js --channels=tests/__data__/input/sites/*.channels.xml --max-clusters=1 --days=2',
|
||||
{ encoding: 'utf8' }
|
||||
)
|
||||
})
|
||||
|
@ -18,17 +18,21 @@ it('can create queue', () => {
|
|||
|
||||
output = output.map(i => {
|
||||
i._id = null
|
||||
i.date = null
|
||||
return i
|
||||
})
|
||||
expected = expected.map(i => {
|
||||
i._id = null
|
||||
i.date = null
|
||||
return i
|
||||
})
|
||||
|
||||
expect(output).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining(expected[0]),
|
||||
expect.objectContaining(expected[1])
|
||||
expect.objectContaining(expected[1]),
|
||||
expect.objectContaining(expected[2]),
|
||||
expect.objectContaining(expected[3])
|
||||
])
|
||||
)
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue