]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tools/peertube-import-videos.ts
Fix test cleanup
[github/Chocobozzz/PeerTube.git] / server / tools / peertube-import-videos.ts
index 54ac910e634c5d61218ef922537fb4cffe0af8d9..bbdaa09c0b40dce14026a85cd704bd32538be48c 100644 (file)
@@ -1,11 +1,10 @@
-import { registerTSPaths } from '../helpers/register-ts-paths'
-registerTSPaths()
-
 import { program } from 'commander'
 import { accessSync, constants } from 'fs'
 import { remove } from 'fs-extra'
 import { join } from 'path'
-import { sha256 } from '../helpers/core-utils'
+import { YoutubeDLCLI, YoutubeDLInfo, YoutubeDLInfoBuilder } from '@server/helpers/youtube-dl'
+import { wait } from '@shared/core-utils'
+import { sha256 } from '@shared/extra-utils'
 import { doRequestAndSaveToFile } from '../helpers/requests'
 import {
   assignToken,
@@ -14,9 +13,8 @@ import {
   buildVideoAttributesFromCommander,
   getLogger,
   getServerCredentials
-} from './cli'
-import { wait } from '@shared/extra-utils'
-import { YoutubeDLCLI, YoutubeDLInfo, YoutubeDLInfoBuilder } from '@server/helpers/youtube-dl'
+} from './shared'
+
 import prompt = require('prompt')
 
 const processOptions = {
@@ -39,7 +37,7 @@ command
   .option('--last <last>', 'Process last n elements of returned playlist')
   .option('--wait-interval <waitInterval>', 'Duration between two video imports (in seconds)', convertIntoMs)
   .option('-T, --tmpdir <tmpdir>', 'Working directory', __dirname)
-  .usage("[global options] [ -- youtube-dl options]")
+  .usage('[global options] [ -- youtube-dl options]')
   .parse(process.argv)
 
 const options = command.opts()
@@ -95,14 +93,15 @@ async function run (url: string, username: string, password: string) {
 
   log.info('Will download and upload %d videos.\n', infoArray.length)
 
+  let skipInterval = true
   for (const [ index, info ] of infoArray.entries()) {
     try {
-      if (index > 0 && options.waitInterval) {
-        log.info("Wait for %d seconds before continuing.", options.waitInterval / 1000)
+      if (index > 0 && options.waitInterval && !skipInterval) {
+        log.info('Wait for %d seconds before continuing.', options.waitInterval / 1000)
         await wait(options.waitInterval)
       }
 
-      await processVideo({
+      skipInterval = await processVideo({
         cwd: options.tmpdir,
         url,
         username,
@@ -132,14 +131,22 @@ async function processVideo (parameters: {
   const videoInfo = await fetchObject(youtubeInfo)
   log.debug('Fetched object.', videoInfo)
 
-  if (options.since && videoInfo.originallyPublishedAt && videoInfo.originallyPublishedAt.getTime() < options.since.getTime()) {
+  if (
+    options.since &&
+    videoInfo.originallyPublishedAtWithoutTime &&
+    videoInfo.originallyPublishedAtWithoutTime.getTime() < options.since.getTime()
+  ) {
     log.info('Video "%s" has been published before "%s", don\'t upload it.\n', videoInfo.name, formatDate(options.since))
-    return
+    return true
   }
 
-  if (options.until && videoInfo.originallyPublishedAt && videoInfo.originallyPublishedAt.getTime() > options.until.getTime()) {
+  if (
+    options.until &&
+    videoInfo.originallyPublishedAtWithoutTime &&
+    videoInfo.originallyPublishedAtWithoutTime.getTime() > options.until.getTime()
+  ) {
     log.info('Video "%s" has been published after "%s", don\'t upload it.\n', videoInfo.name, formatDate(options.until))
-    return
+    return true
   }
 
   const server = buildServer(url)
@@ -155,7 +162,7 @@ async function processVideo (parameters: {
 
   if (data.find(v => v.name === videoInfo.name)) {
     log.info('Video "%s" already exists, don\'t reupload it.\n', videoInfo.name)
-    return
+    return true
   }
 
   const path = join(cwd, sha256(videoInfo.url) + '.mp4')
@@ -166,7 +173,7 @@ async function processVideo (parameters: {
     const youtubeDLBinary = await YoutubeDLCLI.safeGet()
     const output = await youtubeDLBinary.download({
       url: videoInfo.url,
-      format: YoutubeDLCLI.getYoutubeDLVideoFormat([]),
+      format: YoutubeDLCLI.getYoutubeDLVideoFormat([], false),
       output: path,
       additionalYoutubeDLArgs: command.args,
       processOptions
@@ -184,6 +191,8 @@ async function processVideo (parameters: {
   } catch (err) {
     log.error(err.message)
   }
+
+  return false
 }
 
 async function uploadVideoOnPeerTube (parameters: {
@@ -211,8 +220,8 @@ async function uploadVideoOnPeerTube (parameters: {
   const attributes = {
     ...baseAttributes,
 
-    originallyPublishedAt: videoInfo.originallyPublishedAt
-      ? videoInfo.originallyPublishedAt.toISOString()
+    originallyPublishedAtWithoutTime: videoInfo.originallyPublishedAtWithoutTime
+      ? videoInfo.originallyPublishedAtWithoutTime.toISOString()
       : null,
 
     thumbnailfile,
@@ -250,7 +259,7 @@ async function fetchObject (info: any) {
   const youtubeDLCLI = await YoutubeDLCLI.safeGet()
   const result = await youtubeDLCLI.getInfo({
     url,
-    format: YoutubeDLCLI.getYoutubeDLVideoFormat([]),
+    format: YoutubeDLCLI.getYoutubeDLVideoFormat([], false),
     processOptions
   })
 
@@ -335,7 +344,7 @@ function exitError (message: string, ...meta: any[]) {
 function getYoutubeDLInfo (youtubeDLCLI: YoutubeDLCLI, url: string, args: string[]) {
   return youtubeDLCLI.getInfo({
     url,
-    format: YoutubeDLCLI.getYoutubeDLVideoFormat([]),
+    format: YoutubeDLCLI.getYoutubeDLVideoFormat([], false),
     additionalYoutubeDLArgs: [ '-j', '--flat-playlist', '--playlist-reverse', ...args ],
     processOptions
   })