]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/sync-channel.ts
Channel sync (#5135)
[github/Chocobozzz/PeerTube.git] / server / lib / sync-channel.ts
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
21 const user = await UserModel.loadByChannelActorId(channel.actorId)
22 const youtubeDL = new YoutubeDLWrapper(
23 externalChannelUrl,
24 ServerConfigManager.Instance.getEnabledResolutions('vod'),
25 CONFIG.TRANSCODING.ALWAYS_TRANSCODE_ORIGINAL_RESOLUTION
26 )
27
28 const infoList = await youtubeDL.getInfoForListImport({ latestVideosCount: videosCountLimit })
29
30 const targetUrls = infoList
31 .filter(videoInfo => {
32 if (!onlyAfter) return true
33
34 return videoInfo.originallyPublishedAt.getTime() >= onlyAfter.getTime()
35 })
36 .map(videoInfo => videoInfo.webpageUrl)
37
38 logger.info(
39 'Fetched %d candidate URLs for sync channel %s.',
40 targetUrls.length, channel.Actor.preferredUsername, { targetUrls }
41 )
42
43 if (targetUrls.length === 0) {
44 if (channelSync) {
45 channelSync.state = VideoChannelSyncState.SYNCED
46 await channelSync.save()
47 }
48
49 return
50 }
51
52 const children: CreateJobArgument[] = []
53
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)
57 continue
58 }
59
60 const { job } = await buildYoutubeDLImport({
61 user,
62 channel,
63 targetUrl,
64 channelSync,
65 importDataOverride: {
66 privacy: VideoPrivacy.PUBLIC
67 }
68 })
69
70 children.push(job)
71 }
72
73 const parent: CreateJobArgument = {
74 type: 'after-video-channel-import',
75 payload: {
76 channelSyncId: channelSync?.id
77 }
78 }
79
80 await JobQueue.Instance.createJobWithChildren(parent, children)
81 }