iptv/scripts/parser.js

226 lines
5.5 KiB
JavaScript
Raw Normal View History

2021-01-30 00:26:29 +01:00
const playlistParser = require('iptv-playlist-parser')
const utils = require('./utils')
2021-01-31 18:11:56 +01:00
const categories = require('./categories')
const path = require('path')
2021-01-30 00:26:29 +01:00
const parser = {}
parser.parseIndex = function () {
const content = utils.readFile('index.m3u')
const result = playlistParser.parse(content)
return result.items
}
parser.parsePlaylist = function (filename) {
const content = utils.readFile(filename)
const result = playlistParser.parse(content)
2021-02-20 13:47:05 +01:00
const name = path.parse(filename).name
const country = utils.code2name(name)
2021-01-30 00:26:29 +01:00
2021-02-20 13:47:05 +01:00
return new Playlist({ header: result.header, items: result.items, url: filename, country, name })
2021-01-30 00:26:29 +01:00
}
class Playlist {
2021-02-20 13:47:05 +01:00
constructor({ header, items, url, name, country }) {
2021-01-30 00:26:29 +01:00
this.url = url
2021-02-20 13:47:05 +01:00
this.name = name
this.country = country
2021-01-30 00:26:29 +01:00
this.header = header
this.channels = items
.map(item => new Channel({ data: item, header, sourceUrl: url }))
.filter(channel => channel.url)
}
2021-01-30 05:19:17 +01:00
toString(short = false) {
2021-01-30 00:26:29 +01:00
let parts = ['#EXTM3U']
for (let key in this.header.attrs) {
let value = this.header.attrs[key]
if (value) {
parts.push(`${key}="${value}"`)
}
}
2021-01-30 05:19:17 +01:00
let output = `${parts.join(' ')}\n`
for (let channel of this.channels) {
output += channel.toString(short)
}
return output
2021-01-30 00:26:29 +01:00
}
}
class Channel {
constructor({ data, header, sourceUrl }) {
this.parseData(data)
this.filename = utils.getBasename(sourceUrl)
2021-01-30 00:26:29 +01:00
if (!this.countries.length) {
const countryName = utils.code2name(this.filename)
this.countries = countryName ? [{ code: this.filename, name: countryName }] : []
2021-01-31 18:11:56 +01:00
this.tvg.country = this.countries.map(c => c.code.toUpperCase()).join(';')
2021-01-30 00:26:29 +01:00
}
this.tvg.url = header.attrs['x-tvg-url'] || ''
}
parseData(data) {
2021-01-31 18:11:56 +01:00
const title = this.parseTitle(data.name)
2021-01-30 00:26:29 +01:00
this.tvg = data.tvg
this.http = data.http
this.url = data.url
this.logo = data.tvg.logo
this.name = title.channelName
this.status = title.streamStatus
this.resolution = title.streamResolution
2021-01-31 18:11:56 +01:00
this.countries = this.parseCountries(data.tvg.country)
this.languages = this.parseLanguages(data.tvg.language)
this.category = this.parseCategory(data.group.title)
}
parseCountries(string) {
let arr = string
.split(';')
.reduce((acc, curr) => {
const codes = utils.region2codes(curr)
if (codes.length) {
for (let code of codes) {
if (!acc.includes(code)) {
acc.push(code)
}
}
} else {
acc.push(curr)
}
return acc
}, [])
.filter(code => code && utils.code2name(code))
return arr.map(code => {
return { code: code.toLowerCase(), name: utils.code2name(code) }
})
}
parseLanguages(string) {
return string
.split(';')
.map(name => {
const code = name ? utils.language2code(name) : null
if (!code) return null
return {
code,
name
}
})
.filter(l => l)
}
parseCategory(string) {
const category = categories.find(c => c.id === string.toLowerCase())
return category ? category.name : ''
}
parseTitle(title) {
const channelName = title
.trim()
.split(' ')
.map(s => s.trim())
.filter(s => {
return !/\[|\]/i.test(s) && !/\((\d+)P\)/i.test(s)
})
.join(' ')
const streamStatusMatch = title.match(/\[(.*)\]/i)
const streamStatus = streamStatusMatch ? streamStatusMatch[1] : null
const streamResolutionMatch = title.match(/\((\d+)P\)/i)
const streamResolutionHeight = streamResolutionMatch ? parseInt(streamResolutionMatch[1]) : null
const streamResolution = { width: null, height: streamResolutionHeight }
return { channelName, streamStatus, streamResolution }
2021-01-30 00:26:29 +01:00
}
get tvgCountry() {
2021-01-31 18:11:56 +01:00
return this.tvg.country
.split(';')
.map(code => utils.code2name(code))
.join(';')
2021-01-30 00:26:29 +01:00
}
get tvgLanguage() {
2021-01-31 18:11:56 +01:00
return this.tvg.language
2021-01-30 00:26:29 +01:00
}
get tvgUrl() {
return (this.tvg.id || this.tvg.name) && this.tvg.url ? this.tvg.url : ''
}
get tvgId() {
if (this.tvg.id) {
return this.tvg.id
} else if (this.filename !== 'unsorted') {
const id = utils.name2id(this.tvgName)
return id ? `${id}.${this.filename}` : ''
}
return ''
}
get tvgName() {
return this.tvg.name || this.name
}
2021-01-30 00:26:29 +01:00
toString(short = false) {
2021-01-31 18:11:56 +01:00
this.tvg.country = this.tvg.country.toUpperCase()
let info = `-1 tvg-id="${this.tvgId}" tvg-name="${this.tvgName}" tvg-country="${this.tvg.country}" tvg-language="${this.tvg.language}" tvg-logo="${this.logo}"`
2021-01-30 00:26:29 +01:00
if (!short) {
info += ` tvg-url="${this.tvgUrl}"`
}
info += ` group-title="${this.category}",${this.name}`
if (this.resolution.height) {
info += ` (${this.resolution.height}p)`
}
if (this.status) {
info += ` [${this.status}]`
}
if (this.http['referrer']) {
info += `\n#EXTVLCOPT:http-referrer=${this.http['referrer']}`
}
if (this.http['user-agent']) {
info += `\n#EXTVLCOPT:http-user-agent=${this.http['user-agent']}`
}
return '#EXTINF:' + info + '\n' + this.url + '\n'
}
toJSON() {
return {
name: this.name,
logo: this.logo || null,
url: this.url,
category: this.category || null,
languages: this.languages,
countries: this.countries,
tvg: {
id: this.tvgId || null,
name: this.tvgName || null,
2021-01-30 00:26:29 +01:00
url: this.tvg.url || null
}
}
}
}
module.exports = parser