mirror of
https://github.com/iptv-org/iptv
synced 2026-05-07 10:07:05 -04:00
wip
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
import { IssueData } from '../core'
|
||||
import { DataSet } from '../core'
|
||||
|
||||
type IssueProps = {
|
||||
number: number
|
||||
labels: string[]
|
||||
data: IssueData
|
||||
data: DataSet
|
||||
}
|
||||
|
||||
export class Issue {
|
||||
number: number
|
||||
labels: string[]
|
||||
data: IssueData
|
||||
data: DataSet
|
||||
|
||||
constructor({ number, labels, data }: IssueProps) {
|
||||
this.number = number
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { normalizeURL, isURI } from '../utils'
|
||||
import { Collection } from '@freearhey/core'
|
||||
import parser from 'iptv-playlist-parser'
|
||||
import { normalizeURL } from '../utils'
|
||||
import * as sdk from '@iptv-org/sdk'
|
||||
import { IssueData } from '../core'
|
||||
import { DataSet } from '../core'
|
||||
import { data } from '../api'
|
||||
import path from 'node:path'
|
||||
|
||||
@@ -12,15 +12,32 @@ export class Stream extends sdk.Models.Stream {
|
||||
groupTitle: string = 'Undefined'
|
||||
removed: boolean = false
|
||||
tvgId?: string
|
||||
label: string | null
|
||||
statusCode?: string
|
||||
|
||||
updateWithIssue(issueData: IssueData): this {
|
||||
validate(): Collection<Error> {
|
||||
const errors = new Collection<Error>()
|
||||
if (!isURI(this.url)) {
|
||||
errors.add(new Error(`The stream URL "${this.url}" is invalid`))
|
||||
}
|
||||
|
||||
return errors
|
||||
}
|
||||
|
||||
updateWithIssue(dataSet: DataSet): this {
|
||||
const streamId = dataSet.getString('stream_id') || ''
|
||||
const [channelId, feedId] = streamId.split('@')
|
||||
|
||||
if (channelId) {
|
||||
this.channel = channelId
|
||||
this.feed = feedId
|
||||
this.updateTvgId().updateTitle().updateFilepath()
|
||||
}
|
||||
|
||||
const data = {
|
||||
label: issueData.getString('label'),
|
||||
quality: issueData.getString('quality'),
|
||||
httpUserAgent: issueData.getString('http_user_agent'),
|
||||
httpReferrer: issueData.getString('http_referrer')
|
||||
label: dataSet.getString('label'),
|
||||
quality: dataSet.getString('quality'),
|
||||
httpUserAgent: dataSet.getString('http_user_agent'),
|
||||
httpReferrer: dataSet.getString('http_referrer')
|
||||
}
|
||||
|
||||
if (data.label !== undefined) this.label = data.label
|
||||
|
||||
@@ -18,7 +18,7 @@ import fs from 'node:fs'
|
||||
export function isURI(string: string): boolean {
|
||||
try {
|
||||
const url = new URL(string)
|
||||
return /^(http:|https:|mmsh:|rtsp:|rtmp:)/.test(url.protocol)
|
||||
return /^(http:|https:|mmsh:|rtsp:|rtmp:|srt:|rtp:|udp:)/.test(url.protocol)
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user