You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iptv/scripts/commands/report/create.ts

177 lines
6.1 KiB
TypeScript

import { Logger, Storage, Collection, Dictionary } from '@freearhey/core'
2 years ago
import { DATA_DIR, STREAMS_DIR } from '../../constants'
import { IssueLoader, PlaylistParser } from '../../core'
4 months ago
import { Blocked, Channel, Issue, Stream, Feed } from '../../models'
import { uniqueId } from 'lodash'
2 years ago
async function main() {
2 years ago
const logger = new Logger()
2 years ago
const loader = new IssueLoader()
4 months ago
let report = new Collection()
2 years ago
5 months ago
logger.info('loading issues...')
const issues = await loader.load()
4 months ago
logger.info('loading data from api...')
const dataStorage = new Storage(DATA_DIR)
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()
)
const blocklistContent = await dataStorage.json('blocklist.json')
const blocklist = new Collection(blocklistContent).map(data => new Blocked(data))
const blocklistGroupedByChannelId = blocklist.keyBy((blocked: Blocked) => blocked.channelId)
5 months ago
logger.info('loading streams...')
const streamsStorage = new Storage(STREAMS_DIR)
4 months ago
const parser = new PlaylistParser({
storage: streamsStorage,
channelsGroupedById,
feedsGroupedByChannelId
})
5 months ago
const files = await streamsStorage.list('**/*.m3u')
const streams = await parser.parse(files)
const streamsGroupedByUrl = streams.groupBy((stream: Stream) => stream.url)
4 months ago
const streamsGroupedByChannelId = streams.groupBy((stream: Stream) => stream.channelId)
logger.info('checking broken streams reports...')
const brokenStreamReports = issues.filter(issue =>
issue.labels.find((label: string) => label === 'broken stream')
)
brokenStreamReports.forEach((issue: Issue) => {
const brokenLinks = issue.data.getArray('brokenLinks') || []
if (!brokenLinks.length) {
const result = {
issueNumber: issue.number,
type: 'broken stream',
streamId: undefined,
streamUrl: undefined,
status: 'missing_link'
}
5 months ago
4 months ago
report.add(result)
} else {
for (const streamUrl of brokenLinks) {
const result = {
issueNumber: issue.number,
type: 'broken stream',
streamId: undefined,
streamUrl: truncate(streamUrl),
status: 'pending'
}
2 years ago
4 months ago
if (streamsGroupedByUrl.missing(streamUrl)) {
result.status = 'wrong_link'
}
2 years ago
4 months ago
report.add(result)
}
}
})
2 years ago
logger.info('checking streams:add requests...')
5 months ago
const addRequests = issues.filter(issue => issue.labels.includes('streams:add'))
const addRequestsBuffer = new Dictionary()
2 years ago
addRequests.forEach((issue: Issue) => {
4 months ago
const streamId = issue.data.getString('streamId') || ''
const streamUrl = issue.data.getString('streamUrl') || ''
const [channelId] = streamId.split('@')
2 years ago
4 months ago
const result = {
2 years ago
issueNumber: issue.number,
2 years ago
type: 'streams:add',
4 months ago
streamId: streamId || undefined,
streamUrl: truncate(streamUrl),
5 months ago
status: 'pending'
4 months ago
}
2 years ago
4 months ago
if (!channelId) result.status = 'missing_id'
else if (!streamUrl) result.status = 'missing_link'
else if (blocklistGroupedByChannelId.has(channelId)) result.status = 'blocked'
else if (channelsGroupedById.missing(channelId)) result.status = 'wrong_id'
else if (streamsGroupedByUrl.has(streamUrl)) result.status = 'on_playlist'
else if (addRequestsBuffer.has(streamUrl)) result.status = 'duplicate'
else result.status = 'pending'
2 years ago
5 months ago
addRequestsBuffer.set(streamUrl, true)
2 years ago
4 months ago
report.add(result)
2 years ago
})
2 years ago
logger.info('checking streams:edit requests...')
4 months ago
const editRequests = issues.filter(issue =>
issue.labels.find((label: string) => label === 'streams:edit')
)
2 years ago
editRequests.forEach((issue: Issue) => {
4 months ago
const streamId = issue.data.getString('streamId') || ''
const streamUrl = issue.data.getString('streamUrl') || ''
const [channelId] = streamId.split('@')
2 years ago
4 months ago
const result = {
2 years ago
issueNumber: issue.number,
type: 'streams:edit',
4 months ago
streamId: streamId || undefined,
streamUrl: truncate(streamUrl),
5 months ago
status: 'pending'
4 months ago
}
5 months ago
4 months ago
if (!streamUrl) result.status = 'missing_link'
else if (streamsGroupedByUrl.missing(streamUrl)) result.status = 'invalid_link'
else if (channelId && channelsGroupedById.missing(channelId)) result.status = 'invalid_id'
5 months ago
4 months ago
report.add(result)
5 months ago
})
logger.info('checking channel search requests...')
const channelSearchRequests = issues.filter(issue =>
4 months ago
issue.labels.find((label: string) => label === 'channel search')
5 months ago
)
const channelSearchRequestsBuffer = new Dictionary()
channelSearchRequests.forEach((issue: Issue) => {
4 months ago
const streamId = issue.data.getString('channelId') || ''
const [channelId] = streamId.split('@')
2 years ago
4 months ago
const result = {
2 years ago
issueNumber: issue.number,
5 months ago
type: 'channel search',
4 months ago
streamId: streamId || undefined,
5 months ago
streamUrl: undefined,
status: 'pending'
4 months ago
}
2 years ago
4 months ago
if (!channelId) result.status = 'missing_id'
else if (channelsGroupedById.missing(channelId)) result.status = 'invalid_id'
else if (channelSearchRequestsBuffer.has(channelId)) result.status = 'duplicate'
else if (blocklistGroupedByChannelId.has(channelId)) result.status = 'blocked'
else if (streamsGroupedByChannelId.has(channelId)) result.status = 'fulfilled'
5 months ago
else {
const channelData = channelsGroupedById.get(channelId)
4 months ago
if (channelData.length && channelData[0].closed) result.status = 'closed'
5 months ago
}
5 months ago
channelSearchRequestsBuffer.set(channelId, true)
2 years ago
4 months ago
report.add(result)
2 years ago
})
5 months ago
report = report.orderBy(item => item.issueNumber).filter(item => item.status !== 'pending')
2 years ago
2 years ago
console.table(report.all())
}
main()
4 months ago
function truncate(string: string, limit: number = 100) {
if (!string) return string
if (string.length < limit) return string
return string.slice(0, limit) + '...'
}