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

203 lines
5.9 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() {
const logger = new Logger({ disabled: true })
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()
const { channelsKeyById, feedsGroupedByChannelId }: 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-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
2023-09-15 18:40:35 +03:00
logger.info('removing broken 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-03-09 19:53:25 +03:00
if (data.missing('brokenLinks')) return
2023-09-15 18:40:35 +03:00
2025-03-29 11:39:46 +03:00
const brokenLinks = data.getString('brokenLinks') || ''
2024-12-26 03:10:36 +03:00
let changed = false
2025-03-29 11:39:46 +03:00
brokenLinks
.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()
.updateName()
.updateFilepath()
2023-09-15 18:40:35 +03:00
}
2025-03-29 11:39:46 +03:00
const label = data.getString('label') || ''
const quality = data.getString('quality') || ''
const httpUserAgent = data.getString('httpUserAgent') || ''
const httpReferrer = data.getString('httpReferrer') || ''
if (data.has('label')) stream.setLabel(label)
if (data.has('quality')) stream.setQuality(quality)
2025-04-16 20:54:55 +03:00
if (data.has('httpUserAgent')) stream.setUserAgent(httpUserAgent)
if (data.has('httpReferrer')) stream.setReferrer(httpReferrer)
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-03-29 11:39:46 +03:00
2023-09-15 18:40:35 +03:00
const stream = new Stream({
2025-04-16 20:54:55 +03:00
channel: channelId,
feed: feedId,
2025-03-29 11:39:46 +03:00
name: data.getString('channelName') || channel.name,
2025-04-16 20:54:55 +03:00
url: streamUrl,
user_agent: httpUserAgent,
referrer: httpReferrer,
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)
.updateName()
.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))
}