]>
Commit | Line | Data |
---|---|---|
2a491182 F |
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' | |
11 | ||
12 | export async function synchronizeChannel (options: { | |
13 | channel: MChannelAccountDefault | |
14 | externalChannelUrl: string | |
15 | channelSync?: MChannelSync | |
16 | videosCountLimit?: number | |
17 | onlyAfter?: Date | |
18 | }) { | |
19 | const { channel, externalChannelUrl, videosCountLimit, onlyAfter, channelSync } = options | |
20 | ||
a3b472a1 C |
21 | if (channelSync) { |
22 | channelSync.state = VideoChannelSyncState.PROCESSING | |
23 | channelSync.lastSyncAt = new Date() | |
24 | await channelSync.save() | |
25 | } | |
26 | ||
2a491182 F |
27 | const user = await UserModel.loadByChannelActorId(channel.actorId) |
28 | const youtubeDL = new YoutubeDLWrapper( | |
29 | externalChannelUrl, | |
30 | ServerConfigManager.Instance.getEnabledResolutions('vod'), | |
31 | CONFIG.TRANSCODING.ALWAYS_TRANSCODE_ORIGINAL_RESOLUTION | |
32 | ) | |
33 | ||
34 | const infoList = await youtubeDL.getInfoForListImport({ latestVideosCount: videosCountLimit }) | |
35 | ||
36 | const targetUrls = infoList | |
37 | .filter(videoInfo => { | |
38 | if (!onlyAfter) return true | |
39 | ||
40 | return videoInfo.originallyPublishedAt.getTime() >= onlyAfter.getTime() | |
41 | }) | |
42 | .map(videoInfo => videoInfo.webpageUrl) | |
43 | ||
44 | logger.info( | |
45 | 'Fetched %d candidate URLs for sync channel %s.', | |
46 | targetUrls.length, channel.Actor.preferredUsername, { targetUrls } | |
47 | ) | |
48 | ||
49 | if (targetUrls.length === 0) { | |
50 | if (channelSync) { | |
51 | channelSync.state = VideoChannelSyncState.SYNCED | |
52 | await channelSync.save() | |
53 | } | |
54 | ||
55 | return | |
56 | } | |
57 | ||
58 | const children: CreateJobArgument[] = [] | |
59 | ||
60 | for (const targetUrl of targetUrls) { | |
61 | if (await VideoImportModel.urlAlreadyImported(channel.id, targetUrl)) { | |
62 | logger.debug('%s is already imported for channel %s, skipping video channel synchronization.', channel.name, targetUrl) | |
63 | continue | |
64 | } | |
65 | ||
66 | const { job } = await buildYoutubeDLImport({ | |
67 | user, | |
68 | channel, | |
69 | targetUrl, | |
70 | channelSync, | |
71 | importDataOverride: { | |
72 | privacy: VideoPrivacy.PUBLIC | |
73 | } | |
74 | }) | |
75 | ||
76 | children.push(job) | |
77 | } | |
78 | ||
a3b472a1 | 79 | // Will update the channel sync status |
2a491182 F |
80 | const parent: CreateJobArgument = { |
81 | type: 'after-video-channel-import', | |
82 | payload: { | |
83 | channelSyncId: channelSync?.id | |
84 | } | |
85 | } | |
86 | ||
87 | await JobQueue.Instance.createJobWithChildren(parent, children) | |
88 | } |