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 { 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
21 const user = await UserModel.loadByChannelActorId(channel.actorId)
22 const youtubeDL = new YoutubeDLWrapper(
24 ServerConfigManager.Instance.getEnabledResolutions('vod'),
25 CONFIG.TRANSCODING.ALWAYS_TRANSCODE_ORIGINAL_RESOLUTION
28 const infoList = await youtubeDL.getInfoForListImport({ latestVideosCount: videosCountLimit })
30 const targetUrls = infoList
31 .filter(videoInfo => {
32 if (!onlyAfter) return true
34 return videoInfo.originallyPublishedAt.getTime() >= onlyAfter.getTime()
36 .map(videoInfo => videoInfo.webpageUrl)
39 'Fetched %d candidate URLs for sync channel %s.',
40 targetUrls.length, channel.Actor.preferredUsername, { targetUrls }
43 if (targetUrls.length === 0) {
45 channelSync.state = VideoChannelSyncState.SYNCED
46 await channelSync.save()
52 const children: CreateJobArgument[] = []
54 for (const targetUrl of targetUrls) {
55 if (await VideoImportModel.urlAlreadyImported(channel.id, targetUrl)) {
56 logger.debug('%s is already imported for channel %s, skipping video channel synchronization.', channel.name, targetUrl)
60 const { job } = await buildYoutubeDLImport({
66 privacy: VideoPrivacy.PUBLIC
73 const parent: CreateJobArgument = {
74 type: 'after-video-channel-import',
76 channelSyncId: channelSync?.id
80 await JobQueue.Instance.createJobWithChildren(parent, children)