iptv/scripts/commands/database/create.js

89 lines
2.6 KiB
JavaScript
Raw Normal View History

2022-02-13 01:04:25 +01:00
const { db, file, parser, store, logger, id, api } = require('../../core')
2022-02-12 23:38:07 +01:00
const { program } = require('commander')
const _ = require('lodash')
const options = program
.option(
'--max-clusters <max-clusters>',
'Set maximum number of clusters',
parser.parseNumber,
256
)
2022-02-15 00:46:08 +01:00
.option('--input-dir <input-dir>', 'Set path to input directory', 'streams')
2022-02-12 23:38:07 +01:00
.parse(process.argv)
.opts()
async function main() {
logger.info('starting...')
logger.info(`number of clusters: ${options.maxClusters}`)
await saveToDatabase(await findStreams())
logger.info('done')
}
main()
async function findStreams() {
logger.info(`looking for streams...`)
2022-02-13 01:04:25 +01:00
await api.channels.load()
2022-08-15 01:22:46 +02:00
await api.streams.load()
2022-02-12 23:38:07 +01:00
await db.streams.load()
2022-02-13 01:04:25 +01:00
2022-02-12 23:38:07 +01:00
const streams = []
2022-02-14 00:48:11 +01:00
const files = await file.list(`${options.inputDir}/**/*.m3u`)
2022-02-12 23:38:07 +01:00
for (const filepath of files) {
2022-03-11 16:01:45 +01:00
const playlist = await parser.parsePlaylist(filepath)
for (const item of playlist.items) {
2022-02-12 23:38:07 +01:00
item.filepath = filepath
2022-02-14 00:48:11 +01:00
const stream = store.create()
const channel = await api.channels.find({ id: item.tvg.id })
2022-08-15 01:22:46 +02:00
const cached = (await api.streams.find({ url: item.url })) || {}
2022-02-14 00:48:11 +01:00
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'] })
2023-04-03 21:30:36 +02:00
stream.set('status', { status: cached.status || 'online' })
2023-04-20 02:15:15 +02:00
stream.set('width', { width: cached.width || 0 })
stream.set('height', { height: cached.height || 0 })
stream.set('bitrate', { bitrate: cached.bitrate || 0 })
stream.set('frame_rate', { frame_rate: cached.frame_rate || 0 })
2022-08-15 01:22:46 +02:00
stream.set('added_at', { added_at: cached.added_at })
stream.set('updated_at', { updated_at: cached.updated_at })
stream.set('checked_at', { checked_at: cached.checked_at })
2022-02-14 00:48:11 +01:00
streams.push(stream)
2022-02-12 23:38:07 +01:00
}
}
logger.info(`found ${streams.length} streams`)
return streams
}
async function saveToDatabase(streams = []) {
logger.info('saving to the database...')
await db.streams.reset()
const chunks = split(_.shuffle(streams), options.maxClusters)
for (const [i, chunk] of chunks.entries()) {
2022-02-14 00:48:11 +01:00
for (const stream of chunk) {
2022-02-12 23:38:07 +01:00
stream.set('cluster_id', { cluster_id: i + 1 })
await db.streams.insert(stream.data())
}
}
}
function split(arr, n) {
let result = []
for (let i = n; i > 0; i--) {
result.push(arr.splice(0, Math.ceil(arr.length / i)))
}
return result
}