1 import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES } from '../../initializers/constants'
2 import { peertubeTruncate } from '../core-utils'
3 import { isUrlValid } from '../custom-validators/activitypub/misc'
15 originallyPublishedAt?: Date
20 class YoutubeDLInfoBuilder {
21 private readonly info: any
23 constructor (info: any) {
24 this.info = { ...info }
28 const obj = this.buildVideoInfo(this.normalizeObject(this.info))
29 if (obj.name && obj.name.length < CONSTRAINTS_FIELDS.VIDEOS.NAME.min) obj.name += ' video'
34 private normalizeObject (obj: any) {
35 const newObj: any = {}
37 for (const key of Object.keys(obj)) {
39 if (key === 'resolution') continue
41 const value = obj[key]
43 if (typeof value === 'string') {
44 newObj[key] = value.normalize()
53 private buildOriginallyPublishedAt (obj: any) {
54 let originallyPublishedAt: Date = null
56 const uploadDateMatcher = /^(\d{4})(\d{2})(\d{2})$/.exec(obj.upload_date)
57 if (uploadDateMatcher) {
58 originallyPublishedAt = new Date()
59 originallyPublishedAt.setHours(0, 0, 0, 0)
61 const year = parseInt(uploadDateMatcher[1], 10)
62 // Month starts from 0
63 const month = parseInt(uploadDateMatcher[2], 10) - 1
64 const day = parseInt(uploadDateMatcher[3], 10)
66 originallyPublishedAt.setFullYear(year, month, day)
69 return originallyPublishedAt
72 private buildVideoInfo (obj: any): YoutubeDLInfo {
74 name: this.titleTruncation(obj.title),
75 description: this.descriptionTruncation(obj.description),
76 category: this.getCategory(obj.categories),
77 licence: this.getLicence(obj.license),
78 language: this.getLanguage(obj.language),
79 nsfw: this.isNSFW(obj),
80 tags: this.getTags(obj.tags),
81 thumbnailUrl: obj.thumbnail || undefined,
82 urls: this.buildAvailableUrl(obj),
83 originallyPublishedAt: this.buildOriginallyPublishedAt(obj),
88 private buildAvailableUrl (obj: any) {
89 const urls: string[] = []
91 if (obj.url) urls.push(obj.url)
93 if (Array.isArray(obj.urls)) urls.push(...obj.urls)
94 else urls.push(obj.urls)
97 const formats = Array.isArray(obj.formats)
101 for (const format of formats) {
102 if (!format.url) continue
104 urls.push(format.url)
107 const thumbnails = Array.isArray(obj.thumbnails)
111 for (const thumbnail of thumbnails) {
112 if (!thumbnail.url) continue
114 urls.push(thumbnail.url)
117 if (obj.thumbnail) urls.push(obj.thumbnail)
119 for (const subtitleKey of Object.keys(obj.subtitles || {})) {
120 const subtitles = obj.subtitles[subtitleKey]
121 if (!Array.isArray(subtitles)) continue
123 for (const subtitle of subtitles) {
124 if (!subtitle.url) continue
126 urls.push(subtitle.url)
130 return urls.filter(u => u && isUrlValid(u))
133 private titleTruncation (title: string) {
134 return peertubeTruncate(title, {
135 length: CONSTRAINTS_FIELDS.VIDEOS.NAME.max,
141 private descriptionTruncation (description: string) {
142 if (!description || description.length < CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.min) return undefined
144 return peertubeTruncate(description, {
145 length: CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max,
151 private isNSFW (info: any) {
152 return info?.age_limit >= 16
155 private getTags (tags: string[]) {
156 if (Array.isArray(tags) === false) return []
159 .filter(t => t.length < CONSTRAINTS_FIELDS.VIDEOS.TAG.max && t.length > CONSTRAINTS_FIELDS.VIDEOS.TAG.min)
160 .map(t => t.normalize())
164 private getLicence (licence: string) {
165 if (!licence) return undefined
167 if (licence.includes('Creative Commons Attribution')) return 1
169 for (const key of Object.keys(VIDEO_LICENCES)) {
170 const peertubeLicence = VIDEO_LICENCES[key]
171 if (peertubeLicence.toLowerCase() === licence.toLowerCase()) return parseInt(key, 10)
177 private getCategory (categories: string[]) {
178 if (!categories) return undefined
180 const categoryString = categories[0]
181 if (!categoryString || typeof categoryString !== 'string') return undefined
183 if (categoryString === 'News & Politics') return 11
185 for (const key of Object.keys(VIDEO_CATEGORIES)) {
186 const category = VIDEO_CATEGORIES[key]
187 if (categoryString.toLowerCase() === category.toLowerCase()) return parseInt(key, 10)
193 private getLanguage (language: string) {
194 return VIDEO_LANGUAGES[language] ? language : undefined
198 // ---------------------------------------------------------------------------