diff --git a/scripts/commands/database/create.js b/scripts/commands/database/create.js index 7611872ad..e4095db97 100644 --- a/scripts/commands/database/create.js +++ b/scripts/commands/database/create.js @@ -1,4 +1,4 @@ -const { db, file, parser, store, logger, id } = require('../../core') +const { db, file, parser, store, logger, id, api } = require('../../core') const { program } = require('commander') const _ = require('lodash') @@ -27,7 +27,9 @@ main() async function findStreams() { logger.info(`looking for streams...`) + await api.channels.load() await db.streams.load() + const files = await file.list(`${options.inputDir}/**/*.m3u`) const streams = [] for (const filepath of files) { @@ -50,24 +52,17 @@ async function saveToDatabase(streams = []) { for (const [i, chunk] of chunks.entries()) { for (const item of chunk) { const stream = store.create() - stream.set('channel_id', { channel_id: item.tvg.id }) - stream.set('channel_name', { title: item.name }) + const channel = await api.channels.find({ id: item.tvg.id }) + const channel_id = channel ? channel.id : null + + stream.set('channel', { channel: channel_id }) + stream.set('title', { title: item.name }) stream.set('filepath', { filepath: item.filepath }) - stream.set('resolution', { title: item.name }) - stream.set('status', { title: item.name }) stream.set('url', { url: item.url }) - stream.set('http', { http: item.http }) - stream.set('is_broken', { status: stream.get('status') }) - stream.set('updated', { updated: false }) + stream.set('http_referrer', { http_referrer: item.http.referrer }) + stream.set('user_agent', { user_agent: item.http['user-agent'] }) stream.set('cluster_id', { cluster_id: i + 1 }) - if (!stream.get('channel_id')) { - const channel_id = id.generate(item.name, item.filepath) - - stream.set('channel_id', { channel_id }) - stream.set('updated', { updated: true }) - } - await db.streams.insert(stream.data()) } } diff --git a/tests/__data__/expected/database/db_create.streams.db b/tests/__data__/expected/database/db_create.streams.db new file mode 100644 index 000000000..c4fc74f15 --- /dev/null +++ b/tests/__data__/expected/database/db_create.streams.db @@ -0,0 +1,4 @@ +{"channel":null,"title":"ABC (720p)","filepath":"tests/__data__/input/channels/wrong_id.m3u","url":"https://example.com/playlist2.m3u8","http_referrer":"","user_agent":"","cluster_id":1,"_id":"hrkAABqRIoVtOXd4"} +{"channel":null,"title":"Fox Sports 2 Asia (Thai) (720p)","filepath":"tests/__data__/input/channels/us_blocked.m3u","url":"https://example.com/playlist.m3u8","http_referrer":"","user_agent":"","cluster_id":1,"_id":"eeofJMxX7fOzDR9R"} +{"channel":null,"title":"1A Network (720p)","filepath":"tests/__data__/input/channels/unsorted.m3u","url":"https://simultv.s.llnwi.net/n4s4/2ANetwork/interlink.m3u8","http_referrer":"","user_agent":"","cluster_id":1,"_id":"omQ1KYCOKqb5Lxyn"} +{"channel":"ATV.ad","title":"ATV (720p) [Offline]","filepath":"tests/__data__/input/channels/ad.m3u","url":"https://iptv-all.lanesh4d0w.repl.co/andorra/atv","http_referrer":"","user_agent":"","cluster_id":1,"_id":"v9O0SiMdhSh7wiKB"} diff --git a/tests/commands/database/create.test.js b/tests/commands/database/create.test.js index 614272ab4..d3f7e07da 100644 --- a/tests/commands/database/create.test.js +++ b/tests/commands/database/create.test.js @@ -13,7 +13,7 @@ beforeEach(() => { it('can create database', () => { let output = content('tests/__data__/output/database/streams.db') - let expected = content('tests/__data__/expected/database/streams.db') + let expected = content('tests/__data__/expected/database/db_create.streams.db') output = output.map(i => { i._id = null @@ -28,7 +28,8 @@ it('can create database', () => { expect.arrayContaining([ expect.objectContaining(expected[0]), expect.objectContaining(expected[1]), - expect.objectContaining(expected[2]) + expect.objectContaining(expected[2]), + expect.objectContaining(expected[3]) ]) ) })