From ca3913f35714178809b7356c710989efe3b6d7d5 Mon Sep 17 00:00:00 2001 From: Aleksandr Statciuk Date: Sun, 30 Jan 2022 22:55:48 +0300 Subject: [PATCH] Update create-queue.js --- scripts/commands/create-queue.js | 34 +++++++++++++++-------- scripts/core/date.js | 13 +++++++++ scripts/core/index.js | 1 + tests/__data__/expected/database/queue.db | 6 ++-- tests/commands/create-queue.test.js | 8 ++++-- 5 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 scripts/core/date.js diff --git a/scripts/commands/create-queue.js b/scripts/commands/create-queue.js index 0dc67dd7..703d08c5 100644 --- a/scripts/commands/create-queue.js +++ b/scripts/commands/create-queue.js @@ -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 ', 'Number of days for which to grab the program', parser.parseNumber, 1) .option('--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 } diff --git a/scripts/core/date.js b/scripts/core/date.js new file mode 100644 index 00000000..6fb38597 --- /dev/null +++ b/scripts/core/date.js @@ -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 diff --git a/scripts/core/index.js b/scripts/core/index.js index 6003c058..19b46793 100644 --- a/scripts/core/index.js +++ b/scripts/core/index.js @@ -5,3 +5,4 @@ exports.parser = require('./parser') exports.timer = require('./timer') exports.markdown = require('./markdown') exports.api = require('./api') +exports.date = require('./date') diff --git a/tests/__data__/expected/database/queue.db b/tests/__data__/expected/database/queue.db index d81a2a4d..8bdde0d4 100644 --- a/tests/__data__/expected/database/queue.db +++ b/tests/__data__/expected/database/queue.db @@ -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"} diff --git a/tests/commands/create-queue.test.js b/tests/commands/create-queue.test.js index a52e5c87..f81970db 100644 --- a/tests/commands/create-queue.test.js +++ b/tests/commands/create-queue.test.js @@ -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]) ]) ) })