]>
Commit | Line | Data |
---|---|---|
62549e6c C |
1 | import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES } from '../../initializers/constants' |
2 | import { peertubeTruncate } from '../core-utils' | |
474542d7 | 3 | import { isUrlValid } from '../custom-validators/activitypub/misc' |
62549e6c C |
4 | |
5 | type YoutubeDLInfo = { | |
6 | name?: string | |
7 | description?: string | |
8 | category?: number | |
9 | language?: string | |
10 | licence?: number | |
11 | nsfw?: boolean | |
12 | tags?: string[] | |
13 | thumbnailUrl?: string | |
14 | ext?: string | |
15 | originallyPublishedAt?: Date | |
474542d7 C |
16 | |
17 | urls?: string[] | |
62549e6c C |
18 | } |
19 | ||
20 | class YoutubeDLInfoBuilder { | |
21 | private readonly info: any | |
22 | ||
23 | constructor (info: any) { | |
24 | this.info = { ...info } | |
25 | } | |
26 | ||
27 | getInfo () { | |
28 | const obj = this.buildVideoInfo(this.normalizeObject(this.info)) | |
29 | if (obj.name && obj.name.length < CONSTRAINTS_FIELDS.VIDEOS.NAME.min) obj.name += ' video' | |
30 | ||
31 | return obj | |
32 | } | |
33 | ||
34 | private normalizeObject (obj: any) { | |
35 | const newObj: any = {} | |
36 | ||
37 | for (const key of Object.keys(obj)) { | |
38 | // Deprecated key | |
39 | if (key === 'resolution') continue | |
40 | ||
41 | const value = obj[key] | |
42 | ||
43 | if (typeof value === 'string') { | |
44 | newObj[key] = value.normalize() | |
45 | } else { | |
46 | newObj[key] = value | |
47 | } | |
48 | } | |
49 | ||
50 | return newObj | |
51 | } | |
52 | ||
53 | private buildOriginallyPublishedAt (obj: any) { | |
54 | let originallyPublishedAt: Date = null | |
55 | ||
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) | |
60 | ||
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) | |
65 | ||
66 | originallyPublishedAt.setFullYear(year, month, day) | |
67 | } | |
68 | ||
69 | return originallyPublishedAt | |
70 | } | |
71 | ||
72 | private buildVideoInfo (obj: any): YoutubeDLInfo { | |
73 | return { | |
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, | |
474542d7 | 82 | urls: this.buildAvailableUrl(obj), |
62549e6c C |
83 | originallyPublishedAt: this.buildOriginallyPublishedAt(obj), |
84 | ext: obj.ext | |
85 | } | |
86 | } | |
87 | ||
474542d7 C |
88 | private buildAvailableUrl (obj: any) { |
89 | const urls: string[] = [] | |
90 | ||
91 | if (obj.url) urls.push(obj.url) | |
92 | if (obj.urls) { | |
93 | if (Array.isArray(obj.urls)) urls.push(...obj.urls) | |
94 | else urls.push(obj.urls) | |
95 | } | |
96 | ||
97 | const formats = Array.isArray(obj.formats) | |
98 | ? obj.formats | |
99 | : [] | |
100 | ||
101 | for (const format of formats) { | |
102 | if (!format.url) continue | |
103 | ||
104 | urls.push(format.url) | |
105 | } | |
106 | ||
107 | const thumbnails = Array.isArray(obj.thumbnails) | |
108 | ? obj.thumbnails | |
109 | : [] | |
110 | ||
111 | for (const thumbnail of thumbnails) { | |
112 | if (!thumbnail.url) continue | |
113 | ||
114 | urls.push(thumbnail.url) | |
115 | } | |
116 | ||
117 | if (obj.thumbnail) urls.push(obj.thumbnail) | |
118 | ||
119 | for (const subtitleKey of Object.keys(obj.subtitles || {})) { | |
120 | const subtitles = obj.subtitles[subtitleKey] | |
121 | if (!Array.isArray(subtitles)) continue | |
122 | ||
123 | for (const subtitle of subtitles) { | |
124 | if (!subtitle.url) continue | |
125 | ||
126 | urls.push(subtitle.url) | |
127 | } | |
128 | } | |
129 | ||
130 | return urls.filter(u => u && isUrlValid(u)) | |
131 | } | |
132 | ||
62549e6c C |
133 | private titleTruncation (title: string) { |
134 | return peertubeTruncate(title, { | |
135 | length: CONSTRAINTS_FIELDS.VIDEOS.NAME.max, | |
136 | separator: /,? +/, | |
137 | omission: ' […]' | |
138 | }) | |
139 | } | |
140 | ||
141 | private descriptionTruncation (description: string) { | |
142 | if (!description || description.length < CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.min) return undefined | |
143 | ||
144 | return peertubeTruncate(description, { | |
145 | length: CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max, | |
146 | separator: /,? +/, | |
147 | omission: ' […]' | |
148 | }) | |
149 | } | |
150 | ||
151 | private isNSFW (info: any) { | |
152 | return info?.age_limit >= 16 | |
153 | } | |
154 | ||
155 | private getTags (tags: string[]) { | |
156 | if (Array.isArray(tags) === false) return [] | |
157 | ||
158 | return tags | |
159 | .filter(t => t.length < CONSTRAINTS_FIELDS.VIDEOS.TAG.max && t.length > CONSTRAINTS_FIELDS.VIDEOS.TAG.min) | |
160 | .map(t => t.normalize()) | |
161 | .slice(0, 5) | |
162 | } | |
163 | ||
164 | private getLicence (licence: string) { | |
165 | if (!licence) return undefined | |
166 | ||
167 | if (licence.includes('Creative Commons Attribution')) return 1 | |
168 | ||
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) | |
172 | } | |
173 | ||
174 | return undefined | |
175 | } | |
176 | ||
177 | private getCategory (categories: string[]) { | |
178 | if (!categories) return undefined | |
179 | ||
180 | const categoryString = categories[0] | |
181 | if (!categoryString || typeof categoryString !== 'string') return undefined | |
182 | ||
183 | if (categoryString === 'News & Politics') return 11 | |
184 | ||
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) | |
188 | } | |
189 | ||
190 | return undefined | |
191 | } | |
192 | ||
193 | private getLanguage (language: string) { | |
194 | return VIDEO_LANGUAGES[language] ? language : undefined | |
195 | } | |
196 | } | |
197 | ||
198 | // --------------------------------------------------------------------------- | |
199 | ||
200 | export { | |
201 | YoutubeDLInfo, | |
202 | YoutubeDLInfoBuilder | |
203 | } |