diff --git a/scripts/commands/create-database.js b/scripts/commands/create-queue.js similarity index 65% rename from scripts/commands/create-database.js rename to scripts/commands/create-queue.js index edffef29..0dc67dd7 100644 --- a/scripts/commands/create-database.js +++ b/scripts/commands/create-queue.js @@ -17,17 +17,17 @@ async function main() { logger.info('Starting...') logger.info(`Number of clusters: ${options.maxClusters}`) - await saveToDatabase(await getChannels()) + await saveToDatabase(await createQueue()) logger.info('Done') } main() -async function getChannels() { - logger.info(`Loading channels...`) +async function createQueue() { + logger.info(`Create queue...`) - let channels = {} + let queue = {} const files = await file.list(options.channels) for (const filepath of files) { @@ -41,40 +41,37 @@ async function getChannels() { const [__, region] = filename.match(/_([a-z-]+)\.channels\.xml/i) || [null, null] const groupId = `${region}/${site}` for (const item of items) { - if (!item.site || !item.site_id || !item.xmltv_id || !item.name) continue + if (!item.site || !item.site_id || !item.xmltv_id) continue const key = `${item.site}:${item.site_id}` - if (!channels[key]) { - const countryCode = item.xmltv_id.split('.')[1] - item.country = countryCode ? countryCode.toUpperCase() : null - item.channelsPath = filepath + if (!queue[key]) { item.configPath = configPath item.groups = [] - channels[key] = item + queue[key] = item } - if (!channels[key].groups.includes(groupId)) { - channels[key].groups.push(groupId) + if (!queue[key].groups.includes(groupId)) { + queue[key].groups.push(groupId) } } } - channels = Object.values(channels) + queue = Object.values(queue) - logger.info(`Found ${channels.length} channels`) + logger.info(`Found ${queue.length} items`) - return channels + return queue } -async function saveToDatabase(channels = []) { +async function saveToDatabase(items = []) { logger.info('Saving to the database...') - await db.channels.load() - await db.channels.reset() - const chunks = split(shuffle(channels), options.maxClusters) + await db.queue.load() + await db.queue.reset() + const chunks = split(shuffle(items), options.maxClusters) for (const [i, chunk] of chunks.entries()) { for (const item of chunk) { item.cluster_id = i + 1 - await db.channels.insert(item) + await db.queue.insert(item) } } } diff --git a/tests/commands/create-database.test.js b/tests/commands/create-queue.test.js similarity index 70% rename from tests/commands/create-database.test.js rename to tests/commands/create-queue.test.js index 7a0a6da8..b3e1c22b 100644 --- a/tests/commands/create-database.test.js +++ b/tests/commands/create-queue.test.js @@ -7,24 +7,21 @@ beforeEach(() => { fs.mkdirSync('tests/__data__/output') const stdout = execSync( - 'DB_DIR=tests/__data__/output/database node scripts/commands/create-database.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', { encoding: 'utf8' } ) }) -it('can create channels database', () => { - const output = content('tests/__data__/output/database/channels.db') +it('can create queue', () => { + const output = content('tests/__data__/output/database/queue.db') expect(output).toEqual( expect.arrayContaining([ expect.objectContaining({ lang: 'ru', - country: 'US', xmltv_id: 'CNNInternationalEurope.us', site_id: '140', - name: 'CNN International Europe', site: 'example.com', - channelsPath: 'tests/__data__/input/sites/example.com_ca-nl.channels.xml', configPath: 'tests/__data__/input/sites/example.com.config.js', groups: ['ca-nl/example.com'], cluster_id: 1 @@ -33,10 +30,7 @@ it('can create channels database', () => { lang: 'en', xmltv_id: 'CNNInternationalEurope2.us', site_id: '141', - name: 'CNN International Europe 2', site: 'example.com', - country: 'US', - channelsPath: 'tests/__data__/input/sites/example.com_ca-nl.channels.xml', configPath: 'tests/__data__/input/sites/example.com.config.js', groups: ['ca-nl/example.com'], cluster_id: 1