iptv/scripts/core/parser.js

51 lines
1.2 KiB
JavaScript
Raw Normal View History

2021-12-12 05:10:18 +01:00
const ipp = require('iptv-playlist-parser')
const logger = require('./logger')
const file = require('./file')
const parser = {}
parser.parsePlaylist = async function (filepath) {
const content = await file.read(filepath)
const playlist = ipp.parse(content)
return playlist.items
}
parser.parseLogs = async function (filepath) {
const content = await file.read(filepath)
if (!content) return []
const lines = content.split('\n')
return lines.map(line => (line ? JSON.parse(line) : null)).filter(l => l)
}
parser.parseNumber = function (string) {
const parsed = parseInt(string)
if (isNaN(parsed)) {
2021-12-12 07:05:55 +01:00
throw new Error('scripts/core/parser.js:parseNumber() Input value is not a number')
2021-12-12 05:10:18 +01:00
}
return parsed
}
2022-02-05 05:25:56 +01:00
parser.parseChannelName = function (string) {
return string
.trim()
.split(' ')
.map(s => s.trim())
.filter(s => {
return !/\[|\]/i.test(s) && !/\((\d+)P\)/i.test(s)
})
.join(' ')
}
parser.parseCountryCode = function (filepath) {
if (!filepath) return null
const basename = file.basename(filepath)
2022-02-05 05:45:12 +01:00
const [_, code] = basename.match(/^([a-z]{2})(_|\.)/) || [null, null]
2022-02-05 05:25:56 +01:00
return code
}
2021-12-12 05:10:18 +01:00
module.exports = parser