iptv/scripts/generators/index_region_m3u.js

57 lines
1.4 KiB
JavaScript
Raw Normal View History

2022-02-07 03:53:30 +01:00
const api = require('../core/api')
const _ = require('lodash')
module.exports = async function (streams = []) {
2022-02-07 23:11:47 +01:00
streams = _.filter(streams, stream => !stream.channel || stream.channel.is_nsfw === false)
await api.regions.load()
let regions = await api.regions.all()
regions = _.keyBy(regions, 'code')
2022-02-07 03:53:30 +01:00
let items = []
streams.forEach(stream => {
2022-02-07 23:11:47 +01:00
if (!stream.channel || !stream.channel.broadcast_area.length) {
const item = _.cloneDeep(stream)
item.group_title = null
items.push(item)
return
}
2022-02-07 03:53:30 +01:00
2022-02-07 23:11:47 +01:00
getChannelRegions(stream.channel, { regions }).forEach(region => {
2022-02-07 03:53:30 +01:00
const item = _.cloneDeep(stream)
item.group_title = region.name
items.push(item)
})
})
2022-02-07 23:11:47 +01:00
2022-02-07 03:53:30 +01:00
items = _.sortBy(items, i => {
2022-02-07 23:11:47 +01:00
if (!i.group_title) return ''
2022-02-07 03:53:30 +01:00
return i.group_title
})
return { filepath: 'index.region.m3u', items }
}
2022-02-07 23:11:47 +01:00
function getChannelRegions(channel, { regions }) {
return channel.broadcast_area
.reduce((acc, item) => {
const [type, code] = item.split('/')
switch (type) {
case 'r':
acc.push(regions[code])
break
case 's':
const [c] = item.split('-')
const r1 = _.filter(regions, { countries: [c] })
acc = acc.concat(r1)
break
case 'c':
const r2 = _.filter(regions, { countries: [code] })
acc = acc.concat(r2)
break
}
return acc
}, [])
.filter(i => i)
}