From c74c9be934fa1584edf6f4f7a41f00c4d3f2a8b3 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 12 Feb 2019 11:47:23 +0100 Subject: Refractor published date on video import --- server/helpers/youtube-dl.ts | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) (limited to 'server/helpers') 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) } } -- cgit v1.2.3