From c74c9be934fa1584edf6f4f7a41f00c4d3f2a8b3 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 12 Feb 2019 11:47:23 +0100 Subject: [PATCH] Refractor published date on video import --- server/helpers/youtube-dl.ts | 29 +++++++++++++++++++----- server/middlewares/validators/search.ts | 3 +++ server/tests/api/videos/video-imports.ts | 1 + server/tools/peertube-import-videos.ts | 6 ++--- shared/utils/videos/videos.ts | 1 - 5 files changed, 30 insertions(+), 10 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 = { nsfw?: boolean tags?: string[] thumbnailUrl?: string - originallyPublishedAt?: string + originallyPublishedAt?: Date } const processOptions = { @@ -143,13 +143,33 @@ async function safeGetYoutubeDL () { return youtubeDL } +function buildOriginallyPublishedAt (obj: any) { + let originallyPublishedAt: Date = null + + const uploadDateMatcher = /^(\d{4})(\d{2})(\d{2})$/.exec(obj.upload_date) + if (uploadDateMatcher) { + originallyPublishedAt = new Date() + originallyPublishedAt.setHours(0, 0, 0, 0) + + const year = parseInt(uploadDateMatcher[1], 10) + // Month starts from 0 + const month = parseInt(uploadDateMatcher[2], 10) - 1 + const day = parseInt(uploadDateMatcher[3], 10) + + originallyPublishedAt.setFullYear(year, month, day) + } + + return originallyPublishedAt +} + // --------------------------------------------------------------------------- export { updateYoutubeDLBinary, downloadYoutubeDLVideo, getYoutubeDLInfo, - safeGetYoutubeDL + safeGetYoutubeDL, + buildOriginallyPublishedAt } // --------------------------------------------------------------------------- @@ -174,9 +194,6 @@ function normalizeObject (obj: any) { } function buildVideoInfo (obj: any) { - - const date = obj.upload_date.slice(0,4) + ',' + obj.upload_date.slice(4,6) + ',' + obj.upload_date.slice(6,8) - return { name: titleTruncation(obj.title), description: descriptionTruncation(obj.description), @@ -185,7 +202,7 @@ function buildVideoInfo (obj: any) { nsfw: isNSFW(obj), tags: getTags(obj.tags), thumbnailUrl: obj.thumbnail || undefined, - originallyPublishedAt: new Date(date).toISOString() + originallyPublishedAt: buildOriginallyPublishedAt(obj) } } 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 = [ query('startDate').optional().custom(isDateValid).withMessage('Should have a valid start date'), query('endDate').optional().custom(isDateValid).withMessage('Should have a valid end date'), + query('originallyPublishedStartDate').optional().custom(isDateValid).withMessage('Should have a valid published start date'), + query('originallyPublishedEndDate').optional().custom(isDateValid).withMessage('Should have a valid published end date'), + query('durationMin').optional().isInt().withMessage('Should have a valid min duration'), query('durationMax').optional().isInt().withMessage('Should have a valid max duration'), 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 () { expect(videoHttp.description).to.equal('this is a super description') expect(videoHttp.tags).to.deep.equal([ 'tag1', 'tag2' ]) expect(videoHttp.files).to.have.lengthOf(1) + expect(videoHttp.originallyPublishedAt).to.equal('2019-01-13T23:00:00.000Z') const resMagnet = await getVideo(url, idMagnet) 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' import * as prompt from 'prompt' import { remove } from 'fs-extra' import { sha256 } from '../helpers/core-utils' -import { safeGetYoutubeDL } from '../helpers/youtube-dl' +import { safeGetYoutubeDL, buildOriginallyPublishedAt } from '../helpers/youtube-dl' import { getSettings, netrc } from './cli' let accessToken: string @@ -212,7 +212,7 @@ async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, cwd: st }, thumbnailfile) } - const date = videoInfo.upload_date.slice(0,4) + ',' + videoInfo.upload_date.slice(4,6) + ',' + videoInfo.upload_date.slice(6,8) + const originallyPublishedAt = buildOriginallyPublishedAt(videoInfo) const videoAttributes = { name: truncate(videoInfo.title, { @@ -234,7 +234,7 @@ async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, cwd: st fixture: videoPath, thumbnailfile, previewfile: thumbnailfile, - originallyPublishedAt: new Date(date).toISOString() + originallyPublishedAt: originallyPublishedAt ? originallyPublishedAt.toISOString() : null } console.log('\nUploading on PeerTube video "%s".', videoAttributes.name) diff --git a/shared/utils/videos/videos.ts b/shared/utils/videos/videos.ts index 92dadfb69..5d43d9061 100644 --- a/shared/utils/videos/videos.ts +++ b/shared/utils/videos/videos.ts @@ -42,7 +42,6 @@ type VideoAttributes = { updateAt: string privacy?: VideoPrivacy } - originallyPublishedAt?: string } function getVideoCategories (url: string) { -- 2.25.1