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 { MChannel, MChannelAccountDefault, MChannelSync } from '@server/types/models'
export async function synchronizeChannel (options: {
channel: MChannelAccountDefault
externalChannelUrl: string
+ videosCountLimit: number
channelSync?: MChannelSync
- videosCountLimit?: number
onlyAfter?: Date
}) {
const { channel, externalChannelUrl, videosCountLimit, onlyAfter, channelSync } = 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 targetUrls = await youtubeDL.getInfoForListImport({ latestVideosCount: videosCountLimit })
+ const targetUrls = await youtubeDL.getInfoForListImport({ latestVideosCount: videosCountLimit })
- 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[] = []
- const children: CreateJobArgument[] = []
+ for (const targetUrl of targetUrls) {
+ if (await skipImport(channel, targetUrl, onlyAfter)) continue
- for (const targetUrl of targetUrls) {
- if (await skipImport(channel, targetUrl, onlyAfter)) continue
+ const { job } = await buildYoutubeDLImport({
+ user,
+ channel,
+ targetUrl,
+ channelSync,
+ importDataOverride: {
+ privacy: VideoPrivacy.PUBLIC
+ }
+ })
- const { job } = await buildYoutubeDLImport({
- user,
- channel,
- targetUrl,
- channelSync,
- importDataOverride: {
- privacy: VideoPrivacy.PUBLIC
- }
- })
-
- children.push(job)
- }
+ children.push(job)
+ }
- // Will update the channel sync status
- const parent: CreateJobArgument = {
- type: 'after-video-channel-import',
- payload: {
- channelSyncId: channelSync?.id
+ // Will update the channel sync status
+ const parent: CreateJobArgument = {
+ type: 'after-video-channel-import',
+ payload: {
+ channelSyncId: channelSync?.id
+ }
}
- }
- await JobQueue.Instance.createJobWithChildren(parent, children)
+ 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()
+ }
}
// ---------------------------------------------------------------------------