1 import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES } from '../../initializers/constants'
2 import { peertubeTruncate } from '../core-utils'
14 originallyPublishedAt?: Date
17 class YoutubeDLInfoBuilder {
18 private readonly info: any
20 constructor (info: any) {
21 this.info = { ...info }
25 const obj = this.buildVideoInfo(this.normalizeObject(this.info))
26 if (obj.name && obj.name.length < CONSTRAINTS_FIELDS.VIDEOS.NAME.min) obj.name += ' video'
31 private normalizeObject (obj: any) {
32 const newObj: any = {}
34 for (const key of Object.keys(obj)) {
36 if (key === 'resolution') continue
38 const value = obj[key]
40 if (typeof value === 'string') {
41 newObj[key] = value.normalize()
50 private buildOriginallyPublishedAt (obj: any) {
51 let originallyPublishedAt: Date = null
53 const uploadDateMatcher = /^(\d{4})(\d{2})(\d{2})$/.exec(obj.upload_date)
54 if (uploadDateMatcher) {
55 originallyPublishedAt = new Date()
56 originallyPublishedAt.setHours(0, 0, 0, 0)
58 const year = parseInt(uploadDateMatcher[1], 10)
59 // Month starts from 0
60 const month = parseInt(uploadDateMatcher[2], 10) - 1
61 const day = parseInt(uploadDateMatcher[3], 10)
63 originallyPublishedAt.setFullYear(year, month, day)
66 return originallyPublishedAt
69 private buildVideoInfo (obj: any): YoutubeDLInfo {
71 name: this.titleTruncation(obj.title),
72 description: this.descriptionTruncation(obj.description),
73 category: this.getCategory(obj.categories),
74 licence: this.getLicence(obj.license),
75 language: this.getLanguage(obj.language),
76 nsfw: this.isNSFW(obj),
77 tags: this.getTags(obj.tags),
78 thumbnailUrl: obj.thumbnail || undefined,
79 originallyPublishedAt: this.buildOriginallyPublishedAt(obj),
84 private titleTruncation (title: string) {
85 return peertubeTruncate(title, {
86 length: CONSTRAINTS_FIELDS.VIDEOS.NAME.max,
92 private descriptionTruncation (description: string) {
93 if (!description || description.length < CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.min) return undefined
95 return peertubeTruncate(description, {
96 length: CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max,
102 private isNSFW (info: any) {
103 return info?.age_limit >= 16
106 private getTags (tags: string[]) {
107 if (Array.isArray(tags) === false) return []
110 .filter(t => t.length < CONSTRAINTS_FIELDS.VIDEOS.TAG.max && t.length > CONSTRAINTS_FIELDS.VIDEOS.TAG.min)
111 .map(t => t.normalize())
115 private getLicence (licence: string) {
116 if (!licence) return undefined
118 if (licence.includes('Creative Commons Attribution')) return 1
120 for (const key of Object.keys(VIDEO_LICENCES)) {
121 const peertubeLicence = VIDEO_LICENCES[key]
122 if (peertubeLicence.toLowerCase() === licence.toLowerCase()) return parseInt(key, 10)
128 private getCategory (categories: string[]) {
129 if (!categories) return undefined
131 const categoryString = categories[0]
132 if (!categoryString || typeof categoryString !== 'string') return undefined
134 if (categoryString === 'News & Politics') return 11
136 for (const key of Object.keys(VIDEO_CATEGORIES)) {
137 const category = VIDEO_CATEGORIES[key]
138 if (categoryString.toLowerCase() === category.toLowerCase()) return parseInt(key, 10)
144 private getLanguage (language: string) {
145 return VIDEO_LANGUAGES[language] ? language : undefined
149 // ---------------------------------------------------------------------------