1 import { logger } from '@server/helpers/logger'
2 import { YoutubeDLWrapper } from '@server/helpers/youtube-dl'
3 import { CONFIG } from '@server/initializers/config'
4 import { buildYoutubeDLImport } from '@server/lib/video-pre-import'
5 import { UserModel } from '@server/models/user/user'
6 import { VideoImportModel } from '@server/models/video/video-import'
7 import { MChannel, MChannelAccountDefault, MChannelSync } from '@server/types/models'
8 import { VideoChannelSyncState, VideoPrivacy } from '@shared/models'
9 import { CreateJobArgument, JobQueue } from './job-queue'
10 import { ServerConfigManager } from './server-config-manager'
12 export async function synchronizeChannel (options: {
13 channel: MChannelAccountDefault
14 externalChannelUrl: string
15 videosCountLimit: number
16 channelSync?: MChannelSync
19 const { channel, externalChannelUrl, videosCountLimit, onlyAfter, channelSync } = options
22 channelSync.state = VideoChannelSyncState.PROCESSING
23 channelSync.lastSyncAt = new Date()
24 await channelSync.save()
28 const user = await UserModel.loadByChannelActorId(channel.actorId)
29 const youtubeDL = new YoutubeDLWrapper(
31 ServerConfigManager.Instance.getEnabledResolutions('vod'),
32 CONFIG.TRANSCODING.ALWAYS_TRANSCODE_ORIGINAL_RESOLUTION
35 const targetUrls = await youtubeDL.getInfoForListImport({ latestVideosCount: videosCountLimit })
38 'Fetched %d candidate URLs for sync channel %s.',
39 targetUrls.length, channel.Actor.preferredUsername, { targetUrls }
42 if (targetUrls.length === 0) {
44 channelSync.state = VideoChannelSyncState.SYNCED
45 await channelSync.save()
51 const children: CreateJobArgument[] = []
53 for (const targetUrl of targetUrls) {
54 if (await skipImport(channel, targetUrl, onlyAfter)) continue
56 const { job } = await buildYoutubeDLImport({
62 privacy: VideoPrivacy.PUBLIC
69 // Will update the channel sync status
70 const parent: CreateJobArgument = {
71 type: 'after-video-channel-import',
73 channelSyncId: channelSync?.id
77 await JobQueue.Instance.createJobWithChildren(parent, children)
79 logger.error(`Failed to import ${externalChannelUrl} in channel ${channel.name}`, { err })
80 channelSync.state = VideoChannelSyncState.FAILED
81 await channelSync.save()
85 // ---------------------------------------------------------------------------
87 async function skipImport (channel: MChannel, targetUrl: string, onlyAfter?: Date) {
88 if (await VideoImportModel.urlAlreadyImported(channel.id, targetUrl)) {
89 logger.debug('%s is already imported for channel %s, skipping video channel synchronization.', targetUrl, channel.name)
94 const youtubeDL = new YoutubeDLWrapper(
96 ServerConfigManager.Instance.getEnabledResolutions('vod'),
97 CONFIG.TRANSCODING.ALWAYS_TRANSCODE_ORIGINAL_RESOLUTION
100 const videoInfo = await youtubeDL.getInfoForDownload()
102 const onlyAfterWithoutTime = new Date(onlyAfter)
103 onlyAfterWithoutTime.setHours(0, 0, 0, 0)
105 if (videoInfo.originallyPublishedAtWithoutTime.getTime() < onlyAfterWithoutTime.getTime()) {