Update generate-playlist.js

This commit is contained in:
Aleksandr Statciuk
2022-02-11 19:56:11 +03:00
parent d3774679bd
commit e3e4441909
65 changed files with 246 additions and 156 deletions

View File

@@ -1,4 +1,4 @@
const { db, generator, api, logger } = require('../core')
const { db, generator, api, logger, file } = require('../core')
const _ = require('lodash')
async function main() {
@@ -30,7 +30,7 @@ main()
async function loadStreams() {
await db.streams.load()
let streams = await db.streams.find({})
let streams = await db.streams.find({ is_broken: false })
await api.channels.load()
let channels = await api.channels.all()
@@ -50,31 +50,16 @@ async function loadStreams() {
return streams.map(stream => {
const channel = channels[stream.channel_id] || null
const filename = file.getFilename(stream.filepath)
const [_, code] = filename.match(/^([a-z]{2})(_|$)/) || [null, null]
const defaultBroadcastArea = code ? [`c/${code.toUpperCase()}`] : []
if (channel) {
stream.group_title = channel.categories
.map(id => (categories[id] ? categories[id].name : null))
.filter(i => i)
.sort()
.join(';')
stream.tvg_language = channel.languages
.map(code => (languages[code] ? languages[code].name : ''))
.filter(i => i)
.sort()
.join(';')
stream.tvg_country = channel.broadcast_area
.map(item => {
const [_, code] = item.split('/')
return code
})
.filter(i => i)
.sort()
.join(';')
stream.tvg_logo = channel.logo
stream.tvg_url =
guides[channel.id] && guides[channel.id].length ? guides[channel.id][0].url : null
stream.channel = channel
}
stream.guides = channel && Array.isArray(guides[channel.id]) ? guides[channel.id] : []
stream.categories = channel ? channel.categories.map(id => categories[id]) : []
stream.languages = channel ? channel.languages.map(id => languages[id]) : []
stream.broadcast_area = channel ? channel.broadcast_area : defaultBroadcastArea
stream.is_nsfw = channel ? channel.is_nsfw : false
stream.logo = channel ? channel.logo : null
return stream
})

View File

@@ -53,7 +53,7 @@ playlist.create = function (items = [], options = {}) {
const header = {}
if (options.public) {
let guides = items.map(item => item.tvg_url).filter(i => i)
let guides = items.map(item => (item.guides.length ? item.guides[0].url : null)).filter(i => i)
header['x-tvg-url'] = _.uniq(guides).sort().join(',')
}
p.setHeader(header)

View File

@@ -2,15 +2,16 @@ const api = require('../core/api')
const _ = require('lodash')
module.exports = async function (streams = []) {
const output = []
await api.categories.load()
const categories = await api.categories.all()
const output = []
for (const category of categories) {
let items = _.filter(streams, { channel: { categories: [category.id] } })
let items = _.filter(streams, { categories: [{ id: category.id }] })
output.push({ filepath: `categories/${category.id}.m3u`, items })
}
let items = _.filter(streams, stream => !stream.channel || !stream.channel.categories.length)
let items = _.filter(streams, stream => !stream.categories.length)
output.push({ filepath: 'categories/undefined.m3u', items })
return output

View File

@@ -2,7 +2,7 @@ const api = require('../core/api')
const _ = require('lodash')
module.exports = async function (streams = []) {
streams = _.filter(streams, stream => !stream.channel || stream.channel.is_nsfw === false)
streams = _.filter(streams, stream => stream.is_nsfw === false)
await api.countries.load()
const countries = await api.countries.all()
@@ -15,15 +15,15 @@ module.exports = async function (streams = []) {
r => `r/${r.code}`
)
countryAreaCodes.push(`c/${country.code}`)
let items = _.filter(
streams,
stream =>
stream.channel && _.intersection(stream.channel.broadcast_area, countryAreaCodes).length
)
let items = _.filter(streams, stream => {
return _.intersection(stream.broadcast_area, countryAreaCodes).length
})
output.push({ filepath: `countries/${country.code.toLowerCase()}.m3u`, items })
}
let items = _.filter(streams, stream => !stream.channel || !stream.channel.broadcast_area.length)
let items = _.filter(streams, stream => !stream.broadcast_area.length)
output.push({ filepath: 'countries/undefined.m3u', items })
return output

View File

@@ -1,32 +1,27 @@
const api = require('../core/api')
const _ = require('lodash')
module.exports = async function (streams = []) {
streams = _.filter(streams, s => !s.channel || s.channel.is_nsfw === false)
await api.categories.load()
let categories = await api.categories.all()
categories = _.keyBy(categories, 'id')
streams = _.filter(streams, stream => stream.is_nsfw === false)
let items = []
streams.forEach(stream => {
if (!stream.channel || !stream.channel.categories.length) {
if (!stream.categories.length) {
const item = _.cloneDeep(stream)
item.group_title = null
item.group_title = 'Undefined'
items.push(item)
return
}
stream.channel.categories.forEach(id => {
stream.categories.forEach(category => {
const item = _.cloneDeep(stream)
item.group_title = categories[id] ? categories[id].name : null
item.group_title = category.name
items.push(item)
})
})
items = _.sortBy(items, item => {
if (!item.group_title) return ''
if (item.group_title === 'Undefined') return ''
return item.group_title
})

View File

@@ -2,7 +2,7 @@ const api = require('../core/api')
const _ = require('lodash')
module.exports = async function (streams = []) {
streams = _.filter(streams, s => !s.channel || s.channel.is_nsfw === false)
streams = _.filter(streams, stream => stream.is_nsfw === false)
await api.regions.load()
let regions = await api.regions.all()
@@ -14,14 +14,14 @@ module.exports = async function (streams = []) {
let items = []
streams.forEach(stream => {
if (!stream.channel || !stream.channel.broadcast_area.length) {
if (!stream.broadcast_area.length) {
const item = _.cloneDeep(stream)
item.group_title = null
item.group_title = 'Undefined'
items.push(item)
return
}
getBroadcastCountries(stream.channel, { countries, regions }).forEach(country => {
getBroadcastCountries(stream, { countries, regions }).forEach(country => {
const item = _.cloneDeep(stream)
item.group_title = country.name
items.push(item)
@@ -29,15 +29,16 @@ module.exports = async function (streams = []) {
})
items = _.sortBy(items, item => {
if (!item.group_title) return false
if (item.group_title === 'Undefined') return ''
return item.group_title
})
return { filepath: 'index.country.m3u', items }
}
function getBroadcastCountries(channel, { countries, regions }) {
let codes = channel.broadcast_area.reduce((acc, item) => {
function getBroadcastCountries(stream, { countries, regions }) {
let codes = stream.broadcast_area.reduce((acc, item) => {
const [type, code] = item.split('/')
switch (type) {
case 'c':

View File

@@ -1,30 +1,27 @@
const api = require('../core/api')
const _ = require('lodash')
module.exports = async function (streams = []) {
streams = _.filter(streams, s => !s.channel || s.channel.is_nsfw === false)
await api.languages.load()
let languages = await api.languages.all()
languages = _.keyBy(languages, 'code')
streams = _.filter(streams, stream => stream.is_nsfw === false)
let items = []
streams.forEach(stream => {
if (!stream.channel || !stream.channel.languages.length) {
if (!stream.languages.length) {
const item = _.cloneDeep(stream)
item.group_title = null
item.group_title = 'Undefined'
items.push(stream)
return
}
stream.channel.languages.forEach(code => {
stream.languages.forEach(language => {
const item = _.cloneDeep(stream)
item.group_title = languages[code] ? languages[code].name : null
item.group_title = language.name
items.push(item)
})
})
items = _.sortBy(items, i => {
if (!i.group_title) return ''
if (i.group_title === 'Undefined') return ''
return i.group_title
})

View File

@@ -2,6 +2,6 @@ const api = require('../core/api')
const _ = require('lodash')
module.exports = async function (streams = []) {
streams = _.filter(streams, s => !s.channel || s.channel.is_nsfw === false)
streams = _.filter(streams, stream => stream.is_nsfw === false)
return { filepath: 'index.m3u', items: streams }
}

View File

@@ -2,7 +2,7 @@ const api = require('../core/api')
const _ = require('lodash')
module.exports = async function (streams = []) {
streams = _.filter(streams, stream => !stream.channel || stream.channel.is_nsfw === false)
streams = _.filter(streams, stream => stream.is_nsfw === false)
await api.regions.load()
let regions = await api.regions.all()
@@ -10,14 +10,14 @@ module.exports = async function (streams = []) {
let items = []
streams.forEach(stream => {
if (!stream.channel || !stream.channel.broadcast_area.length) {
if (!stream.broadcast_area.length) {
const item = _.cloneDeep(stream)
item.group_title = null
item.group_title = 'Undefined'
items.push(item)
return
}
getChannelRegions(stream.channel, { regions }).forEach(region => {
getChannelRegions(stream, { regions }).forEach(region => {
const item = _.cloneDeep(stream)
item.group_title = region.name
items.push(item)
@@ -25,15 +25,16 @@ module.exports = async function (streams = []) {
})
items = _.sortBy(items, i => {
if (!i.group_title) return ''
if (i.group_title === 'Undefined') return ''
return i.group_title
})
return { filepath: 'index.region.m3u', items }
}
function getChannelRegions(channel, { regions }) {
return channel.broadcast_area
function getChannelRegions(stream, { regions }) {
return stream.broadcast_area
.reduce((acc, item) => {
const [type, code] = item.split('/')
switch (type) {

View File

@@ -2,7 +2,7 @@ const api = require('../core/api')
const _ = require('lodash')
module.exports = async function (streams = []) {
streams = _.filter(streams, s => !s.channel || s.channel.is_nsfw === false)
streams = _.filter(streams, stream => stream.is_nsfw === false)
await api.languages.load()
let languages = await api.languages.all()
@@ -10,13 +10,13 @@ module.exports = async function (streams = []) {
const output = []
for (const language of languages) {
let items = _.filter(streams, { channel: { languages: [language.code] } })
let items = _.filter(streams, { languages: [{ code: language.code }] })
if (items.length) {
output.push({ filepath: `languages/${language.code}.m3u`, items })
}
}
let items = _.filter(streams, stream => !stream.channel || !stream.channel.languages.length)
let items = _.filter(streams, stream => !stream.languages.length)
output.push({ filepath: 'languages/undefined.m3u', items })
return output

View File

@@ -2,21 +2,21 @@ const api = require('../core/api')
const _ = require('lodash')
module.exports = async function (streams = []) {
const output = []
streams = _.filter(streams, stream => stream.is_nsfw === false)
await api.regions.load()
const regions = await api.regions.all()
streams = _.filter(streams, stream => !stream.channel || stream.channel.is_nsfw === false)
const output = []
for (const region of regions) {
const areaCodes = region.countries.map(code => `c/${code}`)
areaCodes.push(`r/${region.code}`)
let items = _.filter(
streams,
stream => stream.channel && _.intersection(stream.channel.broadcast_area, areaCodes).length
)
let items = _.filter(streams, stream => _.intersection(stream.broadcast_area, areaCodes).length)
output.push({ filepath: `regions/${region.code.toLowerCase()}.m3u`, items })
}
let items = _.filter(streams, stream => !stream.channel || !stream.channel.broadcast_area.length)
let items = _.filter(streams, stream => !stream.broadcast_area.length)
output.push({ filepath: 'regions/undefined.m3u', items })
return output

View File

@@ -1,3 +1,12 @@
module.exports = function () {
return this.group_title || 'Undefined'
if (this.group_title) return this.group_title
if (this.categories.length) {
return this.categories
.map(category => category.name)
.sort()
.join(';')
}
return 'Undefined'
}

View File

@@ -2,6 +2,5 @@ exports.group_title = require('./group_title')
exports.title = require('./title')
exports.tvg_id = require('./tvg_id')
exports.tvg_logo = require('./tvg_logo')
exports.tvg_url = require('./tvg_url')
exports.tvg_country = require('./tvg_country')
exports.tvg_language = require('./tvg_language')

View File

@@ -1,3 +1,16 @@
module.exports = function () {
return this.tvg_country || ''
if (this.tvg_country) return this.tvg_country
if (this.broadcast_area.length) {
return this.broadcast_area
.map(item => {
const [_, code] = item.split('/')
return code
})
.filter(i => i)
.sort()
.join(';')
}
return ''
}

View File

@@ -1,3 +1,13 @@
module.exports = function () {
return this.tvg_language || ''
if (this.tvg_language) return this.tvg_language
if (this.languages.length) {
return this.languages
.map(language => (language ? language.name : null))
.filter(l => l)
.sort()
.join(';')
}
return ''
}

View File

@@ -1,3 +1,5 @@
module.exports = function () {
return this.tvg_logo || ''
if (this.tvg_logo) return this.tvg_logo
return this.logo || ''
}

View File

@@ -1,3 +0,0 @@
module.exports = function () {
return this.tvg_url || ''
}