mirror of
https://github.com/iptv-org/epg.git
synced 2025-05-10 00:50:09 -04:00
Created commands/create-database.js
This commit is contained in:
parent
5c95098fea
commit
f5dbc9376e
12 changed files with 29999 additions and 1 deletions
63
scripts/commands/create-database.js
Normal file
63
scripts/commands/create-database.js
Normal file
|
@ -0,0 +1,63 @@
|
|||
const { db, file, parser, logger } = require('../core')
|
||||
const { program } = require('commander')
|
||||
const _ = require('lodash')
|
||||
|
||||
const options = program
|
||||
.option(
|
||||
'--max-clusters <max-clusters>',
|
||||
'Set maximum number of clusters',
|
||||
parser.parseNumber,
|
||||
200
|
||||
)
|
||||
.option('--channels <channels>', 'Set path to channels.xml file', 'sites/**/*.channels.xml')
|
||||
.parse(process.argv)
|
||||
.opts()
|
||||
|
||||
const channels = []
|
||||
|
||||
async function main() {
|
||||
logger.info('Starting...')
|
||||
logger.info(`Number of clusters: ${options.maxClusters}`)
|
||||
|
||||
await loadChannels()
|
||||
await saveToDatabase()
|
||||
|
||||
logger.info('Done')
|
||||
}
|
||||
|
||||
main()
|
||||
|
||||
async function loadChannels() {
|
||||
logger.info(`Loading channels...`)
|
||||
|
||||
const files = await file.list(options.channels)
|
||||
for (const filepath of files) {
|
||||
const items = await parser.parseChannels(filepath)
|
||||
for (const item of items) {
|
||||
item.filepath = filepath
|
||||
channels.push(item)
|
||||
}
|
||||
}
|
||||
logger.info(`Found ${channels.length} channels`)
|
||||
}
|
||||
|
||||
async function saveToDatabase() {
|
||||
logger.info('Saving to the database...')
|
||||
|
||||
await db.reset()
|
||||
const chunks = split(_.shuffle(channels), options.maxClusters)
|
||||
for (const [i, chunk] of chunks.entries()) {
|
||||
for (const item of chunk) {
|
||||
item.cluster_id = i + 1
|
||||
await db.insert(item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue