iptv/scripts/commands/playlist/generate.js

89 lines
3.0 KiB
JavaScript
Raw Normal View History

2022-02-11 19:07:16 +01:00
const { db, generator, api, logger, file } = require('../../core')
2022-02-12 01:11:55 +01:00
const { orderBy } = require('natural-orderby')
2021-12-12 05:10:03 +01:00
const _ = require('lodash')
async function main() {
2022-02-06 22:04:56 +01:00
const streams = await loadStreams()
2022-02-07 03:53:30 +01:00
2022-02-07 23:11:47 +01:00
logger.info('generating categories/...')
2022-02-06 22:04:56 +01:00
await generator.generate('categories', streams)
2022-02-07 23:11:47 +01:00
logger.info('generating countries/...')
2022-02-06 22:19:09 +01:00
await generator.generate('countries', streams)
2022-02-07 23:11:47 +01:00
logger.info('generating languages/...')
2022-02-06 23:22:20 +01:00
await generator.generate('languages', streams)
2022-02-07 23:11:47 +01:00
logger.info('generating regions/...')
2022-02-06 23:22:20 +01:00
await generator.generate('regions', streams)
2022-02-07 23:11:47 +01:00
logger.info('generating index.category.m3u...')
2022-02-07 01:52:47 +01:00
await generator.generate('index_category_m3u', streams)
2022-02-07 23:11:47 +01:00
logger.info('generating index.country.m3u...')
2022-02-07 02:42:15 +01:00
await generator.generate('index_country_m3u', streams)
2022-02-07 23:11:47 +01:00
logger.info('generating index.language.m3u...')
2022-02-07 03:53:30 +01:00
await generator.generate('index_language_m3u', streams)
2022-02-07 23:11:47 +01:00
logger.info('generating index.m3u...')
2022-02-07 03:53:30 +01:00
await generator.generate('index_m3u', streams)
2022-02-07 23:11:47 +01:00
logger.info('generating index.nsfw.m3u...')
2022-02-07 03:53:30 +01:00
await generator.generate('index_nsfw_m3u', streams)
2022-02-07 23:11:47 +01:00
logger.info('generating index.region.m3u...')
2022-02-07 03:53:30 +01:00
await generator.generate('index_region_m3u', streams)
2021-12-12 05:10:03 +01:00
}
2022-02-06 22:04:56 +01:00
main()
2021-12-12 05:10:03 +01:00
2022-02-06 22:04:56 +01:00
async function loadStreams() {
2022-02-07 23:11:47 +01:00
await db.streams.load()
2022-02-14 00:21:54 +01:00
let streams = await db.streams.find({})
streams = _.filter(streams, stream => stream.status !== 'error')
2022-03-14 21:32:46 +01:00
const levels = { online: 1, blocked: 2, timeout: 3, error: 4, default: 5 }
streams = orderBy(
streams,
['channel', s => levels[s.status] || levels['default'], 'height', 'url'],
['asc', 'asc', 'desc', 'asc']
)
2022-02-13 05:03:39 +01:00
streams = _.uniqBy(streams, stream => stream.channel || _.uniqueId())
2022-02-07 23:11:47 +01:00
2022-02-06 22:04:56 +01:00
await api.channels.load()
let channels = await api.channels.all()
channels = _.keyBy(channels, 'id')
2021-12-12 05:10:03 +01:00
2022-02-06 22:04:56 +01:00
await api.categories.load()
let categories = await api.categories.all()
categories = _.keyBy(categories, 'id')
2021-12-12 05:10:03 +01:00
2022-02-06 22:04:56 +01:00
await api.languages.load()
let languages = await api.languages.all()
languages = _.keyBy(languages, 'code')
2021-12-12 05:10:03 +01:00
2022-02-06 22:04:56 +01:00
await api.guides.load()
let guides = await api.guides.all()
guides = _.groupBy(guides, 'channel')
2022-02-13 05:03:39 +01:00
streams = streams.map(stream => {
const channel = channels[stream.channel] || null
2022-02-11 17:56:11 +01:00
const filename = file.getFilename(stream.filepath)
const [_, code] = filename.match(/^([a-z]{2})(_|$)/) || [null, null]
const defaultBroadcastArea = code ? [`c/${code.toUpperCase()}`] : []
2022-02-06 22:04:56 +01:00
if (channel) {
stream.guides = Array.isArray(guides[channel.id]) ? guides[channel.id] : []
stream.categories = channel.categories.map(id => categories[id]).filter(i => i)
stream.languages = channel.languages.map(id => languages[id]).filter(i => i)
stream.broadcast_area = channel.broadcast_area
stream.is_nsfw = channel.is_nsfw
stream.logo = channel.logo
} else {
stream.guides = []
stream.categories = []
stream.languages = []
stream.broadcast_area = defaultBroadcastArea
stream.is_nsfw = false
stream.logo = null
}
2022-02-06 22:04:56 +01:00
return stream
})
2022-02-13 05:03:39 +01:00
streams = orderBy(streams, ['title'], ['asc'])
return streams
2021-12-12 05:10:03 +01:00
}