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-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 channelSync?: MChannelSync
16 videosCountLimit?: number
19 const { channel, externalChannelUrl, videosCountLimit, onlyAfter, channelSync } = options
22 channelSync.state = VideoChannelSyncState.PROCESSING
23 channelSync.lastSyncAt = new Date()
24 await channelSync.save()
27 const user = await UserModel.loadByChannelActorId(channel.actorId)
28 const youtubeDL = new YoutubeDLWrapper(
30 ServerConfigManager.Instance.getEnabledResolutions('vod'),
31 CONFIG.TRANSCODING.ALWAYS_TRANSCODE_ORIGINAL_RESOLUTION
34 const targetUrls = await youtubeDL.getInfoForListImport({ latestVideosCount: videosCountLimit })
37 'Fetched %d candidate URLs for sync channel %s.',
38 targetUrls.length, channel.Actor.preferredUsername, { targetUrls }
41 if (targetUrls.length === 0) {
43 channelSync.state = VideoChannelSyncState.SYNCED
44 await channelSync.save()
50 const children: CreateJobArgument[] = []
52 for (const targetUrl of targetUrls) {
53 if (await skipImport(channel, targetUrl, onlyAfter)) continue
55 const { job } = await buildYoutubeDLImport({
61 privacy: VideoPrivacy.PUBLIC
68 // Will update the channel sync status
69 const parent: CreateJobArgument = {
70 type: 'after-video-channel-import',
72 channelSyncId: channelSync?.id
76 await JobQueue.Instance.createJobWithChildren(parent, children)
79 // ---------------------------------------------------------------------------
81 async function skipImport (channel: MChannel, targetUrl: string, onlyAfter?: Date) {
82 if (await VideoImportModel.urlAlreadyImported(channel.id, targetUrl)) {
83 logger.debug('%s is already imported for channel %s, skipping video channel synchronization.', channel.name, targetUrl)
88 const youtubeDL = new YoutubeDLWrapper(
90 ServerConfigManager.Instance.getEnabledResolutions('vod'),
91 CONFIG.TRANSCODING.ALWAYS_TRANSCODE_ORIGINAL_RESOLUTION
94 const videoInfo = await youtubeDL.getInfoForDownload()
96 const onlyAfterWithoutTime = new Date(onlyAfter)
97 onlyAfterWithoutTime.setHours(0, 0, 0)
99 if (videoInfo.originallyPublishedAtWithoutTime.getTime() >= onlyAfterWithoutTime.getTime()) {