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

199 lines
5.6 KiB
TypeScript
Raw Normal View History

2025-04-16 20:54:55 +03:00
import { DataLoader, DataProcessor, IssueLoader, PlaylistParser } from '../../core'
2023-09-22 05:17:22 +03:00
import { Logger, Storage, Collection, Dictionary } from '@freearhey/core'
2025-04-16 20:54:55 +03:00
import type { DataProcessorData } from '../../types/dataProcessor'
import { Stream, Playlist, Channel, Issue } from '../../models'
import type { DataLoaderData } from '../../types/dataLoader'
2023-09-17 04:08:50 +03:00
import { DATA_DIR, STREAMS_DIR } from '../../constants'
import validUrl from 'valid-url'
2023-09-15 18:40:35 +03:00
let processedIssues = new Collection()
async function main() {
2025-04-24 04:09:04 +03:00
const logger = new Logger({ level: -999 })
2025-04-16 20:54:55 +03:00
const issueLoader = new IssueLoader()
2023-09-15 18:40:35 +03:00
2025-02-27 20:55:39 +03:00
logger.info('loading issues...')
2025-04-16 20:54:55 +03:00
const issues = await issueLoader.load()
2025-02-27 20:55:39 +03:00
2025-04-16 20:54:55 +03:00
logger.info('loading data from api...')
const processor = new DataProcessor()
2023-09-17 04:08:50 +03:00
const dataStorage = new Storage(DATA_DIR)
2025-04-16 20:54:55 +03:00
const dataLoader = new DataLoader({ storage: dataStorage })
const data: DataLoaderData = await dataLoader.load()
2025-07-10 21:13:43 +03:00
const { channelsKeyById, feedsGroupedByChannelId, logosGroupedByStreamId }: DataProcessorData =
processor.process(data)
2023-09-15 18:40:35 +03:00
2023-09-17 04:08:50 +03:00
logger.info('loading streams...')
const streamsStorage = new Storage(STREAMS_DIR)
2025-03-29 11:39:46 +03:00
const parser = new PlaylistParser({
storage: streamsStorage,
feedsGroupedByChannelId,
2025-07-10 21:13:43 +03:00
logosGroupedByStreamId,
2025-04-16 20:54:55 +03:00
channelsKeyById
2025-03-29 11:39:46 +03:00
})
2023-09-17 04:08:50 +03:00
const files = await streamsStorage.list('**/*.m3u')
2025-03-29 11:39:46 +03:00
const streams = await parser.parse(files)
2023-09-17 04:08:50 +03:00
2025-05-01 00:51:41 +03:00
logger.info('removing streams...')
2025-03-29 11:39:46 +03:00
await removeStreams({ streams, issues })
2023-09-15 18:40:35 +03:00
logger.info('edit stream description...')
2025-03-29 11:39:46 +03:00
await editStreams({
streams,
issues,
2025-04-16 20:54:55 +03:00
channelsKeyById,
2025-03-29 11:39:46 +03:00
feedsGroupedByChannelId
})
2023-09-15 18:40:35 +03:00
logger.info('add new streams...')
2025-03-29 11:39:46 +03:00
await addStreams({
streams,
issues,
2025-04-16 20:54:55 +03:00
channelsKeyById,
2025-03-29 11:39:46 +03:00
feedsGroupedByChannelId
})
2023-09-15 18:40:35 +03:00
logger.info('saving...')
2025-03-29 11:39:46 +03:00
const groupedStreams = streams.groupBy((stream: Stream) => stream.getFilepath())
2023-09-15 18:40:35 +03:00
for (let filepath of groupedStreams.keys()) {
2023-09-18 01:51:20 +03:00
let streams = groupedStreams.get(filepath) || []
streams = streams.filter((stream: Stream) => stream.removed === false)
2023-09-15 18:40:35 +03:00
const playlist = new Playlist(streams, { public: false })
await streamsStorage.save(filepath, playlist.toString())
}
const output = processedIssues.map(issue_number => `closes #${issue_number}`).join(', ')
console.log(`OUTPUT=${output}`)
}
main()
2025-03-29 11:39:46 +03:00
async function removeStreams({ streams, issues }: { streams: Collection; issues: Collection }) {
2025-02-27 20:55:39 +03:00
const requests = issues.filter(
issue => issue.labels.includes('streams:remove') && issue.labels.includes('approved')
)
requests.forEach((issue: Issue) => {
2023-09-17 04:08:50 +03:00
const data = issue.data
2025-05-01 00:51:41 +03:00
if (data.missing('streamUrl')) return
2023-09-15 18:40:35 +03:00
2025-05-01 00:51:41 +03:00
const streamUrls = data.getString('streamUrl') || ''
2024-12-26 03:10:36 +03:00
let changed = false
2025-05-01 00:51:41 +03:00
streamUrls
2025-03-29 11:39:46 +03:00
.split(/\r?\n/)
.filter(Boolean)
.forEach(link => {
const found: Stream = streams.first((_stream: Stream) => _stream.url === link.trim())
if (found) {
found.removed = true
changed = true
}
})
2024-12-26 03:10:36 +03:00
if (changed) processedIssues.add(issue.number)
2023-09-15 18:40:35 +03:00
})
}
2025-03-29 11:39:46 +03:00
async function editStreams({
streams,
issues,
2025-04-16 20:54:55 +03:00
channelsKeyById,
2025-03-29 11:39:46 +03:00
feedsGroupedByChannelId
}: {
streams: Collection
issues: Collection
2025-04-16 20:54:55 +03:00
channelsKeyById: Dictionary
2025-03-29 11:39:46 +03:00
feedsGroupedByChannelId: Dictionary
}) {
2025-02-27 20:55:39 +03:00
const requests = issues.filter(
issue => issue.labels.includes('streams:edit') && issue.labels.includes('approved')
)
requests.forEach((issue: Issue) => {
2023-09-17 04:08:50 +03:00
const data = issue.data
2025-03-09 19:53:25 +03:00
if (data.missing('streamUrl')) return
2023-09-15 18:40:35 +03:00
2025-03-29 11:39:46 +03:00
let stream: Stream = streams.first(
2025-03-09 19:53:25 +03:00
(_stream: Stream) => _stream.url === data.getString('streamUrl')
2025-03-29 11:39:46 +03:00
)
2023-09-15 18:40:35 +03:00
if (!stream) return
2025-03-29 11:39:46 +03:00
const streamId = data.getString('streamId') || ''
const [channelId, feedId] = streamId.split('@')
if (channelId) {
stream
.setChannelId(channelId)
.setFeedId(feedId)
2025-04-16 20:54:55 +03:00
.withChannel(channelsKeyById)
2025-03-29 11:39:46 +03:00
.withFeed(feedsGroupedByChannelId)
.updateId()
2025-07-20 00:30:29 +03:00
.updateTitle()
2025-03-29 11:39:46 +03:00
.updateFilepath()
2023-09-15 18:40:35 +03:00
}
2025-05-01 00:51:41 +03:00
stream.update(data)
2023-09-15 18:40:35 +03:00
2023-09-17 04:08:50 +03:00
processedIssues.add(issue.number)
2023-09-15 18:40:35 +03:00
})
}
2025-03-29 11:39:46 +03:00
async function addStreams({
streams,
issues,
2025-04-16 20:54:55 +03:00
channelsKeyById,
2025-03-29 11:39:46 +03:00
feedsGroupedByChannelId
}: {
streams: Collection
issues: Collection
2025-04-16 20:54:55 +03:00
channelsKeyById: Dictionary
2025-03-29 11:39:46 +03:00
feedsGroupedByChannelId: Dictionary
}) {
2025-02-27 20:55:39 +03:00
const requests = issues.filter(
issue => issue.labels.includes('streams:add') && issue.labels.includes('approved')
)
requests.forEach((issue: Issue) => {
2023-09-17 04:08:50 +03:00
const data = issue.data
2025-03-29 11:39:46 +03:00
if (data.missing('streamId') || data.missing('streamUrl')) return
2025-03-09 19:53:25 +03:00
if (streams.includes((_stream: Stream) => _stream.url === data.getString('streamUrl'))) return
2025-04-16 20:54:55 +03:00
const streamUrl = data.getString('streamUrl') || ''
if (!isUri(streamUrl)) return
2023-09-15 18:40:35 +03:00
2025-03-29 11:39:46 +03:00
const streamId = data.getString('streamId') || ''
2025-04-16 20:54:55 +03:00
const [channelId, feedId] = streamId.split('@')
2023-09-15 18:40:35 +03:00
2025-04-16 20:54:55 +03:00
const channel: Channel = channelsKeyById.get(channelId)
2023-09-15 18:40:35 +03:00
if (!channel) return
2025-04-16 20:54:55 +03:00
const label = data.getString('label') || null
const quality = data.getString('quality') || null
const httpUserAgent = data.getString('httpUserAgent') || null
const httpReferrer = data.getString('httpReferrer') || null
2025-07-10 21:13:43 +03:00
const directives = data.getArray('directives') || []
2025-03-29 11:39:46 +03:00
2023-09-15 18:40:35 +03:00
const stream = new Stream({
2025-07-20 00:30:29 +03:00
channelId,
feedId,
title: channel.name,
2025-04-16 20:54:55 +03:00
url: streamUrl,
2025-07-20 00:30:29 +03:00
userAgent: httpUserAgent,
2025-04-16 20:54:55 +03:00
referrer: httpReferrer,
2025-07-10 21:13:43 +03:00
directives,
2025-04-16 20:54:55 +03:00
quality,
label
2023-09-15 18:40:35 +03:00
})
2025-04-16 20:54:55 +03:00
.withChannel(channelsKeyById)
2025-03-29 11:39:46 +03:00
.withFeed(feedsGroupedByChannelId)
2025-07-20 00:30:29 +03:00
.updateTitle()
2025-03-29 11:39:46 +03:00
.updateFilepath()
2023-09-15 18:40:35 +03:00
streams.add(stream)
2023-09-17 04:08:50 +03:00
processedIssues.add(issue.number)
2023-09-15 18:40:35 +03:00
})
}
2025-03-29 11:39:46 +03:00
function isUri(string: string) {
return validUrl.isUri(encodeURI(string))
}