Refractor published date on video import
authorChocobozzz <me@florianbigard.com>
Tue, 12 Feb 2019 10:47:23 +0000 (11:47 +0100)
committerChocobozzz <me@florianbigard.com>
Tue, 12 Feb 2019 10:47:23 +0000 (11:47 +0100)
server/helpers/youtube-dl.ts
server/middlewares/validators/search.ts
server/tests/api/videos/video-imports.ts
server/tools/peertube-import-videos.ts
shared/utils/videos/videos.ts

index a5ab92df0b865240bb3f101b5e698c1f9e7aef14..782dd2e2e194dde892a4a73ec21493a4cc5eeb05 100644 (file)
@@ -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)
   }
 }
 
index 6a95d60958feb4e2a9bd9d05ef2885a4f2c647a2..7816d229c65aa9e6170a56ffc869bf8ca48faa0f 100644 (file)
@@ -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'),
 
index cd4988553db79e2294410c40b33a24aea7d3f49b..b04c0b82689ce6624ecf851644f2997423c7a376 100644 (file)
@@ -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
index 4032c5e0d11dba493d23e997786745c8dac28e40..04e24e81847bbc60ce67a57e6a5e5367ee05f4d4 100644 (file)
@@ -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)
index 92dadfb6904ea8fb7912e2b984f7073f140fc0fd..5d43d9061f993edb115cd751f76ae99d0e2b5ec7 100644 (file)
@@ -42,7 +42,6 @@ type VideoAttributes = {
     updateAt: string
     privacy?: VideoPrivacy
   }
-  originallyPublishedAt?: string
 }
 
 function getVideoCategories (url: string) {