iptv/scripts/generators/index_region_m3u.js

58 lines
1.3 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-11 17:56:11 +01:00
streams = _.filter(streams, stream => stream.is_nsfw === false)
2022-02-07 23:11:47 +01:00
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-11 17:56:11 +01:00
if (!stream.broadcast_area.length) {
2022-02-07 23:11:47 +01:00
const item = _.cloneDeep(stream)
2022-02-11 17:56:11 +01:00
item.group_title = 'Undefined'
2022-02-07 23:11:47 +01:00
items.push(item)
return
}
2022-02-07 03:53:30 +01:00
2022-02-11 17:56:11 +01:00
getChannelRegions(stream, { 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-11 17:56:11 +01:00
if (i.group_title === 'Undefined') 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
2022-02-11 17:56:11 +01:00
function getChannelRegions(stream, { regions }) {
return stream.broadcast_area
2022-02-07 23:11:47 +01:00
.reduce((acc, item) => {
const [type, code] = item.split('/')
switch (type) {
case 'r':
acc.push(regions[code])
break
case 's':
2022-03-14 22:43:01 +01:00
const [c] = code.split('-')
2022-02-07 23:11:47 +01:00
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)
}