From 091d43c4f7cac63a2027ce55830d6c7c15e46cdb Mon Sep 17 00:00:00 2001 From: Aleksandr Statciuk Date: Mon, 7 Feb 2022 06:04:41 +0300 Subject: [PATCH] Update create-database.js --- scripts/commands/create-database.js | 35 ++++++++++--------- .../setters/{title.js => channel_name.js} | 0 scripts/store/setters/index.js | 2 +- tests/commands/create-database.test.js | 6 ++-- 4 files changed, 22 insertions(+), 21 deletions(-) rename scripts/store/setters/{title.js => channel_name.js} (100%) diff --git a/scripts/commands/create-database.js b/scripts/commands/create-database.js index f9927cbb88..8a41dbbfa0 100644 --- a/scripts/commands/create-database.js +++ b/scripts/commands/create-database.js @@ -13,44 +13,45 @@ const options = program .parse(process.argv) .opts() -const links = [] - async function main() { logger.info('starting...') logger.info(`number of clusters: ${options.maxClusters}`) - await loadChannels() - await saveToDatabase() + await saveToDatabase(await findStreams()) logger.info('done') } main() -async function loadChannels() { - logger.info(`loading links...`) +async function findStreams() { + logger.info(`looking for streams...`) + await db.streams.load() const files = await file.list(`${options.inputDir}/**/*.m3u`) + const streams = [] for (const filepath of files) { const items = await parser.parsePlaylist(filepath) for (const item of items) { item.filepath = filepath - links.push(item) + streams.push(item) } } - logger.info(`found ${links.length} links`) + logger.info(`found ${streams.length} streams`) + + return streams } -async function saveToDatabase() { +async function saveToDatabase(streams = []) { logger.info('saving to the database...') - await db.reset() - const chunks = split(_.shuffle(links), options.maxClusters) + await db.streams.reset() + const chunks = split(_.shuffle(streams), options.maxClusters) for (const [i, chunk] of chunks.entries()) { for (const item of chunk) { const stream = store.create() - stream.set('id', { id: item.tvg.id }) - stream.set('title', { title: item.name }) + stream.set('channel_id', { channel_id: item.tvg.id }) + stream.set('channel_name', { title: item.name }) stream.set('filepath', { filepath: item.filepath }) stream.set('resolution', { title: item.name }) stream.set('status', { title: item.name }) @@ -60,14 +61,14 @@ async function saveToDatabase() { stream.set('updated', { updated: false }) stream.set('cluster_id', { cluster_id: i + 1 }) - if (!stream.get('id')) { - const id = cid.generate(item.name, item.filepath) + if (!stream.get('channel_id')) { + const channel_id = cid.generate(item.name, item.filepath) - stream.set('id', { id }) + stream.set('channel_id', { channel_id }) stream.set('updated', { updated: true }) } - await db.insert(stream.data()) + await db.streams.insert(stream.data()) } } } diff --git a/scripts/store/setters/title.js b/scripts/store/setters/channel_name.js similarity index 100% rename from scripts/store/setters/title.js rename to scripts/store/setters/channel_name.js diff --git a/scripts/store/setters/index.js b/scripts/store/setters/index.js index 273dc07105..264b8e97a8 100644 --- a/scripts/store/setters/index.js +++ b/scripts/store/setters/index.js @@ -2,4 +2,4 @@ exports.is_broken = require('./is_broken') exports.resolution = require('./resolution') exports.status = require('./status') exports.url = require('./url') -exports.title = require('./title') +exports.channel_name = require('./channel_name') diff --git a/tests/commands/create-database.test.js b/tests/commands/create-database.test.js index 674c6d81f2..c5c1dca2c2 100644 --- a/tests/commands/create-database.test.js +++ b/tests/commands/create-database.test.js @@ -6,14 +6,14 @@ beforeEach(() => { fs.emptyDirSync('tests/__data__/output') const stdout = execSync( - 'DB_FILEPATH=tests/__data__/output/streams.db node scripts/commands/create-database.js --input-dir=tests/__data__/input/channels --max-clusters=1', + 'DB_DIR=tests/__data__/output/database node scripts/commands/create-database.js --input-dir=tests/__data__/input/channels --max-clusters=1', { encoding: 'utf8' } ) }) it('can create database', () => { - let output = content('tests/__data__/output/streams.db') - let expected = content('tests/__data__/expected/streams.db') + let output = content('tests/__data__/output/database/streams.db') + let expected = content('tests/__data__/expected/database/streams.db') output = output.map(i => { i._id = null