iptv/scripts/commands/report/create.ts

107 lines
3.7 KiB
TypeScript
Raw Normal View History

2023-09-22 04:17:22 +02:00
import { Logger, Storage, Collection, Dictionary } from '@freearhey/core'
2023-09-17 03:08:50 +02:00
import { DATA_DIR, STREAMS_DIR } from '../../constants'
2023-09-22 04:17:22 +02:00
import { IssueLoader, PlaylistParser } from '../../core'
2023-09-17 03:08:50 +02:00
import { Blocked, Channel, Issue, Stream } from '../../models'
2023-09-15 17:40:35 +02:00
async function main() {
2023-09-17 03:08:50 +02:00
const logger = new Logger()
2023-09-15 17:40:35 +02:00
const loader = new IssueLoader()
const storage = new Storage(DATA_DIR)
2023-09-17 03:08:50 +02:00
logger.info('loading channels from api...')
2023-09-15 17:40:35 +02:00
const channelsContent = await storage.json('channels.json')
const groupedChannels = new Collection(channelsContent)
.map(data => new Channel(data))
.groupBy((channel: Channel) => channel.id)
2023-09-17 03:08:50 +02:00
logger.info('loading blocklist from api...')
2023-09-15 17:40:35 +02:00
const blocklistContent = await storage.json('blocklist.json')
const groupedBlocklist = new Collection(blocklistContent)
.map(data => new Blocked(data))
.groupBy((blocked: Blocked) => blocked.channel)
2023-09-17 03:08:50 +02:00
logger.info('loading streams...')
const streamsStorage = new Storage(STREAMS_DIR)
const parser = new PlaylistParser({ storage: streamsStorage })
const files = await streamsStorage.list('**/*.m3u')
const streams = await parser.parse(files)
const groupedStreams = streams.groupBy((stream: Stream) => stream.url)
logger.info('creating report...')
2023-09-18 17:24:40 +02:00
let report = new Collection()
logger.info('checking streams:add requests...')
const addRequests = await loader.load({ labels: ['streams:add'] })
2023-09-15 17:40:35 +02:00
const buffer = new Dictionary()
2023-09-18 17:24:40 +02:00
addRequests.forEach((issue: Issue) => {
2023-09-17 03:08:50 +02:00
const channelId = issue.data.get('channel_id') || undefined
const streamUrl = issue.data.get('stream_url') || undefined
2023-09-15 17:40:35 +02:00
const result = new Dictionary({
2023-09-17 03:08:50 +02:00
issueNumber: issue.number,
2023-09-18 17:24:40 +02:00
type: 'streams:add',
2023-09-15 17:40:35 +02:00
channelId,
status: undefined
})
2023-09-18 17:24:40 +02:00
if (!channelId) result.set('status', 'missing_id')
else if (!streamUrl) result.set('status', 'missing_link')
2023-09-15 17:40:35 +02:00
else if (groupedBlocklist.has(channelId)) result.set('status', 'blocked')
else if (groupedChannels.missing(channelId)) result.set('status', 'invalid_id')
else if (groupedStreams.has(streamUrl)) result.set('status', 'fullfilled')
else if (buffer.has(streamUrl)) result.set('status', 'duplicate')
else result.set('status', 'pending')
buffer.set(streamUrl, true)
2023-09-18 17:24:40 +02:00
report.add(result.data())
2023-09-15 17:40:35 +02:00
})
2023-09-18 17:24:40 +02:00
logger.info('checking streams:edit requests...')
const editRequests = await loader.load({ labels: ['streams:edit'] })
editRequests.forEach((issue: Issue) => {
const channelId = issue.data.get('channel_id') || undefined
const streamUrl = issue.data.get('stream_url') || undefined
const result = new Dictionary({
issueNumber: issue.number,
type: 'streams:edit',
channelId,
status: undefined
})
if (!streamUrl) result.set('status', 'missing_link')
else if (groupedStreams.missing(streamUrl)) result.set('status', 'invalid_link')
else if (channelId && groupedChannels.missing(channelId)) result.set('status', 'invalid_id')
else result.set('status', 'pending')
report.add(result.data())
})
logger.info('checking broken streams reports...')
const brokenStreamReports = await loader.load({ labels: ['broken stream'] })
brokenStreamReports.forEach((issue: Issue) => {
const streamUrl = issue.data.get('stream_url') || undefined
const result = new Dictionary({
issueNumber: issue.number,
type: 'broken stream',
channelId: undefined,
status: undefined
})
if (!streamUrl) result.set('status', 'missing_link')
else if (groupedStreams.missing(streamUrl)) result.set('status', 'invalid_link')
else result.set('status', 'pending')
report.add(result.data())
})
report = report.orderBy(item => item.issueNumber)
2023-09-15 17:40:35 +02:00
console.table(report.all())
}
main()