aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-02-12 11:47:23 +0100
committerChocobozzz <me@florianbigard.com>2019-02-12 11:47:23 +0100
commitc74c9be934fa1584edf6f4f7a41f00c4d3f2a8b3 (patch)
tree42370ebba95a968b45e59bbc81ae40947b541174 /server
parent84929846e743f4b5f3971b386ea2c447db8c416f (diff)
downloadPeerTube-c74c9be934fa1584edf6f4f7a41f00c4d3f2a8b3.tar.gz
PeerTube-c74c9be934fa1584edf6f4f7a41f00c4d3f2a8b3.tar.zst
PeerTube-c74c9be934fa1584edf6f4f7a41f00c4d3f2a8b3.zip
Refractor published date on video import
Diffstat (limited to 'server')
-rw-r--r--server/helpers/youtube-dl.ts29
-rw-r--r--server/middlewares/validators/search.ts3
-rw-r--r--server/tests/api/videos/video-imports.ts1
-rw-r--r--server/tools/peertube-import-videos.ts6
4 files changed, 30 insertions, 9 deletions
diff --git a/server/helpers/youtube-dl.ts b/server/helpers/youtube-dl.ts
index a5ab92df0..782dd2e2e 100644
--- a/server/helpers/youtube-dl.ts
+++ b/server/helpers/youtube-dl.ts
@@ -16,7 +16,7 @@ export type YoutubeDLInfo = {
16 nsfw?: boolean 16 nsfw?: boolean
17 tags?: string[] 17 tags?: string[]
18 thumbnailUrl?: string 18 thumbnailUrl?: string
19 originallyPublishedAt?: string 19 originallyPublishedAt?: Date
20} 20}
21 21
22const processOptions = { 22const processOptions = {
@@ -143,13 +143,33 @@ async function safeGetYoutubeDL () {
143 return youtubeDL 143 return youtubeDL
144} 144}
145 145
146function buildOriginallyPublishedAt (obj: any) {
147 let originallyPublishedAt: Date = null
148
149 const uploadDateMatcher = /^(\d{4})(\d{2})(\d{2})$/.exec(obj.upload_date)
150 if (uploadDateMatcher) {
151 originallyPublishedAt = new Date()
152 originallyPublishedAt.setHours(0, 0, 0, 0)
153
154 const year = parseInt(uploadDateMatcher[1], 10)
155 // Month starts from 0
156 const month = parseInt(uploadDateMatcher[2], 10) - 1
157 const day = parseInt(uploadDateMatcher[3], 10)
158
159 originallyPublishedAt.setFullYear(year, month, day)
160 }
161
162 return originallyPublishedAt
163}
164
146// --------------------------------------------------------------------------- 165// ---------------------------------------------------------------------------
147 166
148export { 167export {
149 updateYoutubeDLBinary, 168 updateYoutubeDLBinary,
150 downloadYoutubeDLVideo, 169 downloadYoutubeDLVideo,
151 getYoutubeDLInfo, 170 getYoutubeDLInfo,
152 safeGetYoutubeDL 171 safeGetYoutubeDL,
172 buildOriginallyPublishedAt
153} 173}
154 174
155// --------------------------------------------------------------------------- 175// ---------------------------------------------------------------------------
@@ -174,9 +194,6 @@ function normalizeObject (obj: any) {
174} 194}
175 195
176function buildVideoInfo (obj: any) { 196function buildVideoInfo (obj: any) {
177
178 const date = obj.upload_date.slice(0,4) + ',' + obj.upload_date.slice(4,6) + ',' + obj.upload_date.slice(6,8)
179
180 return { 197 return {
181 name: titleTruncation(obj.title), 198 name: titleTruncation(obj.title),
182 description: descriptionTruncation(obj.description), 199 description: descriptionTruncation(obj.description),
@@ -185,7 +202,7 @@ function buildVideoInfo (obj: any) {
185 nsfw: isNSFW(obj), 202 nsfw: isNSFW(obj),
186 tags: getTags(obj.tags), 203 tags: getTags(obj.tags),
187 thumbnailUrl: obj.thumbnail || undefined, 204 thumbnailUrl: obj.thumbnail || undefined,
188 originallyPublishedAt: new Date(date).toISOString() 205 originallyPublishedAt: buildOriginallyPublishedAt(obj)
189 } 206 }
190} 207}
191 208
diff --git a/server/middlewares/validators/search.ts b/server/middlewares/validators/search.ts
index 6a95d6095..7816d229c 100644
--- a/server/middlewares/validators/search.ts
+++ b/server/middlewares/validators/search.ts
@@ -10,6 +10,9 @@ const videosSearchValidator = [
10 query('startDate').optional().custom(isDateValid).withMessage('Should have a valid start date'), 10 query('startDate').optional().custom(isDateValid).withMessage('Should have a valid start date'),
11 query('endDate').optional().custom(isDateValid).withMessage('Should have a valid end date'), 11 query('endDate').optional().custom(isDateValid).withMessage('Should have a valid end date'),
12 12
13 query('originallyPublishedStartDate').optional().custom(isDateValid).withMessage('Should have a valid published start date'),
14 query('originallyPublishedEndDate').optional().custom(isDateValid).withMessage('Should have a valid published end date'),
15
13 query('durationMin').optional().isInt().withMessage('Should have a valid min duration'), 16 query('durationMin').optional().isInt().withMessage('Should have a valid min duration'),
14 query('durationMax').optional().isInt().withMessage('Should have a valid max duration'), 17 query('durationMax').optional().isInt().withMessage('Should have a valid max duration'),
15 18
diff --git a/server/tests/api/videos/video-imports.ts b/server/tests/api/videos/video-imports.ts
index cd4988553..b04c0b826 100644
--- a/server/tests/api/videos/video-imports.ts
+++ b/server/tests/api/videos/video-imports.ts
@@ -37,6 +37,7 @@ describe('Test video imports', function () {
37 expect(videoHttp.description).to.equal('this is a super description') 37 expect(videoHttp.description).to.equal('this is a super description')
38 expect(videoHttp.tags).to.deep.equal([ 'tag1', 'tag2' ]) 38 expect(videoHttp.tags).to.deep.equal([ 'tag1', 'tag2' ])
39 expect(videoHttp.files).to.have.lengthOf(1) 39 expect(videoHttp.files).to.have.lengthOf(1)
40 expect(videoHttp.originallyPublishedAt).to.equal('2019-01-13T23:00:00.000Z')
40 41
41 const resMagnet = await getVideo(url, idMagnet) 42 const resMagnet = await getVideo(url, idMagnet)
42 const videoMagnet: VideoDetails = resMagnet.body 43 const videoMagnet: VideoDetails = resMagnet.body
diff --git a/server/tools/peertube-import-videos.ts b/server/tools/peertube-import-videos.ts
index 4032c5e0d..04e24e818 100644
--- a/server/tools/peertube-import-videos.ts
+++ b/server/tools/peertube-import-videos.ts
@@ -11,7 +11,7 @@ import { truncate } from 'lodash'
11import * as prompt from 'prompt' 11import * as prompt from 'prompt'
12import { remove } from 'fs-extra' 12import { remove } from 'fs-extra'
13import { sha256 } from '../helpers/core-utils' 13import { sha256 } from '../helpers/core-utils'
14import { safeGetYoutubeDL } from '../helpers/youtube-dl' 14import { safeGetYoutubeDL, buildOriginallyPublishedAt } from '../helpers/youtube-dl'
15import { getSettings, netrc } from './cli' 15import { getSettings, netrc } from './cli'
16 16
17let accessToken: string 17let accessToken: string
@@ -212,7 +212,7 @@ async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, cwd: st
212 }, thumbnailfile) 212 }, thumbnailfile)
213 } 213 }
214 214
215 const date = videoInfo.upload_date.slice(0,4) + ',' + videoInfo.upload_date.slice(4,6) + ',' + videoInfo.upload_date.slice(6,8) 215 const originallyPublishedAt = buildOriginallyPublishedAt(videoInfo)
216 216
217 const videoAttributes = { 217 const videoAttributes = {
218 name: truncate(videoInfo.title, { 218 name: truncate(videoInfo.title, {
@@ -234,7 +234,7 @@ async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, cwd: st
234 fixture: videoPath, 234 fixture: videoPath,
235 thumbnailfile, 235 thumbnailfile,
236 previewfile: thumbnailfile, 236 previewfile: thumbnailfile,
237 originallyPublishedAt: new Date(date).toISOString() 237 originallyPublishedAt: originallyPublishedAt ? originallyPublishedAt.toISOString() : null
238 } 238 }
239 239
240 console.log('\nUploading on PeerTube video "%s".', videoAttributes.name) 240 console.log('\nUploading on PeerTube video "%s".', videoAttributes.name)