2024-02-16 03:14:12 -06:00
|
|
|
import { logger, loggerTagsFactory } from '@server/helpers/logger.js'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { YoutubeDLWrapper } from '@server/helpers/youtube-dl/index.js'
|
|
|
|
import { CONFIG } from '@server/initializers/config.js'
|
|
|
|
import { buildYoutubeDLImport } from '@server/lib/video-pre-import.js'
|
|
|
|
import { UserModel } from '@server/models/user/user.js'
|
|
|
|
import { VideoImportModel } from '@server/models/video/video-import.js'
|
|
|
|
import { MChannel, MChannelAccountDefault, MChannelSync } from '@server/types/models/index.js'
|
|
|
|
import { VideoChannelSyncState, VideoPrivacy } from '@peertube/peertube-models'
|
|
|
|
import { CreateJobArgument, JobQueue } from './job-queue/index.js'
|
|
|
|
import { ServerConfigManager } from './server-config-manager.js'
|
2022-08-10 02:53:39 -05:00
|
|
|
|
2024-02-16 03:14:12 -06:00
|
|
|
const lTags = loggerTagsFactory('channel-synchronization')
|
|
|
|
|
2022-08-10 02:53:39 -05:00
|
|
|
export async function synchronizeChannel (options: {
|
|
|
|
channel: MChannelAccountDefault
|
|
|
|
externalChannelUrl: string
|
2022-11-14 04:11:39 -06:00
|
|
|
videosCountLimit: number
|
2022-08-10 02:53:39 -05:00
|
|
|
channelSync?: MChannelSync
|
|
|
|
onlyAfter?: Date
|
|
|
|
}) {
|
|
|
|
const { channel, externalChannelUrl, videosCountLimit, onlyAfter, channelSync } = options
|
|
|
|
|
2022-08-10 04:51:13 -05:00
|
|
|
if (channelSync) {
|
|
|
|
channelSync.state = VideoChannelSyncState.PROCESSING
|
|
|
|
channelSync.lastSyncAt = new Date()
|
|
|
|
await channelSync.save()
|
|
|
|
}
|
|
|
|
|
2022-09-27 02:47:38 -05:00
|
|
|
try {
|
|
|
|
const user = await UserModel.loadByChannelActorId(channel.actorId)
|
|
|
|
const youtubeDL = new YoutubeDLWrapper(
|
|
|
|
externalChannelUrl,
|
|
|
|
ServerConfigManager.Instance.getEnabledResolutions('vod'),
|
|
|
|
CONFIG.TRANSCODING.ALWAYS_TRANSCODE_ORIGINAL_RESOLUTION
|
|
|
|
)
|
2022-08-10 02:53:39 -05:00
|
|
|
|
2022-09-27 02:47:38 -05:00
|
|
|
const targetUrls = await youtubeDL.getInfoForListImport({ latestVideosCount: videosCountLimit })
|
2022-08-10 02:53:39 -05:00
|
|
|
|
2022-09-27 02:47:38 -05:00
|
|
|
logger.info(
|
|
|
|
'Fetched %d candidate URLs for sync channel %s.',
|
2024-02-16 03:14:12 -06:00
|
|
|
targetUrls.length, channel.Actor.preferredUsername, { targetUrls, ...lTags() }
|
2022-09-27 02:47:38 -05:00
|
|
|
)
|
2022-08-10 02:53:39 -05:00
|
|
|
|
2022-09-27 02:47:38 -05:00
|
|
|
if (targetUrls.length === 0) {
|
|
|
|
if (channelSync) {
|
|
|
|
channelSync.state = VideoChannelSyncState.SYNCED
|
|
|
|
await channelSync.save()
|
2022-08-10 02:53:39 -05:00
|
|
|
}
|
|
|
|
|
2022-09-27 02:47:38 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const children: CreateJobArgument[] = []
|
|
|
|
|
|
|
|
for (const targetUrl of targetUrls) {
|
2024-02-16 03:14:12 -06:00
|
|
|
logger.debug(`Import candidate: ${targetUrl}`, lTags())
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (await skipImport(channel, targetUrl, onlyAfter)) continue
|
|
|
|
|
|
|
|
const { job } = await buildYoutubeDLImport({
|
|
|
|
user,
|
|
|
|
channel,
|
|
|
|
targetUrl,
|
|
|
|
channelSync,
|
|
|
|
importDataOverride: {
|
|
|
|
privacy: VideoPrivacy.PUBLIC
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
children.push(job)
|
|
|
|
} catch (err) {
|
|
|
|
logger.error(`Cannot build import for ${targetUrl} in channel ${channel.name}`, { err, ...lTags() })
|
|
|
|
}
|
2022-08-10 02:53:39 -05:00
|
|
|
}
|
|
|
|
|
2022-09-27 02:47:38 -05:00
|
|
|
// Will update the channel sync status
|
|
|
|
const parent: CreateJobArgument = {
|
|
|
|
type: 'after-video-channel-import',
|
|
|
|
payload: {
|
|
|
|
channelSyncId: channelSync?.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await JobQueue.Instance.createJobWithChildren(parent, children)
|
|
|
|
} catch (err) {
|
2024-02-16 03:14:12 -06:00
|
|
|
logger.error(`Failed to import ${externalChannelUrl} in channel ${channel.name}`, { err, ...lTags() })
|
2022-09-27 02:47:38 -05:00
|
|
|
channelSync.state = VideoChannelSyncState.FAILED
|
|
|
|
await channelSync.save()
|
|
|
|
}
|
2022-08-10 02:53:39 -05:00
|
|
|
}
|
2022-09-07 10:30:21 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function skipImport (channel: MChannel, targetUrl: string, onlyAfter?: Date) {
|
|
|
|
if (await VideoImportModel.urlAlreadyImported(channel.id, targetUrl)) {
|
2024-02-16 03:14:12 -06:00
|
|
|
logger.debug('%s is already imported for channel %s, skipping video channel synchronization.', targetUrl, channel.name, lTags())
|
2022-09-07 10:30:21 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if (onlyAfter) {
|
|
|
|
const youtubeDL = new YoutubeDLWrapper(
|
|
|
|
targetUrl,
|
|
|
|
ServerConfigManager.Instance.getEnabledResolutions('vod'),
|
|
|
|
CONFIG.TRANSCODING.ALWAYS_TRANSCODE_ORIGINAL_RESOLUTION
|
|
|
|
)
|
|
|
|
|
|
|
|
const videoInfo = await youtubeDL.getInfoForDownload()
|
|
|
|
|
2022-09-16 02:37:07 -05:00
|
|
|
const onlyAfterWithoutTime = new Date(onlyAfter)
|
2022-09-16 06:48:13 -05:00
|
|
|
onlyAfterWithoutTime.setHours(0, 0, 0, 0)
|
2022-09-16 02:37:07 -05:00
|
|
|
|
2022-09-16 06:48:13 -05:00
|
|
|
if (videoInfo.originallyPublishedAtWithoutTime.getTime() < onlyAfterWithoutTime.getTime()) {
|
2022-09-07 10:30:21 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|