iptv/scripts/generators/index_country_m3u.js

70 lines
1.7 KiB
JavaScript
Raw Normal View History

2022-02-07 02:42:15 +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
2022-02-07 02:42:15 +01:00
await api.regions.load()
let regions = await api.regions.all()
regions = _.keyBy(regions, 'code')
await api.countries.load()
let countries = await api.countries.all()
countries = _.keyBy(countries, 'code')
2022-03-14 22:42:57 +01:00
await api.subdivisions.load()
let subdivisions = await api.subdivisions.all()
subdivisions = _.keyBy(subdivisions, 'code')
2022-02-07 02:42:15 +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 02:42:15 +01:00
2022-03-14 22:42:57 +01:00
const broadcastCountries = getBroadcastCountries(stream, { countries, regions, subdivisions })
broadcastCountries.forEach(country => {
2022-02-07 02:42:15 +01:00
const item = _.cloneDeep(stream)
item.group_title = country.name
items.push(item)
})
})
2022-02-07 23:11:47 +01:00
items = _.sortBy(items, item => {
2022-02-11 17:56:11 +01:00
if (item.group_title === 'Undefined') return ''
2022-02-07 23:11:47 +01:00
return item.group_title
2022-02-07 02:42:15 +01:00
})
return { filepath: 'index.country.m3u', items }
}
2022-03-14 22:42:57 +01:00
function getBroadcastCountries(stream, { countries, regions, subdivisions }) {
2022-02-11 17:56:11 +01:00
let codes = stream.broadcast_area.reduce((acc, item) => {
2022-02-07 02:42:15 +01:00
const [type, code] = item.split('/')
switch (type) {
case 'c':
acc.push(code)
break
case 'r':
if (regions[code]) {
acc = acc.concat(regions[code].countries)
}
break
case 's':
2022-03-14 22:42:57 +01:00
if (subdivisions[code]) {
acc.push(subdivisions[code].country)
}
2022-02-07 02:42:15 +01:00
break
}
return acc
}, [])
codes = _.uniq(codes)
return codes.map(code => countries[code]).filter(c => c)
}