Update create-databasee.js

This commit is contained in:
Aleksandr Statciuk 2022-01-21 17:30:14 +03:00
parent 465d38abb9
commit af69100bc5
3 changed files with 24 additions and 11 deletions

View file

@ -1,6 +1,6 @@
const { db, file, parser, logger } = require('../core') const { db, file, parser, logger } = require('../core')
const { program } = require('commander') const { program } = require('commander')
const _ = require('lodash') const { shuffle } = require('lodash')
const options = program const options = program
.option( .option(
@ -27,7 +27,8 @@ main()
async function getChannels() { async function getChannels() {
logger.info(`Loading channels...`) logger.info(`Loading channels...`)
const channels = [] let channels = {}
const files = await file.list(options.channels) const files = await file.list(options.channels)
for (const filepath of files) { for (const filepath of files) {
const dir = file.dirname(filepath) const dir = file.dirname(filepath)
@ -37,17 +38,28 @@ async function getChannels() {
const configPath = `${dir}/${site}.config.js` const configPath = `${dir}/${site}.config.js`
const config = require(file.resolve(configPath)) const config = require(file.resolve(configPath))
if (config.ignore) continue if (config.ignore) continue
const [__, gid] = filename.match(/_([a-z-]+)\.channels\.xml/i) || [null, null] const [__, groupId] = filename.match(/_([a-z-]+)\.channels\.xml/i) || [null, null]
const items = await parser.parseChannels(filepath) const items = await parser.parseChannels(filepath)
for (const item of items) { for (const item of items) {
const key = `${item.site}:${item.site_id}`
if (!channels[key]) {
const countryCode = item.xmltv_id.split('.')[1] const countryCode = item.xmltv_id.split('.')[1]
item.country = countryCode ? countryCode.toUpperCase() : null item.country = countryCode ? countryCode.toUpperCase() : null
item.channelsPath = filepath item.channelsPath = filepath
item.configPath = configPath item.configPath = configPath
item.gid = gid item.groups = []
channels.push(item)
channels[key] = item
}
if (!channels[key].groups.includes(groupId)) {
channels[key].groups.push(groupId)
} }
} }
}
channels = Object.values(channels)
logger.info(`Found ${channels.length} channels`) logger.info(`Found ${channels.length} channels`)
return channels return channels
@ -57,7 +69,7 @@ async function saveToDatabase(channels = []) {
logger.info('Saving to the database...') logger.info('Saving to the database...')
await db.channels.load() await db.channels.load()
await db.channels.reset() await db.channels.reset()
const chunks = split(_.shuffle(channels), options.maxClusters) const chunks = split(shuffle(channels), options.maxClusters)
for (const [i, chunk] of chunks.entries()) { for (const [i, chunk] of chunks.entries()) {
for (const item of chunk) { for (const item of chunk) {
item.cluster_id = i + 1 item.cluster_id = i + 1

View file

@ -2,5 +2,6 @@
<site site="example.com"> <site site="example.com">
<channels> <channels>
<channel lang="ru" xmltv_id="CNNInternationalEurope.us" site_id="140">CNN International Europe</channel> <channel lang="ru" xmltv_id="CNNInternationalEurope.us" site_id="140">CNN International Europe</channel>
<channel lang="en" xmltv_id="CNNInternationalEurope2.us" site_id="140">CNN International Europe 2</channel>
</channels> </channels>
</site> </site>

View file

@ -25,7 +25,7 @@ it('can create channels database', () => {
site: 'example.com', site: 'example.com',
channelsPath: 'tests/__data__/input/sites/example.com_ca-nl.channels.xml', channelsPath: 'tests/__data__/input/sites/example.com_ca-nl.channels.xml',
configPath: 'tests/__data__/input/sites/example.com.config.js', configPath: 'tests/__data__/input/sites/example.com.config.js',
gid: 'ca-nl', groups: ['ca-nl'],
cluster_id: 1 cluster_id: 1
} }
]) ])