mirror of
https://github.com/iptv-org/epg.git
synced 2025-05-10 00:50:09 -04:00
Update create-databasee.js
This commit is contained in:
parent
465d38abb9
commit
af69100bc5
3 changed files with 24 additions and 11 deletions
|
@ -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 countryCode = item.xmltv_id.split('.')[1]
|
const key = `${item.site}:${item.site_id}`
|
||||||
item.country = countryCode ? countryCode.toUpperCase() : null
|
if (!channels[key]) {
|
||||||
item.channelsPath = filepath
|
const countryCode = item.xmltv_id.split('.')[1]
|
||||||
item.configPath = configPath
|
item.country = countryCode ? countryCode.toUpperCase() : null
|
||||||
item.gid = gid
|
item.channelsPath = filepath
|
||||||
channels.push(item)
|
item.configPath = configPath
|
||||||
|
item.groups = []
|
||||||
|
|
||||||
|
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
|
||||||
|
|
|
@ -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>
|
|
@ -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
|
||||||
}
|
}
|
||||||
])
|
])
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue