mirror of
https://github.com/iptv-org/iptv.git
synced 2025-05-12 18:10:04 -04:00
Update playlist/validate.js
This commit is contained in:
parent
bb8baf348a
commit
bf03dae463
13 changed files with 154 additions and 1378 deletions
|
@ -1,37 +1,57 @@
|
|||
const { file, logger, api, parser, blocklist } = require('../../core')
|
||||
const { file, logger, api, parser, id } = require('../../core')
|
||||
const { program } = require('commander')
|
||||
const chalk = require('chalk')
|
||||
const _ = require('lodash')
|
||||
|
||||
program.argument('<filepath>', 'Path to file to validate').parse(process.argv)
|
||||
program.argument('[filepath]', 'Path to file to validate').parse(process.argv)
|
||||
|
||||
async function main() {
|
||||
const files = program.args.length ? program.args : await file.list('channels/*.m3u')
|
||||
|
||||
logger.info(`loading blocklist...`)
|
||||
await api.channels.load()
|
||||
await api.blocklist.load()
|
||||
|
||||
let blocklist = await api.blocklist.all()
|
||||
blocklist = blocklist
|
||||
.map(blocked => {
|
||||
const channel = api.channels.find({ id: blocked.channel })
|
||||
if (!channel) return null
|
||||
return { ...blocked, name: channel.name }
|
||||
})
|
||||
.filter(i => i)
|
||||
logger.info(`found ${blocklist.length} records`)
|
||||
|
||||
let errors = []
|
||||
let warnings = []
|
||||
for (const filepath of program.args) {
|
||||
for (const filepath of files) {
|
||||
if (!filepath.endsWith('.m3u')) continue
|
||||
|
||||
const basename = file.basename(filepath)
|
||||
const [_, countryCode] = basename.match(/([a-z]{2})(|_.*)\.m3u/i) || [null, null]
|
||||
const [__, country] = basename.match(/([a-z]{2})(|_.*)\.m3u/i) || [null, null]
|
||||
|
||||
const fileLog = []
|
||||
const streams = await parser.parsePlaylist(filepath)
|
||||
for (const stream of streams) {
|
||||
const found = blocklist.find(stream.name, countryCode.toUpperCase())
|
||||
if (found) {
|
||||
const items = await parser.parsePlaylist(filepath)
|
||||
for (const item of items) {
|
||||
if (item.tvg.id && !api.channels.find({ id: item.tvg.id })) {
|
||||
fileLog.push({
|
||||
type: 'error',
|
||||
line: stream.line,
|
||||
message: `"${found.name}" is on the blocklist due to claims of copyright holders (${found.reference})`
|
||||
type: 'warning',
|
||||
line: item.line,
|
||||
message: `"${item.tvg.id}" is not in the database`
|
||||
})
|
||||
}
|
||||
|
||||
if (stream.tvg.id && !api.channels.find({ id: stream.tvg.id })) {
|
||||
const channel_id = id.generate(item.name, country)
|
||||
const found = blocklist.find(
|
||||
blocked =>
|
||||
item.tvg.id.toLowerCase() === blocked.channel.toLowerCase() ||
|
||||
channel_id.toLowerCase() === blocked.channel.toLowerCase()
|
||||
)
|
||||
if (found) {
|
||||
fileLog.push({
|
||||
type: 'warning',
|
||||
line: stream.line,
|
||||
message: `"${stream.tvg.id}" is not in the database`
|
||||
type: 'error',
|
||||
line: item.line,
|
||||
message: `"${found.name}" is on the blocklist due to claims of copyright holders (${found.ref})`
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
const list = require('../data/blocklist')
|
||||
const parser = require('./parser')
|
||||
|
||||
const blocklist = {}
|
||||
|
||||
blocklist.find = function (title, country) {
|
||||
const name = parser.parseChannelName(title)
|
||||
|
||||
return list.find(item => {
|
||||
const regexp = new RegExp(item.regex, 'i')
|
||||
const hasSameName = regexp.test(name)
|
||||
const fromSameCountry = country === item.country
|
||||
|
||||
return hasSameName && fromSameCountry
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = blocklist
|
|
@ -1,23 +1,19 @@
|
|||
const file = require('./file')
|
||||
const parser = require('./parser')
|
||||
const transliteration = require('transliteration')
|
||||
const { transliterate } = require('transliteration')
|
||||
|
||||
const id = {}
|
||||
|
||||
id.generate = function (title, filepath) {
|
||||
const name = parser.parseChannelName(title)
|
||||
const code = parser.parseCountryCode(filepath)
|
||||
id.generate = function (name, code) {
|
||||
if (!name || !code) return null
|
||||
|
||||
if (name && code) {
|
||||
const slug = transliteration
|
||||
.transliterate(name)
|
||||
.replace(/\+/gi, 'Plus')
|
||||
.replace(/[^a-z\d]+/gi, '')
|
||||
name = name.replace(/ *\([^)]*\) */g, '')
|
||||
name = name.replace(/ *\[[^)]*\] */g, '')
|
||||
name = name.replace(/\+/gi, 'Plus')
|
||||
name = name.replace(/[^a-z\d]+/gi, '')
|
||||
name = name.trim()
|
||||
name = transliterate(name)
|
||||
code = code.toLowerCase()
|
||||
|
||||
return `${slug}.${code.toLowerCase()}`
|
||||
}
|
||||
|
||||
return null
|
||||
return `${name}.${code}`
|
||||
}
|
||||
|
||||
module.exports = id
|
||||
|
|
|
@ -10,4 +10,3 @@ exports.store = require('./store')
|
|||
exports.markdown = require('./markdown')
|
||||
exports.api = require('./api')
|
||||
exports.id = require('./id')
|
||||
exports.blocklist = require('./blocklist')
|
||||
|
|
|
@ -28,23 +28,4 @@ parser.parseNumber = function (string) {
|
|||
return parsed
|
||||
}
|
||||
|
||||
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)
|
||||
const [_, code] = basename.match(/^([a-z]{2})(_|\.)/) || [null, null]
|
||||
|
||||
return code
|
||||
}
|
||||
|
||||
module.exports = parser
|
||||
|
|
3
scripts/data/.gitignore
vendored
3
scripts/data/.gitignore
vendored
|
@ -1,3 +1,2 @@
|
|||
*
|
||||
!.gitignore
|
||||
!blocklist.json
|
||||
!.gitignore
|
File diff suppressed because one or more lines are too long
Loading…
Add table
Add a link
Reference in a new issue