X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=sidebyside;f=server%2Flib%2Fsync-channel.ts;h=35af91429c1218bc08ffba4eb3440a4952b13321;hb=91a4893063402d7beabb3104f9b989b8f88b6038;hp=50f80e6f9273c94de64d0cff33033a406902979b;hpb=2a491182e483b97afb1b65c908b23cb48d591807;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/sync-channel.ts b/server/lib/sync-channel.ts index 50f80e6f9..35af91429 100644 --- a/server/lib/sync-channel.ts +++ b/server/lib/sync-channel.ts @@ -4,7 +4,7 @@ import { CONFIG } from '@server/initializers/config' import { buildYoutubeDLImport } from '@server/lib/video-import' import { UserModel } from '@server/models/user/user' import { VideoImportModel } from '@server/models/video/video-import' -import { MChannelAccountDefault, MChannelSync } from '@server/types/models' +import { MChannel, MChannelAccountDefault, MChannelSync } from '@server/types/models' import { VideoChannelSyncState, VideoPrivacy } from '@shared/models' import { CreateJobArgument, JobQueue } from './job-queue' import { ServerConfigManager } from './server-config-manager' @@ -18,64 +18,94 @@ export async function synchronizeChannel (options: { }) { const { channel, externalChannelUrl, videosCountLimit, onlyAfter, channelSync } = options - const user = await UserModel.loadByChannelActorId(channel.actorId) - const youtubeDL = new YoutubeDLWrapper( - externalChannelUrl, - ServerConfigManager.Instance.getEnabledResolutions('vod'), - CONFIG.TRANSCODING.ALWAYS_TRANSCODE_ORIGINAL_RESOLUTION - ) + if (channelSync) { + channelSync.state = VideoChannelSyncState.PROCESSING + channelSync.lastSyncAt = new Date() + await channelSync.save() + } - const infoList = await youtubeDL.getInfoForListImport({ latestVideosCount: videosCountLimit }) + try { + const user = await UserModel.loadByChannelActorId(channel.actorId) + const youtubeDL = new YoutubeDLWrapper( + externalChannelUrl, + ServerConfigManager.Instance.getEnabledResolutions('vod'), + CONFIG.TRANSCODING.ALWAYS_TRANSCODE_ORIGINAL_RESOLUTION + ) - const targetUrls = infoList - .filter(videoInfo => { - if (!onlyAfter) return true + const targetUrls = await youtubeDL.getInfoForListImport({ latestVideosCount: videosCountLimit }) - return videoInfo.originallyPublishedAt.getTime() >= onlyAfter.getTime() - }) - .map(videoInfo => videoInfo.webpageUrl) + logger.info( + 'Fetched %d candidate URLs for sync channel %s.', + targetUrls.length, channel.Actor.preferredUsername, { targetUrls } + ) - logger.info( - 'Fetched %d candidate URLs for sync channel %s.', - targetUrls.length, channel.Actor.preferredUsername, { targetUrls } - ) + if (targetUrls.length === 0) { + if (channelSync) { + channelSync.state = VideoChannelSyncState.SYNCED + await channelSync.save() + } - if (targetUrls.length === 0) { - if (channelSync) { - channelSync.state = VideoChannelSyncState.SYNCED - await channelSync.save() + return } - return - } + const children: CreateJobArgument[] = [] + + for (const targetUrl of targetUrls) { + if (await skipImport(channel, targetUrl, onlyAfter)) continue - const children: CreateJobArgument[] = [] + const { job } = await buildYoutubeDLImport({ + user, + channel, + targetUrl, + channelSync, + importDataOverride: { + privacy: VideoPrivacy.PUBLIC + } + }) - for (const targetUrl of targetUrls) { - if (await VideoImportModel.urlAlreadyImported(channel.id, targetUrl)) { - logger.debug('%s is already imported for channel %s, skipping video channel synchronization.', channel.name, targetUrl) - continue + children.push(job) } - const { job } = await buildYoutubeDLImport({ - user, - channel, - targetUrl, - channelSync, - importDataOverride: { - privacy: VideoPrivacy.PUBLIC + // 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) { + logger.error(`Failed to import channel ${channel.name}`, { err }) + channelSync.state = VideoChannelSyncState.FAILED + await channelSync.save() + } +} - children.push(job) +// --------------------------------------------------------------------------- + +async function skipImport (channel: MChannel, targetUrl: string, onlyAfter?: Date) { + if (await VideoImportModel.urlAlreadyImported(channel.id, targetUrl)) { + logger.debug('%s is already imported for channel %s, skipping video channel synchronization.', targetUrl, channel.name) + return true } - const parent: CreateJobArgument = { - type: 'after-video-channel-import', - payload: { - channelSyncId: channelSync?.id + if (onlyAfter) { + const youtubeDL = new YoutubeDLWrapper( + targetUrl, + ServerConfigManager.Instance.getEnabledResolutions('vod'), + CONFIG.TRANSCODING.ALWAYS_TRANSCODE_ORIGINAL_RESOLUTION + ) + + const videoInfo = await youtubeDL.getInfoForDownload() + + const onlyAfterWithoutTime = new Date(onlyAfter) + onlyAfterWithoutTime.setHours(0, 0, 0, 0) + + if (videoInfo.originallyPublishedAtWithoutTime.getTime() < onlyAfterWithoutTime.getTime()) { + return true } } - await JobQueue.Instance.createJobWithChildren(parent, children) + return false }