This commit is contained in:
Aleksandr Statciuk 2022-02-07 00:19:09 +03:00
parent 9a4a62fd10
commit 109deb476d
4 changed files with 36 additions and 46 deletions

View File

@ -7,6 +7,7 @@ async function main() {
logger.info(`generating categories/...`) logger.info(`generating categories/...`)
await generator.generate('categories', streams) await generator.generate('categories', streams)
await generator.generate('countries', streams)
// await generateCountries(streams) // await generateCountries(streams)
// await generateLanguages() // await generateLanguages()

View File

@ -1,6 +1,9 @@
const { create: createPlaylist } = require('./playlist')
const logger = require('./logger')
const file = require('./file') const file = require('./file')
const generators = require('../generators') const generators = require('../generators')
const PUBLIC_DIR = process.env.PUBLIC_DIR || '.gh-pages'
const LOGS_DIR = process.env.LOGS_DIR || 'scripts/logs/generators' const LOGS_DIR = process.env.LOGS_DIR || 'scripts/logs/generators'
const generator = {} const generator = {}
@ -8,8 +11,12 @@ const generator = {}
generator.generate = async function (name, items = []) { generator.generate = async function (name, items = []) {
if (typeof generators[name] === 'function') { if (typeof generators[name] === 'function') {
try { try {
const logs = await generators[name].bind()(items) const output = await generators[name].bind()(items)
await file.create(`${LOGS_DIR}/${name}.log`, logs.map(toJSON).join('\n')) await file.create(`${LOGS_DIR}/${name}.log`, output.map(toJSON).join('\n'))
for (const type of output) {
const playlist = createPlaylist(type.items, { public: true })
await file.create(`${PUBLIC_DIR}/${name}/${type.id}.m3u`, playlist.toString())
}
} catch (error) { } catch (error) {
logger.error(`generators/${name}.js: ${error.message}`) logger.error(`generators/${name}.js: ${error.message}`)
} }
@ -18,6 +25,6 @@ generator.generate = async function (name, items = []) {
module.exports = generator module.exports = generator
function toJSON(item) { function toJSON(type) {
return JSON.stringify(item) return JSON.stringify({ id: type.id, count: type.items.length })
} }

View File

@ -1,47 +1,27 @@
const { create: createPlaylist } = require('../core/playlist')
const api = require('../core/api') const api = require('../core/api')
const file = require('../core/file')
const _ = require('lodash') const _ = require('lodash')
const PUBLIC_DIR = process.env.PUBLIC_DIR || '.gh-pages'
module.exports = async function (streams = []) { module.exports = async function (streams = []) {
const logs = [] streams = _.orderBy(
streams,
await api.categories.load()
const categories = await api.categories.all()
for (const category of categories) {
let output = _.filter(streams, { channel: { categories: [category.id] } })
output = _.orderBy(
output,
['channel.name', 'status.level', 'resolution.height'],
['asc', 'asc', 'desc']
)
output = _.uniqBy(output, s => s.channel_id || _.uniqueId())
const playlist = createPlaylist(output, { public: true })
await file.create(`${PUBLIC_DIR}/categories/${category.id}.m3u`, playlist.toString())
logs.push({ id: category.id, count: output.length })
}
let output = _.filter(streams, s => !s.categories.length)
output = _.orderBy(
output,
['channel.name', 'status.level', 'resolution.height'], ['channel.name', 'status.level', 'resolution.height'],
['asc', 'asc', 'desc'] ['asc', 'asc', 'desc']
) )
output = _.uniqBy(output, s => s.channel_id || _.uniqueId()) streams = _.uniqBy(streams, s => s.channel_id || _.uniqueId())
output = output.map(item => { const output = []
await api.categories.load()
const categories = await api.categories.all()
for (const category of categories) {
let items = _.filter(streams, { channel: { categories: [category.id] } })
output.push({ id: category.id, items })
}
let items = _.filter(streams, s => !s.categories.length)
items = items.map(item => {
item.group_title = 'Other' item.group_title = 'Other'
return item return item
}) })
output.push({ id: 'other', items })
const playlist = createPlaylist(output, { public: true }) return output
await file.create(`${PUBLIC_DIR}/categories/other.m3u`, playlist.toString())
logs.push({ id: 'other', count: output.length })
return logs
} }

View File

@ -18,7 +18,7 @@ beforeEach(() => {
console.log(stdout) console.log(stdout)
}) })
it.each([ fit.each([
'.gh-pages/categories/general.m3u', '.gh-pages/categories/general.m3u',
'.gh-pages/categories/legislative.m3u', '.gh-pages/categories/legislative.m3u',
'.gh-pages/categories/news.m3u', '.gh-pages/categories/news.m3u',
@ -28,12 +28,14 @@ it.each([
expect(content(`output/${filepath}`)).toBe(content(`expected/${filepath}`)) expect(content(`output/${filepath}`)).toBe(content(`expected/${filepath}`))
}) })
it.each(['countries/ru.m3u', 'countries/uk.m3u', 'countries/undefined.m3u'])( it.each([
'can generate %s', '.gh-pages/countries/ru.m3u',
filepath => { '.gh-pages/countries/uk.m3u',
expect(content(`output/.gh-pages/${filepath}`)).toBe(content(`expected/.gh-pages/${filepath}`)) '.gh-pages/countries/undefined.m3u',
} 'logs/generators/countries.log'
) ])('can generate %s', filepath => {
expect(content(`output/${filepath}`)).toBe(content(`expected/${filepath}`))
})
function content(filepath) { function content(filepath) {
return fs.readFileSync(`tests/__data__/${filepath}`, { return fs.readFileSync(`tests/__data__/${filepath}`, {