Begin import script with youtube-dl
authorChocobozzz <me@florianbigard.com>
Fri, 9 Feb 2018 15:47:06 +0000 (16:47 +0100)
committerChocobozzz <me@florianbigard.com>
Fri, 9 Feb 2018 15:47:06 +0000 (16:47 +0100)
package.json
server/tests/real-world/tools/get-access-token.ts [deleted file]
server/tests/real-world/tools/upload-directory.ts [deleted file]
server/tests/real-world/tools/upload.ts [deleted file]
server/tests/utils/videos/videos.ts
server/tools/get-access-token.ts [new file with mode: 0644]
server/tools/import-youtube.ts [new file with mode: 0644]
server/tools/upload-directory.ts [new file with mode: 0644]
server/tools/upload.ts [new file with mode: 0644]
yarn.lock

index 948ba3f6712c5689a3a381f4fccb3f98186f83d8..7ed520b65430bb18e03855645f3acdd8fbad095f 100644 (file)
     "supertest": "^3.0.0",
     "tslint": "^5.7.0",
     "tslint-config-standard": "^7.0.0",
-    "webtorrent": "^0.98.0"
+    "webtorrent": "^0.98.0",
+    "youtube-dl": "^1.12.2"
   },
   "scripty": {
     "silent": true
diff --git a/server/tests/real-world/tools/get-access-token.ts b/server/tests/real-world/tools/get-access-token.ts
deleted file mode 100644 (file)
index ee14733..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-import * as program from 'commander'
-
-import {
-  getClient,
-  serverLogin
-} from '../../utils'
-
-program
-  .option('-u, --url <url>', 'Server url')
-  .option('-n, --username <username>', 'Username')
-  .option('-p, --password <token>', 'Password')
-  .parse(process.argv)
-
-if (
-  !program['url'] ||
-  !program['username'] ||
-  !program['password']
-) {
-  throw new Error('All arguments are required.')
-}
-
-const server = {
-  url: program['url'],
-  user: {
-    username: program['username'],
-    password: program['password']
-  },
-  client: {
-    id: null,
-    secret: null
-  }
-}
-
-getClient(program.url)
-  .then(res => {
-    server.client.id = res.body.client_id
-    server.client.secret = res.body.client_secret
-
-    return serverLogin(server)
-  })
-  .then(accessToken => {
-    console.log(accessToken)
-    process.exit(0)
-  })
-  .catch(err => {
-    console.error(err)
-    process.exit(-1)
-  })
diff --git a/server/tests/real-world/tools/upload-directory.ts b/server/tests/real-world/tools/upload-directory.ts
deleted file mode 100644 (file)
index fdd5685..0000000
+++ /dev/null
@@ -1,82 +0,0 @@
-import * as program from 'commander'
-import * as Promise from 'bluebird'
-import { isAbsolute, join } from 'path'
-
-import { readdirPromise } from '../../../helpers/core-utils'
-import { execCLI } from '../../utils'
-
-program
-  .option('-u, --url <url>', 'Server url')
-  .option('-U, --username <username>', 'Username')
-  .option('-p, --password <token>', 'Password')
-  .option('-i, --input <directory>', 'Videos directory absolute path')
-  .option('-d, --description <description>', 'Video descriptions')
-  .option('-c, --category <category>', 'Video categories')
-  .option('-l, --licence <licence>', 'Video licences')
-  .option('-t, --tags <tags>', 'Video tags', list)
-  .parse(process.argv)
-
-if (
-  !program['url'] ||
-  !program['username'] ||
-  !program['password'] ||
-  !program['input'] ||
-  !program['description'] ||
-  !program['category'] ||
-  !program['licence'] ||
-  !program['tags']
-) {
-  throw new Error('All arguments are required.')
-}
-
-if (isAbsolute(program['input']) === false) {
-  throw new Error('Input path should be absolute.')
-}
-
-let command = `npm run ts-node -- ${__dirname}/get-access-token.ts`
-command += ` -u "${program['url']}"`
-command += ` -n "${program['username']}"`
-command += ` -p "${program['password']}"`
-
-execCLI(command)
-  .then(stdout => {
-    const accessToken = stdout.replace('\n', '')
-
-    console.log(accessToken)
-
-    return readdirPromise(program['input']).then(files => ({ accessToken, files }))
-  })
-  .then(({ accessToken, files }) => {
-    return Promise.each(files, file => {
-      const video = {
-        tags: program['tags'],
-        name: file,
-        description: program['description'],
-        category: program['category'],
-        licence: program['licence']
-      }
-
-      let command = `npm run ts-node -- ${__dirname}/upload.ts`
-      command += ` -u "${program['url']}"`
-      command += ` -a "${accessToken}"`
-      command += ` -n "${video.name}"`
-      command += ` -d "${video.description}"`
-      command += ` -c "${video.category}"`
-      command += ` -l "${video.licence}"`
-      command += ` -t "${video.tags.join(',')}"`
-      command += ` -f "${join(program['input'], file)}"`
-
-      return execCLI(command).then(stdout => console.log(stdout))
-    })
-  })
-  .then(() => process.exit(0))
-  .catch(err => {
-    console.error(err)
-    process.exit(-1)
-  })
-
-// ----------------------------------------------------------------------------
-
-function list (val) {
-  return val.split(',')
-}
diff --git a/server/tests/real-world/tools/upload.ts b/server/tests/real-world/tools/upload.ts
deleted file mode 100644 (file)
index 81bc0d4..0000000
+++ /dev/null
@@ -1,85 +0,0 @@
-import * as program from 'commander'
-import { access, constants } from 'fs'
-import { isAbsolute } from 'path'
-import { promisify } from 'util'
-
-const accessPromise = promisify(access)
-
-import { uploadVideo } from '../../utils'
-
-program
-  .option('-u, --url <url>', 'Server url')
-  .option('-a, --access-token <token>', 'Access token')
-  .option('-n, --name <name>', 'Video name')
-  .option('-N, --nsfw', 'Video is Not Safe For Work')
-  .option('-c, --category <category number>', 'Category number')
-  .option('-l, --licence <licence number>', 'Licence number')
-  .option('-L, --language <language number>', 'Language number')
-  .option('-d, --description <description>', 'Video description')
-  .option('-t, --tags <tags>', 'Video tags', list)
-  .option('-f, --file <file>', 'Video absolute file path')
-  .parse(process.argv)
-
-if (!program['tags']) program['tags'] = []
-if (!program['nsfw']) program['nsfw'] = false
-
-if (
-  !program['url'] ||
-  !program['accessToken'] ||
-  !program['name'] ||
-  !program['category'] ||
-  !program['licence'] ||
-  !program['description'] ||
-  !program['file']
-) {
-  throw new Error('All arguments but tags, language and nsfw are required.')
-}
-
-if (isAbsolute(program['file']) === false) {
-  throw new Error('File path should be absolute.')
-}
-
-accessPromise(program['file'], constants.F_OK)
-  .then(() => {
-    return upload(
-      program['url'],
-      program['accessToken'],
-      program['name'],
-      program['category'],
-      program['licence'],
-      program['language'],
-      program['nsfw'],
-      program['description'],
-      program['tags'],
-      program['file']
-    )
-  })
-  .then(() => process.exit(0))
-  .catch(err => {
-    console.error(err)
-    process.exit(-1)
-  })
-
-// ----------------------------------------------------------------------------
-
-function list (val) {
-  return val.split(',')
-}
-
-function upload (url, accessToken, name, category, licence, language, nsfw, description, tags, fixture) {
-  console.log('Uploading %s video...', program['name'])
-
-  const videoAttributes = {
-    name,
-    category,
-    licence,
-    language,
-    nsfw,
-    description,
-    tags,
-    fixture
-  }
-  return uploadVideo(url, accessToken, videoAttributes).then(() => {
-    console.log(`Video ${name} uploaded.`)
-  })
-}
index 0b28edd48f2bf5946a83cb56ed66d8e4b6bfe2b2..923ca48f1a3aa5fd53e5b248eb67ab18dbfcdf2f 100644 (file)
@@ -249,8 +249,6 @@ async function uploadVideo (url: string, accessToken: string, videoAttributesArg
               .set('Accept', 'application/json')
               .set('Authorization', 'Bearer ' + accessToken)
               .field('name', attributes.name)
-              .field('category', attributes.category.toString())
-              .field('licence', attributes.licence.toString())
               .field('nsfw', JSON.stringify(attributes.nsfw))
               .field('commentsEnabled', JSON.stringify(attributes.commentsEnabled))
               .field('description', attributes.description)
@@ -260,6 +258,12 @@ async function uploadVideo (url: string, accessToken: string, videoAttributesArg
   if (attributes.language !== undefined) {
     req.field('language', attributes.language.toString())
   }
+  if (attributes.category !== undefined) {
+    req.field('category', attributes.category.toString())
+  }
+  if (attributes.licence !== undefined) {
+    req.field('licence', attributes.licence.toString())
+  }
 
   for (let i = 0; i < attributes.tags.length; i++) {
     req.field('tags[' + i + ']', attributes.tags[i])
diff --git a/server/tools/get-access-token.ts b/server/tools/get-access-token.ts
new file mode 100644 (file)
index 0000000..66fa708
--- /dev/null
@@ -0,0 +1,48 @@
+import * as program from 'commander'
+
+import {
+  getClient,
+  serverLogin
+} from '../tests/utils/index'
+
+program
+  .option('-u, --url <url>', 'Server url')
+  .option('-n, --username <username>', 'Username')
+  .option('-p, --password <token>', 'Password')
+  .parse(process.argv)
+
+if (
+  !program['url'] ||
+  !program['username'] ||
+  !program['password']
+) {
+  throw new Error('All arguments are required.')
+}
+
+const server = {
+  url: program['url'],
+  user: {
+    username: program['username'],
+    password: program['password']
+  },
+  client: {
+    id: null,
+    secret: null
+  }
+}
+
+getClient(program.url)
+  .then(res => {
+    server.client.id = res.body.client_id
+    server.client.secret = res.body.client_secret
+
+    return serverLogin(server)
+  })
+  .then(accessToken => {
+    console.log(accessToken)
+    process.exit(0)
+  })
+  .catch(err => {
+    console.error(err)
+    process.exit(-1)
+  })
diff --git a/server/tools/import-youtube.ts b/server/tools/import-youtube.ts
new file mode 100644 (file)
index 0000000..b4405c4
--- /dev/null
@@ -0,0 +1,155 @@
+import * as program from 'commander'
+import { createWriteStream } from 'fs'
+import { join } from 'path'
+import { cursorTo } from 'readline'
+import * as youtubeDL from 'youtube-dl'
+import { VideoPrivacy } from '../../shared/models/videos'
+import { unlinkPromise } from '../helpers/core-utils'
+import { getClient, getVideoCategories, login, searchVideo, uploadVideo } from '../tests/utils'
+
+program
+  .option('-u, --url <url>', 'Server url')
+  .option('-U, --username <username>', 'Username')
+  .option('-p, --password <token>', 'Password')
+  .option('-y, --youtube-url <directory>', 'Youtube URL')
+  .parse(process.argv)
+
+if (
+  !program['url'] ||
+  !program['username'] ||
+  !program['password'] ||
+  !program['youtubeUrl']
+) {
+  throw new Error('All arguments are required.')
+}
+
+run().catch(err => console.error(err))
+
+let accessToken: string
+
+async function run () {
+  const res = await getClient(program['url'])
+  const client = {
+    id: res.body.client_id,
+    secret: res.body.client_secret
+  }
+
+  const user = {
+    username: program['username'],
+    password: program['password']
+  }
+
+  const res2 = await login(program['url'], client, user)
+  accessToken = res2.body.access_token
+
+  youtubeDL.getInfo(program['youtubeUrl'], [ '-j', '--flat-playlist' ], async (err, info) => {
+    if (err) throw err
+
+    const videos = info.map(i => {
+      return { url: 'https://www.youtube.com/watch?v=' + i.id, name: i.title }
+    })
+
+    console.log('Will download and upload %d videos.\n', videos.length)
+
+    for (const video of videos) {
+      await processVideo(video)
+    }
+
+    console.log('I\'m finished!')
+    process.exit(0)
+  })
+}
+
+function processVideo (videoUrlName: { name: string, url: string }) {
+  return new Promise(async res => {
+    const result = await searchVideo(program['url'], videoUrlName.name)
+    if (result.body.total !== 0) {
+      console.log('Video "%s" already exist, don\'t reupload it.\n', videoUrlName.name)
+      return res()
+    }
+
+    const video = youtubeDL(videoUrlName.url)
+    let videoInfo
+    let videoPath: string
+
+    video.on('error', err => console.error(err))
+
+    let size = 0
+    video.on('info', info => {
+      videoInfo = info
+      size = info.size
+
+      videoPath = join(__dirname, size + '.mp4')
+      console.log('Creating "%s" of video "%s".', videoPath, videoInfo.title)
+
+      video.pipe(createWriteStream(videoPath))
+    })
+
+    let pos = 0
+    video.on('data', chunk => {
+      pos += chunk.length
+      // `size` should not be 0 here.
+      if (size) {
+        const percent = (pos / size * 100).toFixed(2)
+        writeWaitingPercent(percent)
+      }
+    })
+
+    video.on('end', async () => {
+      await uploadVideoOnPeerTube(videoInfo, videoPath)
+
+      return res()
+    })
+  })
+}
+
+function writeWaitingPercent (p: string) {
+  cursorTo(process.stdout, 0)
+  process.stdout.write(`waiting ... ${p}%`)
+}
+
+async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string) {
+  const category = await getCategory(videoInfo.categories)
+  const licence = getLicence(videoInfo.license)
+  const language = 13
+
+  const videoAttributes = {
+    name: videoInfo.title,
+    category,
+    licence,
+    language,
+    nsfw: false,
+    commentsEnabled: true,
+    description: videoInfo.description,
+    tags: videoInfo.tags.slice(0, 5),
+    privacy: VideoPrivacy.PUBLIC,
+    fixture: videoPath
+  }
+
+  console.log('\nUploading on PeerTube video "%s".', videoAttributes.name)
+  await uploadVideo(program['url'], accessToken, videoAttributes)
+  await unlinkPromise(videoPath)
+  console.log('Uploaded video "%s"!\n', videoAttributes.name)
+}
+
+async function getCategory (categories: string[]) {
+  const categoryString = categories[0]
+
+  if (categoryString === 'News & Politics') return 11
+
+  const res = await getVideoCategories(program['url'])
+  const categoriesServer = res.body
+
+  for (const key of Object.keys(categoriesServer)) {
+    const categoryServer = categoriesServer[key]
+    if (categoryString.toLowerCase() === categoryServer.toLowerCase()) return parseInt(key, 10)
+  }
+
+  return undefined
+}
+
+function getLicence (licence: string) {
+  if (licence.indexOf('Creative Commons Attribution licence') !== -1) return 1
+
+  return undefined
+}
diff --git a/server/tools/upload-directory.ts b/server/tools/upload-directory.ts
new file mode 100644 (file)
index 0000000..c0094f8
--- /dev/null
@@ -0,0 +1,82 @@
+import * as program from 'commander'
+import * as Promise from 'bluebird'
+import { isAbsolute, join } from 'path'
+
+import { readdirPromise } from '../helpers/core-utils'
+import { execCLI } from '../tests/utils/index'
+
+program
+  .option('-u, --url <url>', 'Server url')
+  .option('-U, --username <username>', 'Username')
+  .option('-p, --password <token>', 'Password')
+  .option('-i, --input <directory>', 'Videos directory absolute path')
+  .option('-d, --description <description>', 'Video descriptions')
+  .option('-c, --category <category>', 'Video categories')
+  .option('-l, --licence <licence>', 'Video licences')
+  .option('-t, --tags <tags>', 'Video tags', list)
+  .parse(process.argv)
+
+if (
+  !program['url'] ||
+  !program['username'] ||
+  !program['password'] ||
+  !program['input'] ||
+  !program['description'] ||
+  !program['category'] ||
+  !program['licence'] ||
+  !program['tags']
+) {
+  throw new Error('All arguments are required.')
+}
+
+if (isAbsolute(program['input']) === false) {
+  throw new Error('Input path should be absolute.')
+}
+
+let command = `npm run ts-node -- ${__dirname}/get-access-token.ts`
+command += ` -u "${program['url']}"`
+command += ` -n "${program['username']}"`
+command += ` -p "${program['password']}"`
+
+execCLI(command)
+  .then(stdout => {
+    const accessToken = stdout.replace('\n', '')
+
+    console.log(accessToken)
+
+    return readdirPromise(program['input']).then(files => ({ accessToken, files }))
+  })
+  .then(({ accessToken, files }) => {
+    return Promise.each(files, file => {
+      const video = {
+        tags: program['tags'],
+        name: file,
+        description: program['description'],
+        category: program['category'],
+        licence: program['licence']
+      }
+
+      let command = `npm run ts-node -- ${__dirname}/upload.ts`
+      command += ` -u "${program['url']}"`
+      command += ` -a "${accessToken}"`
+      command += ` -n "${video.name}"`
+      command += ` -d "${video.description}"`
+      command += ` -c "${video.category}"`
+      command += ` -l "${video.licence}"`
+      command += ` -t "${video.tags.join(',')}"`
+      command += ` -f "${join(program['input'], file)}"`
+
+      return execCLI(command).then(stdout => console.log(stdout))
+    })
+  })
+  .then(() => process.exit(0))
+  .catch(err => {
+    console.error(err)
+    process.exit(-1)
+  })
+
+// ----------------------------------------------------------------------------
+
+function list (val) {
+  return val.split(',')
+}
diff --git a/server/tools/upload.ts b/server/tools/upload.ts
new file mode 100644 (file)
index 0000000..db59bbd
--- /dev/null
@@ -0,0 +1,85 @@
+import * as program from 'commander'
+import { access, constants } from 'fs'
+import { isAbsolute } from 'path'
+import { promisify } from 'util'
+
+const accessPromise = promisify(access)
+
+import { uploadVideo } from '../tests/utils/index'
+
+program
+  .option('-u, --url <url>', 'Server url')
+  .option('-a, --access-token <token>', 'Access token')
+  .option('-n, --name <name>', 'Video name')
+  .option('-N, --nsfw', 'Video is Not Safe For Work')
+  .option('-c, --category <category number>', 'Category number')
+  .option('-l, --licence <licence number>', 'Licence number')
+  .option('-L, --language <language number>', 'Language number')
+  .option('-d, --description <description>', 'Video description')
+  .option('-t, --tags <tags>', 'Video tags', list)
+  .option('-f, --file <file>', 'Video absolute file path')
+  .parse(process.argv)
+
+if (!program['tags']) program['tags'] = []
+if (!program['nsfw']) program['nsfw'] = false
+
+if (
+  !program['url'] ||
+  !program['accessToken'] ||
+  !program['name'] ||
+  !program['category'] ||
+  !program['licence'] ||
+  !program['description'] ||
+  !program['file']
+) {
+  throw new Error('All arguments but tags, language and nsfw are required.')
+}
+
+if (isAbsolute(program['file']) === false) {
+  throw new Error('File path should be absolute.')
+}
+
+accessPromise(program['file'], constants.F_OK)
+  .then(() => {
+    return upload(
+      program['url'],
+      program['accessToken'],
+      program['name'],
+      program['category'],
+      program['licence'],
+      program['language'],
+      program['nsfw'],
+      program['description'],
+      program['tags'],
+      program['file']
+    )
+  })
+  .then(() => process.exit(0))
+  .catch(err => {
+    console.error(err)
+    process.exit(-1)
+  })
+
+// ----------------------------------------------------------------------------
+
+function list (val) {
+  return val.split(',')
+}
+
+function upload (url, accessToken, name, category, licence, language, nsfw, description, tags, fixture) {
+  console.log('Uploading %s video...', program['name'])
+
+  const videoAttributes = {
+    name,
+    category,
+    licence,
+    language,
+    nsfw,
+    description,
+    tags,
+    fixture
+  }
+  return uploadVideo(url, accessToken, videoAttributes).then(() => {
+    console.log(`Video ${name} uploaded.`)
+  })
+}
index 3a8b4cb30c90d33e9f3ec45968b0afd96aac164a..9608266d02cf41dd85e4324b12c0f25c0c59506c 100644 (file)
--- a/yarn.lock
+++ b/yarn.lock
@@ -3089,6 +3089,12 @@ hash.js@^1.0.0:
     inherits "^2.0.3"
     minimalistic-assert "^1.0.0"
 
+hashish@~0.0.4:
+  version "0.0.4"
+  resolved "https://registry.yarnpkg.com/hashish/-/hashish-0.0.4.tgz#6d60bc6ffaf711b6afd60e426d077988014e6554"
+  dependencies:
+    traverse ">=0.2.4"
+
 hawk@3.1.3, hawk@~3.1.3:
   version "3.1.3"
   resolved "https://registry.yarnpkg.com/hawk/-/hawk-3.1.3.tgz#078444bd7c1640b0fe540d2c9b73d59678e8e1c4"
@@ -3111,6 +3117,12 @@ he@1.1.1:
   version "1.1.1"
   resolved "https://registry.yarnpkg.com/he/-/he-1.1.1.tgz#93410fd21b009735151f8868c2f271f3427e23fd"
 
+hh-mm-ss@^1.2.0:
+  version "1.2.0"
+  resolved "https://registry.yarnpkg.com/hh-mm-ss/-/hh-mm-ss-1.2.0.tgz#6d0f0b8280824a634cb1d1f20e0bc7bc8b689948"
+  dependencies:
+    zero-fill "^2.2.3"
+
 highlight.js@^9.1.0:
   version "9.12.0"
   resolved "https://registry.yarnpkg.com/highlight.js/-/highlight.js-9.12.0.tgz#e6d9dbe57cbefe60751f02af336195870c90c01e"
@@ -6370,6 +6382,12 @@ stream-with-known-length-to-buffer@^1.0.0:
   dependencies:
     once "^1.3.3"
 
+streamify@^0.2.9:
+  version "0.2.9"
+  resolved "https://registry.yarnpkg.com/streamify/-/streamify-0.2.9.tgz#8938b14db491e2b6be4f8d99cc4133c9f0384f0b"
+  dependencies:
+    hashish "~0.0.4"
+
 streamsearch@0.1.2:
   version "0.1.2"
   resolved "https://registry.yarnpkg.com/streamsearch/-/streamsearch-0.1.2.tgz#808b9d0e56fc273d809ba57338e929919a1a9f1a"
@@ -6724,6 +6742,10 @@ trace@^1.1.0:
   dependencies:
     stack-chain "~1.3.1"
 
+traverse@>=0.2.4:
+  version "0.6.6"
+  resolved "https://registry.yarnpkg.com/traverse/-/traverse-0.6.6.tgz#cbdf560fd7b9af632502fed40f918c157ea97137"
+
 tree-kill@^1.1.0:
   version "1.2.0"
   resolved "https://registry.yarnpkg.com/tree-kill/-/tree-kill-1.2.0.tgz#5846786237b4239014f05db156b643212d4c6f36"
@@ -7403,6 +7425,15 @@ yn@^2.0.0:
   version "2.0.0"
   resolved "https://registry.yarnpkg.com/yn/-/yn-2.0.0.tgz#e5adabc8acf408f6385fc76495684c88e6af689a"
 
+youtube-dl@^1.12.2:
+  version "1.12.2"
+  resolved "https://registry.yarnpkg.com/youtube-dl/-/youtube-dl-1.12.2.tgz#11985268564c92b229f62b43d97374f86a605d1d"
+  dependencies:
+    hh-mm-ss "^1.2.0"
+    mkdirp "^0.5.1"
+    request "^2.83.0"
+    streamify "^0.2.9"
+
 zero-fill@^2.2.3:
   version "2.2.3"
   resolved "https://registry.yarnpkg.com/zero-fill/-/zero-fill-2.2.3.tgz#a3def06ba5e39ae644850bb4ca2ad4112b4855e9"