Skip to content

Commit

Permalink
Refractor published date on video import
Browse files Browse the repository at this point in the history
  • Loading branch information
Chocobozzz committed Feb 12, 2019
1 parent 8492984 commit c74c9be
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
29 changes: 23 additions & 6 deletions server/helpers/youtube-dl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type YoutubeDLInfo = {
nsfw?: boolean
tags?: string[]
thumbnailUrl?: string
originallyPublishedAt?: string
originallyPublishedAt?: Date
}

const processOptions = {
Expand Down Expand Up @@ -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
}

// ---------------------------------------------------------------------------
Expand All @@ -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),
Expand All @@ -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)
}
}

Expand Down
3 changes: 3 additions & 0 deletions server/middlewares/validators/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),

Expand Down
1 change: 1 addition & 0 deletions server/tests/api/videos/video-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions server/tools/peertube-import-videos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, {
Expand All @@ -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)
Expand Down
1 change: 0 additions & 1 deletion shared/utils/videos/videos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ type VideoAttributes = {
updateAt: string
privacy?: VideoPrivacy
}
originallyPublishedAt?: string
}

function getVideoCategories (url: string) {
Expand Down

0 comments on commit c74c9be

Please sign in to comment.