iptv/scripts/commands/database/create.js

41 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-04-30 04:48:11 +02:00
const { db, file, parser, store, logger } = require('../../core')
2022-02-12 23:38:07 +01:00
const { program } = require('commander')
const _ = require('lodash')
const options = program
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() {
2023-04-27 16:41:54 +02:00
logger.info(`looking for streams...`)
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()
2023-04-30 04:48:11 +02:00
stream.set('channel', { channel: item.tvg.id })
2022-02-14 00:48:11 +01:00
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)
2022-02-12 23:38:07 +01:00
}
}
logger.info(`found ${streams.length} streams`)
logger.info('saving to the database...')
2023-04-30 04:48:11 +02:00
await db.streams.load()
2022-02-12 23:38:07 +01:00
await db.streams.reset()
2023-04-27 16:52:01 +02:00
const data = streams.map(stream => stream.data())
await db.streams.insert(data)
2022-02-12 23:38:07 +01:00
}
2023-04-30 04:48:11 +02:00
main()