Update database/create.js

This commit is contained in:
Aleksandr Statciuk 2022-02-14 02:48:11 +03:00
parent 37b77a7d62
commit 682824a45d
3 changed files with 18 additions and 13 deletions

View File

@ -30,13 +30,24 @@ async function findStreams() {
await api.channels.load() await api.channels.load()
await db.streams.load() await db.streams.load()
const files = await file.list(`${options.inputDir}/**/*.m3u`)
const streams = [] const streams = []
const files = await file.list(`${options.inputDir}/**/*.m3u`)
for (const filepath of files) { for (const filepath of files) {
const items = await parser.parsePlaylist(filepath) const items = await parser.parsePlaylist(filepath)
for (const item of items) { for (const item of items) {
item.filepath = filepath item.filepath = filepath
streams.push(item)
const stream = store.create()
const channel = await api.channels.find({ id: item.tvg.id })
stream.set('channel', { channel: channel ? channel.id : null })
stream.set('title', { title: item.name })
stream.set('filepath', { filepath: item.filepath })
stream.set('url', { url: item.url })
stream.set('http_referrer', { http_referrer: item.http.referrer })
stream.set('user_agent', { user_agent: item.http['user-agent'] })
streams.push(stream)
} }
} }
logger.info(`found ${streams.length} streams`) logger.info(`found ${streams.length} streams`)
@ -50,17 +61,7 @@ async function saveToDatabase(streams = []) {
await db.streams.reset() await db.streams.reset()
const chunks = split(_.shuffle(streams), options.maxClusters) const chunks = split(_.shuffle(streams), options.maxClusters)
for (const [i, chunk] of chunks.entries()) { for (const [i, chunk] of chunks.entries()) {
for (const item of chunk) { for (const stream of chunk) {
const stream = store.create()
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('url', { url: item.url })
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 }) stream.set('cluster_id', { cluster_id: i + 1 })
await db.streams.insert(stream.data()) await db.streams.insert(stream.data())

View File

@ -0,0 +1,3 @@
module.exports = function ({ channel }) {
return channel || null
}

View File

@ -1,3 +1,4 @@
exports.url = require('./url') exports.url = require('./url')
exports.http_referrer = require('./http_referrer') exports.http_referrer = require('./http_referrer')
exports.user_agent = require('./user_agent') exports.user_agent = require('./user_agent')
exports.channel = require('./channel')