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 originallyPublishedAtWithoutTime?: Date
21 class YoutubeDLInfoBuilder {
22 private readonly info: any
24 constructor (info: any) {
25 this.info = { ...info }
29 const obj = this.buildVideoInfo(this.normalizeObject(this.info))
30 if (obj.name && obj.name.length < CONSTRAINTS_FIELDS.VIDEOS.NAME.min) obj.name += ' video'
35 private normalizeObject (obj: any) {
36 const newObj: any = {}
38 for (const key of Object.keys(obj)) {
40 if (key === 'resolution') continue
42 const value = obj[key]
44 if (typeof value === 'string') {
45 newObj[key] = value.normalize()
54 private buildOriginallyPublishedAt (obj: any) {
55 let originallyPublishedAt: Date = null
57 const uploadDateMatcher = /^(\d{4})(\d{2})(\d{2})$/.exec(obj.upload_date)
58 if (uploadDateMatcher) {
59 originallyPublishedAt = new Date()
60 originallyPublishedAt.setHours(0, 0, 0, 0)
62 const year = parseInt(uploadDateMatcher[1], 10)
63 // Month starts from 0
64 const month = parseInt(uploadDateMatcher[2], 10) - 1
65 const day = parseInt(uploadDateMatcher[3], 10)
67 originallyPublishedAt.setFullYear(year, month, day)
70 return originallyPublishedAt
73 private buildVideoInfo (obj: any): YoutubeDLInfo {
75 name: this.titleTruncation(obj.title),
76 description: this.descriptionTruncation(obj.description),
77 category: this.getCategory(obj.categories),
78 licence: this.getLicence(obj.license),
79 language: this.getLanguage(obj.language),
80 nsfw: this.isNSFW(obj),
81 tags: this.getTags(obj.tags),
82 thumbnailUrl: obj.thumbnail || undefined,
83 urls: this.buildAvailableUrl(obj),
84 originallyPublishedAtWithoutTime: this.buildOriginallyPublishedAt(obj),
86 webpageUrl: obj.webpage_url
90 private buildAvailableUrl (obj: any) {
91 const urls: string[] = []
93 if (obj.url) urls.push(obj.url)
95 if (Array.isArray(obj.urls)) urls.push(...obj.urls)
96 else urls.push(obj.urls)
99 const formats = Array.isArray(obj.formats)
103 for (const format of formats) {
104 if (!format.url) continue
106 urls.push(format.url)
109 const thumbnails = Array.isArray(obj.thumbnails)
113 for (const thumbnail of thumbnails) {
114 if (!thumbnail.url) continue
116 urls.push(thumbnail.url)
119 if (obj.thumbnail) urls.push(obj.thumbnail)
121 for (const subtitleKey of Object.keys(obj.subtitles || {})) {
122 const subtitles = obj.subtitles[subtitleKey]
123 if (!Array.isArray(subtitles)) continue
125 for (const subtitle of subtitles) {
126 if (!subtitle.url) continue
128 urls.push(subtitle.url)
132 return urls.filter(u => u && isUrlValid(u))
135 private titleTruncation (title: string) {
136 return peertubeTruncate(title, {
137 length: CONSTRAINTS_FIELDS.VIDEOS.NAME.max,
143 private descriptionTruncation (description: string) {
144 if (!description || description.length < CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.min) return undefined
146 return peertubeTruncate(description, {
147 length: CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max,
153 private isNSFW (info: any) {
154 return info?.age_limit >= 16
157 private getTags (tags: string[]) {
158 if (Array.isArray(tags) === false) return []
161 .filter(t => t.length < CONSTRAINTS_FIELDS.VIDEOS.TAG.max && t.length > CONSTRAINTS_FIELDS.VIDEOS.TAG.min)
162 .map(t => t.normalize())
166 private getLicence (licence: string) {
167 if (!licence) return undefined
169 if (licence.includes('Creative Commons Attribution')) return 1
171 for (const key of Object.keys(VIDEO_LICENCES)) {
172 const peertubeLicence = VIDEO_LICENCES[key]
173 if (peertubeLicence.toLowerCase() === licence.toLowerCase()) return parseInt(key, 10)
179 private getCategory (categories: string[]) {
180 if (!categories) return undefined
182 const categoryString = categories[0]
183 if (!categoryString || typeof categoryString !== 'string') return undefined
185 if (categoryString === 'News & Politics') return 11
187 for (const key of Object.keys(VIDEO_CATEGORIES)) {
188 const category = VIDEO_CATEGORIES[key]
189 if (categoryString.toLowerCase() === category.toLowerCase()) return parseInt(key, 10)
195 private getLanguage (language: string) {
196 return VIDEO_LANGUAGES[language] ? language : undefined
200 // ---------------------------------------------------------------------------