X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=inline;f=server%2Flib%2Fsync-channel.ts;h=3a805a943d6ab5c01271c37807ae54cf4d0ac58e;hb=dc9c9500bf5f0fd66906576ee3df4f1c49a1871d;hp=eb5ca17031e004a981d2c2b0183cbaa3daa7ae52;hpb=a3b472a12ec6e57dbe2f650419f8064864686eab;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/sync-channel.ts b/server/lib/sync-channel.ts index eb5ca1703..3a805a943 100644 --- a/server/lib/sync-channel.ts +++ b/server/lib/sync-channel.ts @@ -1,10 +1,10 @@ import { logger } from '@server/helpers/logger' import { YoutubeDLWrapper } from '@server/helpers/youtube-dl' import { CONFIG } from '@server/initializers/config' -import { buildYoutubeDLImport } from '@server/lib/video-import' +import { buildYoutubeDLImport } from '@server/lib/video-pre-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' @@ -12,8 +12,8 @@ import { ServerConfigManager } from './server-config-manager' export async function synchronizeChannel (options: { channel: MChannelAccountDefault externalChannelUrl: string + videosCountLimit: number channelSync?: MChannelSync - videosCountLimit?: number onlyAfter?: Date }) { const { channel, externalChannelUrl, videosCountLimit, onlyAfter, channelSync } = options @@ -24,65 +24,88 @@ export async function synchronizeChannel (options: { await channelSync.save() } - const user = await UserModel.loadByChannelActorId(channel.actorId) - const youtubeDL = new YoutubeDLWrapper( - externalChannelUrl, - ServerConfigManager.Instance.getEnabledResolutions('vod'), - CONFIG.TRANSCODING.ALWAYS_TRANSCODE_ORIGINAL_RESOLUTION - ) + try { + const user = await UserModel.loadByChannelActorId(channel.actorId) + const youtubeDL = new YoutubeDLWrapper( + externalChannelUrl, + ServerConfigManager.Instance.getEnabledResolutions('vod'), + CONFIG.TRANSCODING.ALWAYS_TRANSCODE_ORIGINAL_RESOLUTION + ) - const infoList = await youtubeDL.getInfoForListImport({ latestVideosCount: videosCountLimit }) + const targetUrls = await youtubeDL.getInfoForListImport({ latestVideosCount: videosCountLimit }) - const targetUrls = infoList - .filter(videoInfo => { - if (!onlyAfter) return true + logger.info( + 'Fetched %d candidate URLs for sync channel %s.', + targetUrls.length, channel.Actor.preferredUsername, { targetUrls } + ) - 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 } - ) + 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 } - }) + } - children.push(job) + await JobQueue.Instance.createJobWithChildren(parent, children) + } catch (err) { + logger.error(`Failed to import ${externalChannelUrl} in channel ${channel.name}`, { err }) + channelSync.state = VideoChannelSyncState.FAILED + await channelSync.save() } +} + +// --------------------------------------------------------------------------- + +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 + } + + 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) - // Will update the channel sync status - const parent: CreateJobArgument = { - type: 'after-video-channel-import', - payload: { - channelSyncId: channelSync?.id + if (videoInfo.originallyPublishedAtWithoutTime.getTime() < onlyAfterWithoutTime.getTime()) { + return true } } - await JobQueue.Instance.createJobWithChildren(parent, children) + return false }