Files
iptv/scripts/commands/playlist/validate.ts

127 lines
4.1 KiB
TypeScript
Raw Normal View History

2025-02-27 21:19:12 +03:00
import { Logger, Storage, Collection, Dictionary } from '@freearhey/core'
2023-09-22 05:17:22 +03:00
import { PlaylistParser } from '../../core'
2025-03-29 11:39:46 +03:00
import { Channel, Stream, Blocked, Feed } from '../../models'
2023-09-15 18:40:35 +03:00
import { program } from 'commander'
import chalk from 'chalk'
2025-03-29 11:39:46 +03:00
import { uniqueId } from 'lodash'
2023-09-15 18:40:35 +03:00
import { DATA_DIR, STREAMS_DIR } from '../../constants'
program.argument('[filepath]', 'Path to file to validate').parse(process.argv)
type LogItem = {
type: string
line: number
message: string
}
async function main() {
const logger = new Logger()
2025-03-29 11:39:46 +03:00
logger.info('loading data from api...')
2023-09-17 04:08:50 +03:00
const dataStorage = new Storage(DATA_DIR)
2025-03-29 11:39:46 +03:00
const channelsData = await dataStorage.json('channels.json')
const channels = new Collection(channelsData).map(data => new Channel(data))
const channelsGroupedById = channels.keyBy((channel: Channel) => channel.id)
const feedsData = await dataStorage.json('feeds.json')
const feeds = new Collection(feedsData).map(data =>
new Feed(data).withChannel(channelsGroupedById)
)
const feedsGroupedByChannelId = feeds.groupBy((feed: Feed) =>
feed.channel ? feed.channel.id : uniqueId()
)
2023-09-17 04:08:50 +03:00
const blocklistContent = await dataStorage.json('blocklist.json')
2023-09-15 18:40:35 +03:00
const blocklist = new Collection(blocklistContent).map(data => new Blocked(data))
2025-03-29 11:39:46 +03:00
const blocklistGroupedByChannelId = blocklist.keyBy((blocked: Blocked) => blocked.channelId)
2023-09-15 18:40:35 +03:00
2023-09-17 04:08:50 +03:00
logger.info('loading streams...')
2023-09-15 18:40:35 +03:00
const streamsStorage = new Storage(STREAMS_DIR)
2025-03-29 11:39:46 +03:00
const parser = new PlaylistParser({
storage: streamsStorage,
channelsGroupedById,
feedsGroupedByChannelId
})
2023-09-15 18:40:35 +03:00
const files = program.args.length ? program.args : await streamsStorage.list('**/*.m3u')
2023-09-17 04:08:50 +03:00
const streams = await parser.parse(files)
logger.info(`found ${streams.count()} streams`)
2023-09-15 18:40:35 +03:00
2023-09-17 04:08:50 +03:00
let errors = new Collection()
let warnings = new Collection()
2025-03-29 11:39:46 +03:00
let streamsGroupedByFilepath = streams.groupBy((stream: Stream) => stream.getFilepath())
for (const filepath of streamsGroupedByFilepath.keys()) {
const streams = streamsGroupedByFilepath.get(filepath)
2023-09-17 04:08:50 +03:00
if (!streams) continue
2023-09-15 18:40:35 +03:00
const log = new Collection()
const buffer = new Dictionary()
2023-09-17 04:08:50 +03:00
streams.forEach((stream: Stream) => {
2025-03-29 11:39:46 +03:00
if (stream.channelId) {
const channel = channelsGroupedById.get(stream.channelId)
if (!channel) {
log.add({
type: 'warning',
line: stream.line,
message: `"${stream.id}" is not in the database`
})
}
2023-09-17 04:08:50 +03:00
}
2023-09-15 18:40:35 +03:00
2025-02-27 21:19:12 +03:00
const duplicate = stream.url && buffer.has(stream.url)
if (duplicate) {
2023-09-17 04:08:50 +03:00
log.add({
type: 'warning',
line: stream.line,
message: `"${stream.url}" is already on the playlist`
})
} else {
buffer.set(stream.url, true)
}
2023-09-15 18:40:35 +03:00
2025-03-29 11:39:46 +03:00
const blocked = stream.channel ? blocklistGroupedByChannelId.get(stream.channel.id) : false
2023-09-17 04:08:50 +03:00
if (blocked) {
2025-02-27 21:19:12 +03:00
if (blocked.reason === 'dmca') {
log.add({
type: 'error',
line: stream.line,
2025-03-29 11:39:46 +03:00
message: `"${blocked.channelId}" is on the blocklist due to claims of copyright holders (${blocked.ref})`
2025-02-27 21:19:12 +03:00
})
} else if (blocked.reason === 'nsfw') {
log.add({
type: 'error',
line: stream.line,
2025-03-29 11:39:46 +03:00
message: `"${blocked.channelId}" is on the blocklist due to NSFW content (${blocked.ref})`
2025-02-27 21:19:12 +03:00
})
}
2024-01-31 10:26:40 +00:00
}
2023-09-17 04:08:50 +03:00
})
2023-09-15 18:40:35 +03:00
if (log.notEmpty()) {
logger.info(`\n${chalk.underline(filepath)}`)
log.forEach((logItem: LogItem) => {
const position = logItem.line.toString().padEnd(6, ' ')
const type = logItem.type.padEnd(9, ' ')
const status = logItem.type === 'error' ? chalk.red(type) : chalk.yellow(type)
logger.info(` ${chalk.gray(position)}${status}${logItem.message}`)
})
errors = errors.concat(log.filter((logItem: LogItem) => logItem.type === 'error'))
warnings = warnings.concat(log.filter((logItem: LogItem) => logItem.type === 'warning'))
}
}
logger.error(
chalk.red(
`\n${
errors.count() + warnings.count()
} problems (${errors.count()} errors, ${warnings.count()} warnings)`
)
)
if (errors.count()) {
process.exit(1)
}
}
main()