]>
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' | |
6740b642 | 4 | import { buildYoutubeDLImport } from '@server/lib/video-pre-import' |
2a491182 F |
5 | import { UserModel } from '@server/models/user/user' |
6 | import { VideoImportModel } from '@server/models/video/video-import' | |
e9fc9e03 | 7 | import { MChannel, MChannelAccountDefault, MChannelSync } from '@server/types/models' |
2a491182 F |
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 | |
4efa5535 | 15 | videosCountLimit: number |
2a491182 | 16 | channelSync?: MChannelSync |
2a491182 F |
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 | ||
97922ecf C |
27 | try { |
28 | const user = await UserModel.loadByChannelActorId(channel.actorId) | |
29 | const youtubeDL = new YoutubeDLWrapper( | |
30 | externalChannelUrl, | |
31 | ServerConfigManager.Instance.getEnabledResolutions('vod'), | |
32 | CONFIG.TRANSCODING.ALWAYS_TRANSCODE_ORIGINAL_RESOLUTION | |
33 | ) | |
2a491182 | 34 | |
97922ecf | 35 | const targetUrls = await youtubeDL.getInfoForListImport({ latestVideosCount: videosCountLimit }) |
2a491182 | 36 | |
97922ecf C |
37 | logger.info( |
38 | 'Fetched %d candidate URLs for sync channel %s.', | |
39 | targetUrls.length, channel.Actor.preferredUsername, { targetUrls } | |
40 | ) | |
2a491182 | 41 | |
97922ecf C |
42 | if (targetUrls.length === 0) { |
43 | if (channelSync) { | |
44 | channelSync.state = VideoChannelSyncState.SYNCED | |
45 | await channelSync.save() | |
2a491182 | 46 | } |
2a491182 | 47 | |
97922ecf C |
48 | return |
49 | } | |
50 | ||
51 | const children: CreateJobArgument[] = [] | |
52 | ||
53 | for (const targetUrl of targetUrls) { | |
54 | if (await skipImport(channel, targetUrl, onlyAfter)) continue | |
2a491182 | 55 | |
97922ecf C |
56 | const { job } = await buildYoutubeDLImport({ |
57 | user, | |
58 | channel, | |
59 | targetUrl, | |
60 | channelSync, | |
61 | importDataOverride: { | |
62 | privacy: VideoPrivacy.PUBLIC | |
63 | } | |
64 | }) | |
65 | ||
66 | children.push(job) | |
2a491182 | 67 | } |
2a491182 | 68 | |
97922ecf C |
69 | // Will update the channel sync status |
70 | const parent: CreateJobArgument = { | |
71 | type: 'after-video-channel-import', | |
72 | payload: { | |
73 | channelSyncId: channelSync?.id | |
74 | } | |
75 | } | |
76 | ||
77 | await JobQueue.Instance.createJobWithChildren(parent, children) | |
78 | } catch (err) { | |
79 | logger.error(`Failed to import channel ${channel.name}`, { err }) | |
80 | channelSync.state = VideoChannelSyncState.FAILED | |
81 | await channelSync.save() | |
82 | } | |
2a491182 | 83 | } |
e9fc9e03 C |
84 | |
85 | // --------------------------------------------------------------------------- | |
86 | ||
87 | async function skipImport (channel: MChannel, targetUrl: string, onlyAfter?: Date) { | |
88 | if (await VideoImportModel.urlAlreadyImported(channel.id, targetUrl)) { | |
9a3a23a8 | 89 | logger.debug('%s is already imported for channel %s, skipping video channel synchronization.', targetUrl, channel.name) |
e9fc9e03 C |
90 | return true |
91 | } | |
92 | ||
93 | if (onlyAfter) { | |
94 | const youtubeDL = new YoutubeDLWrapper( | |
95 | targetUrl, | |
96 | ServerConfigManager.Instance.getEnabledResolutions('vod'), | |
97 | CONFIG.TRANSCODING.ALWAYS_TRANSCODE_ORIGINAL_RESOLUTION | |
98 | ) | |
99 | ||
100 | const videoInfo = await youtubeDL.getInfoForDownload() | |
101 | ||
3204f4d1 | 102 | const onlyAfterWithoutTime = new Date(onlyAfter) |
9a3a23a8 | 103 | onlyAfterWithoutTime.setHours(0, 0, 0, 0) |
3204f4d1 | 104 | |
9a3a23a8 | 105 | if (videoInfo.originallyPublishedAtWithoutTime.getTime() < onlyAfterWithoutTime.getTime()) { |
e9fc9e03 C |
106 | return true |
107 | } | |
108 | } | |
109 | ||
110 | return false | |
111 | } |