Update update.js

This commit is contained in:
Aleksandr Statciuk 2023-01-08 11:22:35 +03:00
parent 807f3cd15b
commit a4d6f33a0c

View file

@ -9,6 +9,10 @@ const CURR_DATE = process.env.CURR_DATE || new Date()
const logPath = `${LOGS_DIR}/guides/update.log` const logPath = `${LOGS_DIR}/guides/update.log`
let api_channels = []
let channels_dic = {}
let db_programs = []
async function main() { async function main() {
logger.info(`starting...`) logger.info(`starting...`)
@ -18,20 +22,14 @@ async function main() {
await api.regions.load() await api.regions.load()
await api.subdivisions.load() await api.subdivisions.load()
let countries = await api.countries.all() api_channels = await api.channels.all()
let api_channels = await api.channels.all()
let channels_dic = {}
api_channels.forEach(channel => { api_channels.forEach(channel => {
channels_dic[channel.id] = channel channels_dic[channel.id] = channel
}) })
let api_regions = await api.regions.all()
let api_subdivisions = await api.subdivisions.all()
logger.info('loading database/programs.db...') logger.info('loading database/programs.db...')
await db.programs.load() await db.programs.load()
let db_programs = await db.programs.find({}) db_programs = await db.programs.find({})
db_programs = db_programs db_programs = db_programs
.map(p => { .map(p => {
if (p.titles.length) { if (p.titles.length) {
@ -48,7 +46,46 @@ async function main() {
logger.info(`creating ${logPath}...`) logger.info(`creating ${logPath}...`)
await file.create(logPath) await file.create(logPath)
for (let country of countries) { await generateByCountry()
await generateBySource()
}
main()
async function generateBySource() {
let sites = _.groupBy(db_programs, 'site')
for (let site in sites) {
let langs = _.groupBy(sites[site], p => p.titles[0].lang)
for (let lang in langs) {
let programs = langs[lang]
let filename = `${site}/${lang}`
let { channels } = await save(filename, programs)
for (let channel of channels) {
let result = {
groupedBy: 'site+lang',
lang: channel.lang,
site: channel.site,
channel: channel.id,
filename
}
await file.append(logPath, JSON.stringify(result) + '\r\n')
}
}
}
}
async function generateByCountry() {
let api_countries = await api.countries.all()
let api_regions = await api.regions.all()
let api_subdivisions = await api.subdivisions.all()
for (let country of api_countries) {
let countryBroadcastCode = `c/${country.code}` let countryBroadcastCode = `c/${country.code}`
let countryRegions = api_regions let countryRegions = api_regions
.filter(r => r.countries.includes(country.code)) .filter(r => r.countries.includes(country.code))
@ -90,6 +127,26 @@ async function main() {
if (!programs.length) continue if (!programs.length) continue
const filename = country.code.toLowerCase()
let { channels } = await save(filename, programs)
for (let channel of channels) {
let result = {
groupedBy: 'broadcast_area',
country: country.code,
lang: channel.lang,
site: channel.site,
channel: channel.id,
filename
}
await file.append(logPath, JSON.stringify(result) + '\r\n')
}
}
}
async function save(filepath, programs) {
let channels = programs.map(p => { let channels = programs.map(p => {
let c = channels_dic[p.channel] let c = channels_dic[p.channel]
c.site = p.site c.site = p.site
@ -104,10 +161,9 @@ async function main() {
programs = programs.map(p => new Program(p, new Channel(channels_dic[p.channel]))) programs = programs.map(p => new Program(p, new Channel(channels_dic[p.channel])))
programs = _.uniqBy(programs, p => p.channel + p.start) programs = _.uniqBy(programs, p => p.channel + p.start)
const filename = country.code.toLowerCase() const xmlFilepath = `${PUBLIC_DIR}/guides/${filepath}.xml`
const xmlFilepath = `${PUBLIC_DIR}/guides/${filename}.xml` const gzFilepath = `${PUBLIC_DIR}/guides/${filepath}.xml.gz`
const gzFilepath = `${PUBLIC_DIR}/guides/${filename}.xml.gz` const jsonFilepath = `${PUBLIC_DIR}/guides/${filepath}.json`
const jsonFilepath = `${PUBLIC_DIR}/guides/${filename}.json`
logger.info(`creating ${xmlFilepath}...`) logger.info(`creating ${xmlFilepath}...`)
const xmltv = generateXMLTV({ const xmltv = generateXMLTV({
channels, channels,
@ -121,21 +177,11 @@ async function main() {
logger.info(`creating ${jsonFilepath}...`) logger.info(`creating ${jsonFilepath}...`)
await file.create(jsonFilepath, JSON.stringify({ channels, programs })) await file.create(jsonFilepath, JSON.stringify({ channels, programs }))
for (let channel of channels) { return {
let result = { channels,
country: country.code, programs
lang: channel.lang,
site: channel.site,
channel: channel.id,
filename
}
await file.append(logPath, JSON.stringify(result) + '\r\n')
} }
} }
}
main()
function convertLangCode(code, from, to) { function convertLangCode(code, from, to) {
let found = langs.where(from, code) let found = langs.where(from, code)