This commit is contained in:
freearhey
2026-05-02 13:29:34 +03:00
parent 6826742c6b
commit 8f7940c8ca
8 changed files with 160 additions and 78 deletions

View File

@@ -1,4 +1,4 @@
import { isURI, getStreamInfo, loadIssues, createThread } from '../../utils'
import { getStreamInfo, loadIssues, createThread } from '../../utils'
import { STREAMS_DIR, LOGS_DIR } from '../../constants'
import { Playlist, Issue, Stream } from '../../models'
import { loadData, data as apiData } from '../../api'
@@ -12,6 +12,15 @@ const skippedIssues = new Collection<Issue>()
const logger = new Logger({ level: 5 })
let streams = new Collection<Stream>()
let cache = new Collection<Stream>()
function cacheData() {
cache = streams.clone()
}
function resetData() {
streams = cache
}
async function main() {
logger.info('loading data from api...')
@@ -97,7 +106,11 @@ async function removeStream(issue: Issue) {
log.start()
const data = issue.data
if (data.missing('stream_url')) return
if (data.missing('stream_url')) {
log.error('The request is missing the "Stream URL"')
skippedIssues.add(issue)
return
}
const streamUrls = data.getString('stream_url') || ''
@@ -105,55 +118,119 @@ async function removeStream(issue: Issue) {
streamUrls
.split(/\r?\n/)
.filter(Boolean)
.forEach(link => {
.forEach((link: string) => {
const found: Stream = streams.first((_stream: Stream) => _stream.url === link.trim())
if (found) {
found.removed = true
changed = true
log.info(`The stream with the URL "${link}" has been removed from the playlists`)
} else {
log.error(`The stream with the URL "${link}" is missing from the playlists`)
}
})
if (changed) processedIssues.add(issue)
if (changed) {
processedIssues.add(issue)
} else {
log.error(`None of the URLs specified in the request were found in the playlists`)
skippedIssues.add(issue)
}
}
async function editStream(issue: Issue) {
const log = createThread(issue, 'streams/edit')
log.start()
const data = issue.data
if (data.missing('stream_url')) return
const streamUrl = data.getString('stream_url')
const stream: Stream = streams.first(
(_stream: Stream) => _stream.url === data.getString('stream_url')
)
if (!stream) return
const streamId = data.getString('stream_id') || ''
const [channelId, feedId] = streamId.split('@')
if (channelId) {
stream.channel = channelId
stream.feed = feedId
stream.updateTvgId().updateTitle().updateFilepath()
if (!streamUrl) {
log.error('The request is missing the "Stream URL"')
skippedIssues.add(issue)
return
}
const stream: Stream = streams.first((_stream: Stream) => _stream.url === streamUrl)
if (!stream) {
log.error(`The stream with the URL "${streamUrl}" is already in the playlists`)
skippedIssues.add(issue)
return
}
cacheData()
stream.updateWithIssue(data)
const errors = new Collection<Error>()
errors.concat(stream.validate())
if (errors.isNotEmpty()) {
errors.forEach((err: Error) => {
log.error(err.message)
})
skippedIssues.add(issue)
resetData()
log.info('All changes have been reverted')
return
}
log.info('The stream description has been updated')
processedIssues.add(issue)
}
async function addStream(issue: Issue) {
const log = createThread(issue, 'streams/add')
log.start()
const data = issue.data
if (data.missing('stream_id') || data.missing('stream_url')) return
if (streams.includes((_stream: Stream) => _stream.url === data.getString('stream_url'))) return
const streamUrl = data.getString('stream_url') || ''
if (!isURI(streamUrl)) return
if (data.missing('stream_id')) {
log.error('The request is missing the "Stream ID"')
skippedIssues.add(issue)
return
}
const streamUrl = data.getString('stream_url')
if (!streamUrl) {
log.error('The request is missing the "Stream URL"')
skippedIssues.add(issue)
return
}
if (streams.includes((_stream: Stream) => _stream.url === streamUrl)) {
log.error(`The stream with the URL "${streamUrl}" is already included in the playlists`)
skippedIssues.add(issue)
return
}
const streamId = data.getString('stream_id') || ''
const [channelId, feedId] = streamId.split('@')
const channel: sdk.Models.Channel | undefined = apiData.channelsKeyById.get(channelId)
if (!channel) return
if (!channel) {
log.error(`There is no channel with the ID "${channelId}" in the database`)
skippedIssues.add(issue)
return
}
const blocklistRecords: sdk.Models.BlocklistRecord[] | undefined =
apiData.blocklistRecordsGroupedByChannel.get(channelId)
if (blocklistRecords) {
blocklistRecords.forEach((record: sdk.Models.BlocklistRecord) => {
if (record.reason === 'dmca') {
log.error(
`The channel has been added to our blocklist due to the claims of the copyright holder: ${record.ref}`
)
} else if (record.reason === 'nsfw') {
log.error(`The channel has been added to our blocklist due to NSFW content: ${record.ref}`)
}
})
skippedIssues.add(issue)
return
}
cacheData()
const label = data.getString('label') || ''
const httpUserAgent = data.getString('http_user_agent') || null
const httpReferrer = data.getString('http_referrer') || null
@@ -177,12 +254,27 @@ async function addStream(issue: Issue) {
url: streamUrl,
user_agent: httpUserAgent,
referrer: httpReferrer,
quality
quality,
label: data.getString('label') || ''
})
stream.label = label
stream.updateTitle().updateFilepath()
streams.add(stream)
const errors = new Collection<Error>()
errors.concat(stream.validate())
if (errors.isNotEmpty()) {
errors.forEach((err: Error) => {
log.error(err.message)
})
skippedIssues.add(issue)
resetData()
log.info('All changes have been reverted')
return
}
log.info('The stream has been added to playlists')
processedIssues.add(issue)
}