Create /helpers directory

This commit is contained in:
Aleksandr Statciuk
2021-08-01 19:45:15 +03:00
parent f164c948ed
commit 16f409856e
8 changed files with 42 additions and 336 deletions

View File

@ -0,0 +1,380 @@
[
{
"name": "Animal Planet",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Asian Food Network",
"country": "sg",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Cooking Channel",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Discovery Asia",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Discovery Channel",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Discovery Civiliztion",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Discovery en Espanol",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Discovery Family",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Discovery Historia",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Discovery History",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Discovery Home and Health",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Discovery Life",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Discovery Science",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Discovery Shed",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Discovery Theater",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Discovery Travel and Living",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Discovery Turbo Xtra",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Discovery World",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Discovery",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "DIY Network",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "DKiss",
"country": "es",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "DMax",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Eurosport 1",
"country": "fr",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Eurosport 2",
"country": "fr",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Eurosport",
"country": "fr",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "eve",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Familia Discovery",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Fatafeat",
"country": "eg",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "FEM",
"country": "no",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Fine Living",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Food Network",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "food tv",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": ["foodtv"]
},
{
"name": "Frisbee",
"country": "it",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Giallo",
"country": "it",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "GolfTV",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": ["Golf TV"]
},
{
"name": "HGTV",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Investigation Discovery",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": ["ID Investigation Discovery", "ID Investigation", "ID"]
},
{
"name": "K2",
"country": "it",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Living Channel",
"country": "nz",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Mango",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Motortrend",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Nove",
"country": "it",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "OWN",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": ["Oprah"]
},
{
"name": "Quest Red",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Quest",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Real Time",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Sky TG24",
"country": "it",
"dmca_notice": "https://github.com/iptv-org/iptv/pull/2294",
"aliases": []
},
{
"name": "TLC",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Trvl Channel",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "TTV",
"country": "pl",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "TV Norge",
"country": "no",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Tvn 24 Bis",
"country": "pl",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "TVN 24",
"country": "pl",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Tvn 7",
"country": "pl",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "TVN Extra",
"country": "pl",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "TVN Fabula",
"country": "pl",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "TVN Meteo",
"country": "pl",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "TVN Style",
"country": "pl",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "TVN Turbo",
"country": "pl",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "TVN Warszawa",
"country": "pl",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "TVN",
"country": "pl",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "Vox",
"country": "no",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
},
{
"name": "World Discovery",
"country": "us",
"dmca_notice": "https://github.com/iptv-org/iptv/issues/1831",
"aliases": []
}
]

View File

@ -0,0 +1,147 @@
[
{
"name": "Auto",
"id": "auto",
"nsfw": false
},
{
"name": "Animation",
"id": "animation",
"nsfw": false
},
{
"name": "Business",
"id": "business",
"nsfw": false
},
{
"name": "Classic",
"id": "classic",
"nsfw": false
},
{
"name": "Comedy",
"id": "comedy",
"nsfw": false
},
{
"name": "Cooking",
"id": "cooking",
"nsfw": false
},
{
"name": "Culture",
"id": "culture",
"nsfw": false
},
{
"name": "Documentary",
"id": "documentary",
"nsfw": false
},
{
"name": "Education",
"id": "education",
"nsfw": false
},
{
"name": "Entertainment",
"id": "entertainment",
"nsfw": false
},
{
"name": "Family",
"id": "family",
"nsfw": false
},
{
"name": "General",
"id": "general",
"nsfw": false
},
{
"name": "Kids",
"id": "kids",
"nsfw": false
},
{
"name": "Legislative",
"id": "legislative",
"nsfw": false
},
{
"name": "Lifestyle",
"id": "lifestyle",
"nsfw": false
},
{
"name": "Local",
"id": "local",
"nsfw": false
},
{
"name": "Movies",
"id": "movies",
"nsfw": false
},
{
"name": "Music",
"id": "music",
"nsfw": false
},
{
"name": "News",
"id": "news",
"nsfw": false
},
{
"name": "Outdoor",
"id": "outdoor",
"nsfw": false
},
{
"name": "Relax",
"id": "relax",
"nsfw": false
},
{
"name": "Religious",
"id": "religious",
"nsfw": false
},
{
"name": "Series",
"id": "series",
"nsfw": false
},
{
"name": "Science",
"id": "science",
"nsfw": false
},
{
"name": "Shop",
"id": "shop",
"nsfw": false
},
{
"name": "Sports",
"id": "sports",
"nsfw": false
},
{
"name": "Travel",
"id": "travel",
"nsfw": false
},
{
"name": "Weather",
"id": "weather",
"nsfw": false
},
{
"name": "XXX",
"id": "xxx",
"nsfw": true
}
]

223
scripts/helpers/db.js Normal file
View File

@ -0,0 +1,223 @@
const categories = require('./categories')
const parser = require('./parser')
const utils = require('./utils')
const sfwCategories = categories.filter(c => !c.nsfw).map(c => c.name)
const db = {}
db.load = function () {
const items = parser.parseIndex()
for (const item of items) {
const playlist = parser.parsePlaylist(item.url)
db.playlists.add(playlist)
for (const channel of playlist.channels) {
db.channels.add(channel)
for (const country of channel.countries) {
if (!db.countries.has(country)) {
db.countries.add(country)
}
}
for (const language of channel.languages) {
if (!db.languages.has(language)) {
db.languages.add(language)
}
}
}
}
}
db.channels = {
list: [],
filter: null,
duplicates: true,
nsfw: true,
add(channel) {
this.list.push(channel)
},
get() {
let output
if (this.filter) {
switch (this.filter.field) {
case 'countries':
if (this.filter.value === 'undefined') {
output = this.list.filter(channel => !channel.countries.length)
} else {
output = this.list.filter(channel =>
channel.countries.map(c => c.code).includes(this.filter.value)
)
}
break
case 'languages':
if (this.filter.value === 'undefined') {
output = this.list.filter(channel => !channel.languages.length)
} else {
output = this.list.filter(channel =>
channel.languages.map(c => c.code).includes(this.filter.value)
)
}
break
case 'category':
if (this.filter.value === 'other') {
output = this.list.filter(channel => !channel.category)
} else {
output = this.list.filter(
channel => channel.category.toLowerCase() === this.filter.value
)
}
break
}
} else {
output = this.list
}
if (!this.duplicates) {
const buffer = []
output = output.filter(channel => {
const info = channel.getInfo()
if (buffer.includes(info)) return false
buffer.push(info)
return true
})
}
if (!this.nsfw) {
output = output.filter(channel => !channel.isNSFW())
}
this.nsfw = true
this.duplicates = true
this.filter = null
return output
},
removeDuplicates() {
this.duplicates = false
return this
},
removeNSFW() {
this.nsfw = false
return this
},
all() {
return this.list
},
sfw() {
return this.list.filter(i => sfwCategories.includes(i.category))
},
forCountry(country) {
this.filter = {
field: 'countries',
value: country.code
}
return this
},
forLanguage(language) {
this.filter = {
field: 'languages',
value: language.code
}
return this
},
forCategory(category) {
this.filter = {
field: 'category',
value: category.id
}
return this
},
count() {
return this.get().length
},
sortBy(fields) {
this.list = utils.sortBy(this.list, fields)
return this
}
}
db.countries = {
list: [],
has(country) {
return this.list.map(c => c.code).includes(country.code)
},
add(country) {
this.list.push(country)
},
all() {
return this.list
},
count() {
return this.list.length
},
sortBy(fields) {
this.list = utils.sortBy(this.list, fields)
return this
}
}
db.languages = {
list: [],
has(language) {
return this.list.map(c => c.code).includes(language.code)
},
add(language) {
this.list.push(language)
},
all() {
return this.list
},
count() {
return this.list.length
},
sortBy(fields) {
this.list = utils.sortBy(this.list, fields)
return this
}
}
db.categories = {
list: categories,
all() {
return this.list
},
count() {
return this.list.length
}
}
db.playlists = {
list: [],
add(playlist) {
this.list.push(playlist)
},
all() {
return this.list
},
only(list = []) {
return this.list.filter(playlist => list.includes(playlist.name))
},
except(list = []) {
return this.list.filter(playlist => !list.includes(playlist.name))
},
sortBy(fields) {
this.list = utils.sortBy(this.list, fields)
return this
},
count() {
return this.list.length
}
}
module.exports = db

16
scripts/helpers/log.js Normal file
View File

@ -0,0 +1,16 @@
const log = {}
log.print = function (string) {
process.stdout.write(string)
}
log.start = function () {
this.print('Starting...\n')
console.time('Done in')
}
log.finish = function () {
console.timeEnd('Done in')
}
module.exports = log

24
scripts/helpers/parser.js Normal file
View File

@ -0,0 +1,24 @@
const playlistParser = require('iptv-playlist-parser')
const Playlist = require('./Playlist')
const utils = require('./utils')
const file = require('./file')
const parser = {}
parser.parseIndex = function () {
const content = file.read('index.m3u')
const result = playlistParser.parse(content)
return result.items
}
parser.parsePlaylist = async function (url) {
const content = file.read(url)
const result = playlistParser.parse(content)
const name = file.getFilename(url)
const country = utils.code2name(name)
return new Playlist({ header: result.header, items: result.items, url, country, name })
}
module.exports = parser

View File

@ -0,0 +1,829 @@
{
"AFR": {
"name": "Africa",
"codes": [
"AO",
"BF",
"BI",
"BJ",
"BW",
"CD",
"CF",
"CG",
"CI",
"CM",
"CV",
"DJ",
"DZ",
"EG",
"EH",
"ER",
"ET",
"GA",
"GH",
"GM",
"GN",
"GQ",
"GW",
"KE",
"KM",
"LR",
"LS",
"LY",
"MA",
"MG",
"ML",
"MR",
"MU",
"MW",
"MZ",
"NA",
"NE",
"NG",
"RE",
"RW",
"SC",
"SD",
"SH",
"SL",
"SN",
"SO",
"SS",
"ST",
"SZ",
"TD",
"TF",
"TG",
"TN",
"TZ",
"UG",
"YT",
"ZA",
"ZM",
"ZW"
]
},
"AMER": {
"name": "Americas",
"codes": [
"AG",
"AI",
"AR",
"AW",
"BB",
"BL",
"BM",
"BO",
"BR",
"BS",
"BV",
"BZ",
"CA",
"CL",
"CO",
"CR",
"CU",
"CW",
"DM",
"DO",
"EC",
"FK",
"GD",
"GF",
"GL",
"GP",
"GS",
"GT",
"GY",
"HN",
"HT",
"JM",
"KN",
"KY",
"LC",
"MF",
"MQ",
"MS",
"MX",
"NI",
"PA",
"PE",
"PM",
"PR",
"PY",
"SR",
"SV",
"SX",
"TC",
"TT",
"US",
"UY",
"VC",
"VE",
"VG",
"VI"
]
},
"APAC": {
"name": "Asia-Pacific",
"codes": [
"AF",
"AS",
"AU",
"BD",
"BN",
"BT",
"CK",
"CN",
"FJ",
"FM",
"GU",
"ID",
"IN",
"JP",
"KH",
"KI",
"KP",
"KR",
"LA",
"LK",
"MH",
"MM",
"MN",
"MP",
"MV",
"MY",
"NC",
"NF",
"NP",
"NR",
"NU",
"NZ",
"PF",
"PG",
"PH",
"PK",
"PN",
"PW",
"SB",
"SG",
"TH",
"TK",
"TL",
"TO",
"TV",
"TW",
"VN",
"VU",
"WF",
"WS"
]
},
"ARAB": {
"name": "Arab world",
"codes": [
"AE",
"BH",
"DJ",
"DZ",
"EG",
"IQ",
"JO",
"KM",
"KW",
"LB",
"LY",
"MA",
"MR",
"OM",
"PS",
"QA",
"SA",
"SD",
"SO",
"SY",
"TN",
"YE"
]
},
"ASIA": {
"name": "Asia",
"codes": [
"AE",
"AF",
"AM",
"AZ",
"BD",
"BH",
"BN",
"BT",
"CN",
"CY",
"GE",
"ID",
"IL",
"IN",
"IQ",
"IR",
"JO",
"JP",
"KG",
"KH",
"KP",
"KR",
"KW",
"KZ",
"LA",
"LB",
"LK",
"MM",
"MN",
"MV",
"MY",
"NP",
"OM",
"PH",
"PK",
"PS",
"QA",
"RU",
"SA",
"SG",
"SY",
"TH",
"TJ",
"TL",
"TM",
"TR",
"TW",
"UZ",
"VN",
"YE"
]
},
"CARIB": {
"name": "Caribbean",
"codes": [
"AG",
"AI",
"AW",
"BB",
"BL",
"BS",
"CU",
"CW",
"DM",
"DO",
"GD",
"GP",
"HT",
"JM",
"KN",
"KY",
"LC",
"MF",
"MQ",
"MS",
"PR",
"SX",
"TC",
"TT",
"VC",
"VG",
"VI"
]
},
"CAS": {
"name": "Central Asia",
"codes": ["KG", "KZ", "TJ", "TM", "UZ"]
},
"CIS": {
"name": "Commonwealth of Independent States",
"codes": ["AM", "AZ", "BY", "KG", "KZ", "MD", "RU", "TJ", "UZ"]
},
"EMEA": {
"name": "Europe, Middle East and Africa",
"codes": [
"AD",
"AE",
"AL",
"AM",
"AO",
"AT",
"AZ",
"BA",
"BE",
"BF",
"BG",
"BH",
"BI",
"BJ",
"BW",
"BY",
"CD",
"CF",
"CG",
"CH",
"CI",
"CM",
"CV",
"CY",
"CZ",
"DE",
"DJ",
"DK",
"DZ",
"EE",
"EG",
"EH",
"ER",
"ES",
"ET",
"FI",
"FR",
"GA",
"GE",
"GH",
"GM",
"GN",
"GQ",
"GR",
"GW",
"HR",
"HU",
"IE",
"IQ",
"IR",
"IS",
"IT",
"JO",
"KE",
"KM",
"KW",
"KZ",
"LB",
"LI",
"LR",
"LS",
"LT",
"LU",
"LV",
"LY",
"MA",
"MC",
"MD",
"ME",
"MG",
"MK",
"ML",
"MR",
"MT",
"MU",
"MW",
"MZ",
"NA",
"NE",
"NG",
"NL",
"NO",
"OM",
"PL",
"PS",
"PT",
"QA",
"RE",
"RO",
"RS",
"RU",
"RW",
"SA",
"SC",
"SD",
"SE",
"SH",
"SI",
"SK",
"SL",
"SM",
"SN",
"SO",
"SS",
"ST",
"SY",
"SZ",
"TD",
"TF",
"TG",
"TN",
"TR",
"TZ",
"UA",
"UG",
"UK",
"VA",
"YE",
"YT",
"ZA",
"ZM",
"ZW"
]
},
"EUR": {
"name": "Europe",
"codes": [
"AD",
"AL",
"AM",
"AT",
"AZ",
"BA",
"BE",
"BG",
"BY",
"CH",
"CY",
"CZ",
"DE",
"DK",
"EE",
"ES",
"FI",
"FR",
"GE",
"GR",
"HR",
"HU",
"IE",
"IS",
"IT",
"KZ",
"LI",
"LT",
"LU",
"LV",
"MC",
"MD",
"ME",
"MK",
"MT",
"NL",
"NO",
"PL",
"PT",
"RO",
"RS",
"RU",
"SE",
"SI",
"SK",
"SM",
"TR",
"UA",
"UK",
"VA"
]
},
"HISPAM": {
"name": "Hispanic America",
"codes": [
"AR",
"BO",
"CL",
"CO",
"CR",
"CU",
"DO",
"EC",
"GT",
"HN",
"MX",
"NI",
"PA",
"PE",
"PR",
"PY",
"SV",
"UY",
"VE"
]
},
"LATAM": {
"name": "Latin America",
"codes": [
"AR",
"BL",
"BO",
"BR",
"CL",
"CO",
"CR",
"CU",
"DO",
"EC",
"GF",
"GP",
"GT",
"HN",
"HT",
"MF",
"MQ",
"MX",
"NI",
"PA",
"PE",
"PR",
"PY",
"SV",
"UY",
"VE"
]
},
"MAGHRIB": {
"name": "Maghrib",
"codes": ["DZ", "LY", "MA", "MR", "TN"]
},
"MEA": {
"name": "Middle East and Africa",
"codes": [
"AE",
"AO",
"BF",
"BH",
"BI",
"BJ",
"BW",
"CD",
"CF",
"CG",
"CI",
"CM",
"CV",
"DJ",
"DZ",
"EG",
"EH",
"ER",
"ET",
"GA",
"GH",
"GM",
"GN",
"GQ",
"GW",
"IQ",
"IR",
"JO",
"KE",
"KM",
"KW",
"LB",
"LR",
"LS",
"LY",
"MA",
"MG",
"ML",
"MR",
"MU",
"MW",
"MZ",
"NA",
"NE",
"NG",
"OM",
"PS",
"QA",
"RE",
"RW",
"SA",
"SC",
"SD",
"SH",
"SL",
"SN",
"SO",
"SS",
"ST",
"SY",
"SZ",
"TD",
"TF",
"TG",
"TN",
"TZ",
"UG",
"YE",
"YT",
"ZA",
"ZM",
"ZW"
]
},
"MENA": {
"name": "Middle East and North Africa",
"codes": [
"AE",
"BH",
"DJ",
"DZ",
"EG",
"EH",
"IQ",
"IR",
"JO",
"KW",
"LB",
"LY",
"MA",
"OM",
"PS",
"QA",
"SA",
"SD",
"SY",
"TN",
"YE"
]
},
"NORD": {
"name": "Nordics",
"codes": ["AX", "DK", "FO", "FI", "IS", "NO", "SE"]
},
"OCE": {
"name": "Oceania",
"codes": [
"AS",
"AU",
"CK",
"FJ",
"FM",
"GU",
"KI",
"MH",
"MP",
"NC",
"NF",
"NR",
"NU",
"NZ",
"PF",
"PG",
"PN",
"PW",
"SB",
"TK",
"TO",
"TV",
"VU",
"WF",
"WS"
]
},
"MIDEAST": {
"name": "Middle East",
"codes": [
"AE",
"BH",
"CY",
"EG",
"IL",
"IQ",
"IR",
"JO",
"KW",
"LB",
"OM",
"PS",
"QA",
"SA",
"SY",
"TR",
"YE"
]
},
"NORAM": {
"name": "North America",
"codes": [
"AG",
"AI",
"AW",
"BB",
"BL",
"BM",
"BS",
"BZ",
"CA",
"CR",
"CU",
"CW",
"DM",
"DO",
"GD",
"GL",
"GP",
"GT",
"HN",
"HT",
"JM",
"KN",
"KY",
"LC",
"MF",
"MQ",
"MS",
"MX",
"NI",
"PA",
"PM",
"PR",
"SV",
"SX",
"TC",
"TT",
"US",
"VC",
"VG",
"VI"
]
},
"SAS": {
"name": "South Asia",
"codes": ["AF", "BD", "BT", "IN", "LK", "MV", "NP", "PK"]
},
"SSA": {
"name": "Sub-Saharan Africa",
"codes": [
"AO",
"BF",
"BI",
"BJ",
"BW",
"CD",
"CF",
"CG",
"CI",
"CM",
"CV",
"DJ",
"ER",
"ET",
"GA",
"GH",
"GM",
"GN",
"GQ",
"GW",
"KE",
"KM",
"LR",
"LS",
"MG",
"ML",
"MR",
"MU",
"MW",
"MZ",
"NA",
"NE",
"NG",
"RW",
"SC",
"SD",
"SL",
"SN",
"SO",
"SS",
"ST",
"SZ",
"TD",
"TG",
"TZ",
"UG",
"ZA",
"ZM",
"ZW"
]
},
"WAFR": {
"name": "West Africa",
"codes": [
"BF",
"BJ",
"CI",
"CV",
"GH",
"GM",
"GN",
"GW",
"LR",
"ML",
"MR",
"NE",
"NG",
"SH",
"SL",
"SN",
"TG"
]
}
}

104
scripts/helpers/utils.js Normal file
View File

@ -0,0 +1,104 @@
const escapeStringRegexp = require('escape-string-regexp')
const transliteration = require('transliteration')
const iso6393 = require('@freearhey/iso-639-3')
const categories = require('./categories')
const regions = require('./regions')
const utils = {}
const intlDisplayNames = new Intl.DisplayNames(['en'], {
style: 'narrow',
type: 'region'
})
utils.name2id = function (name) {
return transliteration
.transliterate(name)
.replace(/\+/gi, 'Plus')
.replace(/[^a-z\d]+/gi, '')
}
utils.code2flag = function (code) {
code = code.toUpperCase()
switch (code) {
case 'UK':
return '🇬🇧'
case 'INT':
return '🌍'
case 'UNDEFINED':
return ''
default:
return code.replace(/./g, char => String.fromCodePoint(char.charCodeAt(0) + 127397))
}
}
utils.region2codes = function (region) {
region = region.toUpperCase()
return regions[region] ? regions[region].codes : []
}
utils.code2name = function (code) {
try {
code = code.toUpperCase()
if (regions[code]) return regions[code].name
if (code === 'US') return 'United States'
if (code === 'INT') return 'International'
return intlDisplayNames.of(code)
} catch (e) {
return null
}
}
utils.language2code = function (name) {
const lang = iso6393.find(l => l.name === name)
return lang && lang.code ? lang.code : null
}
utils.sortBy = function (arr, fields) {
return arr.sort((a, b) => {
for (let field of fields) {
let propA = a[field] ? a[field].toLowerCase() : ''
let propB = b[field] ? b[field].toLowerCase() : ''
if (propA === 'undefined') return 1
if (propB === 'undefined') return -1
if (propA === 'other') return 1
if (propB === 'other') return -1
if (propA < propB) return -1
if (propA > propB) return 1
}
return 0
})
}
utils.escapeStringRegexp = function (scring) {
return escapeStringRegexp(string)
}
utils.sleep = function (ms) {
return function (x) {
return new Promise(resolve => setTimeout(() => resolve(x), ms))
}
}
utils.removeProtocol = function (string) {
return string.replace(/(^\w+:|^)\/\//, '')
}
utils.filterPlaylists = function (arr, include = '', exclude = '') {
if (include) {
const included = include.split(',').map(filename => `channels/${filename}.m3u`)
return arr.filter(i => included.indexOf(i.url) > -1)
}
if (exclude) {
const excluded = exclude.split(',').map(filename => `channels/${filename}.m3u`)
return arr.filter(i => excluded.indexOf(i.url) === -1)
}
return arr
}
module.exports = utils