]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tools/peertube-import-videos.ts
Try to fix video duplication
[github/Chocobozzz/PeerTube.git] / server / tools / peertube-import-videos.ts
index d7bb00e02e7d8955a7d89b9a4a7a53ff93e20be6..0ebfa744263d19b807eb6427432d85c395a66309 100644 (file)
@@ -11,7 +11,7 @@ import * as prompt from 'prompt'
 import { remove } from 'fs-extra'
 import { sha256 } from '../helpers/core-utils'
 import { buildOriginallyPublishedAt, safeGetYoutubeDL } from '../helpers/youtube-dl'
-import { buildCommonVideoOptions, buildVideoAttributesFromCommander, getNetrc, getRemoteObjectOrDie, getSettings } from './cli'
+import { buildCommonVideoOptions, buildVideoAttributesFromCommander, getServerCredentials } from './cli'
 
 type UserInfo = {
   username: string
@@ -32,31 +32,31 @@ command
   .option('-u, --url <url>', 'Server url')
   .option('-U, --username <username>', 'Username')
   .option('-p, --password <token>', 'Password')
-  .option('-t, --target-url <targetUrl>', 'Video target URL')
+  .option('--target-url <targetUrl>', 'Video target URL')
+  .option('--since <since>', 'Publication date (inclusive) since which the videos can be imported (YYYY-MM-DD)', parseDate)
+  .option('--until <until>', 'Publication date (inclusive) until which the videos can be imported (YYYY-MM-DD)', parseDate)
   .option('-v, --verbose', 'Verbose mode')
   .parse(process.argv)
 
-Promise.all([ getSettings(), getNetrc() ])
-       .then(([ settings, netrc ]) => {
-         const { url, username, password } = getRemoteObjectOrDie(program, settings, netrc)
+getServerCredentials(command)
+  .then(({ url, username, password }) => {
+    if (!program[ 'targetUrl' ]) {
+      console.error('--targetUrl field is required.')
 
-         if (!program[ 'targetUrl' ]) {
-           console.error('--targetUrl field is required.')
-
-           process.exit(-1)
-         }
+      process.exit(-1)
+    }
 
-         removeEndSlashes(url)
-         removeEndSlashes(program[ 'targetUrl' ])
+    removeEndSlashes(url)
+    removeEndSlashes(program[ 'targetUrl' ])
 
-         const user = { username, password }
+    const user = { username, password }
 
-         run(url, user)
-           .catch(err => {
-             console.error(err)
-             process.exit(-1)
-           })
-       })
+    run(url, user)
+      .catch(err => {
+        console.error(err)
+        process.exit(-1)
+      })
+  })
 
 async function run (url: string, user: UserInfo) {
   if (!user.password) {
@@ -110,6 +110,21 @@ function processVideo (parameters: {
     const videoInfo = await fetchObject(youtubeInfo)
     if (program[ 'verbose' ]) console.log('Fetched object.', videoInfo)
 
+    if (program[ 'since' ]) {
+      if (buildOriginallyPublishedAt(videoInfo).getTime() < program[ 'since' ].getTime()) {
+        console.log('Video "%s" has been published before "%s", don\'t upload it.\n',
+          videoInfo.title, formatDate(program[ 'since' ]));
+        return res();
+      }
+    }
+    if (program[ 'until' ]) {
+      if (buildOriginallyPublishedAt(videoInfo).getTime() > program[ 'until' ].getTime()) {
+        console.log('Video "%s" has been published after "%s", don\'t upload it.\n',
+          videoInfo.title, formatDate(program[ 'until' ]));
+        return res();
+      }
+    }
+
     const result = await searchVideoWithSort(url, videoInfo.title, '-match')
 
     console.log('############################################################\n')
@@ -344,3 +359,20 @@ async function getAccessTokenOrDie (url: string, user: UserInfo) {
     process.exit(-1)
   }
 }
+
+function parseDate (dateAsStr: string): Date {
+  if (!/\d{4}-\d{2}-\d{2}/.test(dateAsStr)) {
+    console.error(`Invalid date passed: ${dateAsStr}. Expected format: YYYY-MM-DD. See help for usage.`);
+    process.exit(-1);
+  }
+  const date = new Date(dateAsStr);
+  if (isNaN(date.getTime())) {
+    console.error(`Invalid date passed: ${dateAsStr}. See help for usage.`);
+    process.exit(-1);
+  }
+  return date;
+}
+
+function formatDate (date: Date): string {
+  return date.toISOString().split('T')[0];
+}