aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/youtube-dl/youtube-dl-info-builder.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-07-31 14:34:36 +0200
committerChocobozzz <me@florianbigard.com>2023-08-11 15:02:33 +0200
commit3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch)
treee4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/helpers/youtube-dl/youtube-dl-info-builder.ts
parent04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff)
downloadPeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge conflicts, but it's a major step forward: * Server can be faster at startup because imports() are async and we can easily lazy import big modules * Angular doesn't seem to support ES import (with .js extension), so we had to correctly organize peertube into a monorepo: * Use yarn workspace feature * Use typescript reference projects for dependencies * Shared projects have been moved into "packages", each one is now a node module (with a dedicated package.json/tsconfig.json) * server/tools have been moved into apps/ and is now a dedicated app bundled and published on NPM so users don't have to build peertube cli tools manually * server/tests have been moved into packages/ so we don't compile them every time we want to run the server * Use isolatedModule option: * Had to move from const enum to const (https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums) * Had to explictely specify "type" imports when used in decorators * Prefer tsx (that uses esbuild under the hood) instead of ts-node to load typescript files (tests with mocha or scripts): * To reduce test complexity as esbuild doesn't support decorator metadata, we only test server files that do not import server models * We still build tests files into js files for a faster CI * Remove unmaintained peertube CLI import script * Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'server/helpers/youtube-dl/youtube-dl-info-builder.ts')
-rw-r--r--server/helpers/youtube-dl/youtube-dl-info-builder.ts205
1 files changed, 0 insertions, 205 deletions
diff --git a/server/helpers/youtube-dl/youtube-dl-info-builder.ts b/server/helpers/youtube-dl/youtube-dl-info-builder.ts
deleted file mode 100644
index a74904e43..000000000
--- a/server/helpers/youtube-dl/youtube-dl-info-builder.ts
+++ /dev/null
@@ -1,205 +0,0 @@
1import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES } from '../../initializers/constants'
2import { peertubeTruncate } from '../core-utils'
3import { isUrlValid } from '../custom-validators/activitypub/misc'
4
5type 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 originallyPublishedAtWithoutTime?: Date
16 webpageUrl?: string
17
18 urls?: string[]
19}
20
21class 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,
83 urls: this.buildAvailableUrl(obj),
84 originallyPublishedAtWithoutTime: this.buildOriginallyPublishedAt(obj),
85 ext: obj.ext,
86 webpageUrl: obj.webpage_url
87 }
88 }
89
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
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
202export {
203 YoutubeDLInfo,
204 YoutubeDLInfoBuilder
205}