Update database/create.js

This commit is contained in:
Aleksandr Statciuk 2022-02-13 03:04:25 +03:00
parent b39dc83f8b
commit e2e7c8b81c
3 changed files with 17 additions and 17 deletions

View File

@ -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 { program } = require('commander')
const _ = require('lodash') const _ = require('lodash')
@ -27,7 +27,9 @@ main()
async function findStreams() { async function findStreams() {
logger.info(`looking for streams...`) logger.info(`looking for streams...`)
await api.channels.load()
await db.streams.load() await db.streams.load()
const files = await file.list(`${options.inputDir}/**/*.m3u`) const files = await file.list(`${options.inputDir}/**/*.m3u`)
const streams = [] const streams = []
for (const filepath of files) { for (const filepath of files) {
@ -50,24 +52,17 @@ async function saveToDatabase(streams = []) {
for (const [i, chunk] of chunks.entries()) { for (const [i, chunk] of chunks.entries()) {
for (const item of chunk) { for (const item of chunk) {
const stream = store.create() const stream = store.create()
stream.set('channel_id', { channel_id: item.tvg.id }) const channel = await api.channels.find({ id: item.tvg.id })
stream.set('channel_name', { title: item.name }) 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('filepath', { filepath: item.filepath })
stream.set('resolution', { title: item.name })
stream.set('status', { title: item.name })
stream.set('url', { url: item.url }) stream.set('url', { url: item.url })
stream.set('http', { http: item.http }) stream.set('http_referrer', { http_referrer: item.http.referrer })
stream.set('is_broken', { status: stream.get('status') }) stream.set('user_agent', { user_agent: item.http['user-agent'] })
stream.set('updated', { updated: false })
stream.set('cluster_id', { cluster_id: i + 1 }) 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()) await db.streams.insert(stream.data())
} }
} }

View File

@ -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"}

View File

@ -13,7 +13,7 @@ beforeEach(() => {
it('can create database', () => { it('can create database', () => {
let output = content('tests/__data__/output/database/streams.db') 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 => { output = output.map(i => {
i._id = null i._id = null
@ -28,7 +28,8 @@ it('can create database', () => {
expect.arrayContaining([ expect.arrayContaining([
expect.objectContaining(expected[0]), expect.objectContaining(expected[0]),
expect.objectContaining(expected[1]), expect.objectContaining(expected[1]),
expect.objectContaining(expected[2]) expect.objectContaining(expected[2]),
expect.objectContaining(expected[3])
]) ])
) )
}) })