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