Create scripts/store/setters

This commit is contained in:
Aleksandr Statciuk 2021-12-12 07:12:06 +03:00
parent 21b8ce8731
commit 42d8a670d2
13 changed files with 156 additions and 0 deletions

View File

@ -0,0 +1,8 @@
const categories = require('../../data/categories')
module.exports = function ({ group_title }) {
return group_title
.split(';')
.map(i => categories[i.toLowerCase()])
.filter(i => i)
}

View File

@ -0,0 +1,25 @@
const dataRegions = require('../../data/regions')
const dataCountries = require('../../data/countries')
module.exports = function ({ tvg_country, countries = [] }) {
if (tvg_country) {
return tvg_country
.split(';')
.reduce((acc, curr) => {
const region = dataRegions[curr]
if (region) {
for (let code of region.country_codes) {
if (!acc.includes(code)) acc.push(code)
}
} else {
acc.push(curr)
}
return acc
}, [])
.map(item => dataCountries[item])
.filter(i => i)
}
return countries
}

View File

@ -0,0 +1,3 @@
module.exports = function ({ tvg_url, guides = [] }) {
return tvg_url ? [tvg_url] : guides
}

View File

@ -0,0 +1,12 @@
exports.categories = require('./categories')
exports.countries = require('./countries')
exports.guides = require('./guides')
exports.is_broken = require('./is_broken')
exports.is_nsfw = require('./is_nsfw')
exports.languages = require('./languages')
exports.name = require('./name')
exports.regions = require('./regions')
exports.resolution = require('./resolution')
exports.src_country = require('./src_country')
exports.status = require('./status')
exports.url = require('./url')

View File

@ -0,0 +1,7 @@
module.exports = function ({ is_broken = false, status }) {
if (status) {
return status.level > 3 ? true : false
}
return is_broken
}

View File

@ -0,0 +1,3 @@
module.exports = function ({ categories }) {
return Array.isArray(categories) ? categories.filter(c => c.nsfw).length > 0 : false
}

View File

@ -0,0 +1,12 @@
const langs = require('../../data/languages')
module.exports = function ({ tvg_language, languages = [] }) {
if (tvg_language) {
return tvg_language
.split(';')
.map(name => langs.find(l => l.name === name))
.filter(i => i)
}
return languages
}

View File

@ -0,0 +1,10 @@
module.exports = function ({ title }) {
return title
.trim()
.split(' ')
.map(s => s.trim())
.filter(s => {
return !/\[|\]/i.test(s) && !/\((\d+)P\)/i.test(s)
})
.join(' ')
}

View File

@ -0,0 +1,22 @@
const _ = require('lodash')
let regions = require('../../data/regions')
module.exports = function ({ countries }) {
if (!countries.length) return []
const output = []
regions = Object.values(regions)
countries.forEach(country => {
regions
.filter(region => region.country_codes.includes(country.code))
.forEach(found => {
output.push({
name: found.name,
code: found.code
})
})
})
return _.uniqBy(output, 'code')
}

View File

@ -0,0 +1,9 @@
module.exports = function ({ title, resolution = {} }) {
if (title) {
const [_, h] = title.match(/\((\d+)P\)/i) || [null, null]
return h ? { height: parseInt(h), width: null } : resolution
}
return resolution
}

View File

@ -0,0 +1,13 @@
const { file } = require('../../core')
const countries = require('../../data/countries')
module.exports = function ({ filepath }) {
if (filepath) {
const basename = file.basename(filepath)
const [_, code] = basename.match(/([a-z]{2})(|_.*)\.m3u/i) || [null, null]
return code ? countries[code.toUpperCase()] : null
}
return null
}

View File

@ -0,0 +1,25 @@
const statuses = require('../../data/statuses')
module.exports = function ({ title, status = {} }) {
if (title) {
const [_, label] = title.match(/\[(.*)\]/i) || [null, null]
return Object.values(statuses).find(s => s.label === label) || statuses['online']
}
if (status) {
switch (status.code) {
case 'not_247':
case 'geo_blocked':
return status
case 'offline':
return statuses['not_247']
case 'timeout':
return statuses['timeout']
default:
return statuses['online']
}
}
return status
}

View File

@ -0,0 +1,7 @@
const normalize = require('normalize-url')
module.exports = function ({ url }) {
const normalized = normalize(url, { stripWWW: false })
return decodeURIComponent(normalized).replace(/\s/g, '+')
}