]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
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/utils/videos/videos.ts
server/tools/get-access-token.ts [moved from server/tests/real-world/tools/get-access-token.ts with 96% similarity]
server/tools/import-youtube.ts [new file with mode: 0644]
server/tools/upload-directory.ts [moved from server/tests/real-world/tools/upload-directory.ts with 95% similarity]
server/tools/upload.ts [moved from server/tests/real-world/tools/upload.ts with 97% similarity]
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
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])
similarity index 96%
rename from server/tests/real-world/tools/get-access-token.ts
rename to server/tools/get-access-token.ts
index ee14733e34d12b9323c626a1d14f79b2cddbc47c..66fa7081449b5d606ea416db69110601a01c8701 100644 (file)
@@ -3,7 +3,7 @@ import * as program from 'commander'
 import {
   getClient,
   serverLogin
-} from '../../utils'
+} from '../tests/utils/index'
 
 program
   .option('-u, --url <url>', 'Server url')
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
+}
similarity index 95%
rename from server/tests/real-world/tools/upload-directory.ts
rename to server/tools/upload-directory.ts
index fdd56857a0742d1de5387fd50cca3d6e1287c0d9..c0094f852c69211b974f010eece0b5df5ed1a964 100644 (file)
@@ -2,8 +2,8 @@ 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'
+import { readdirPromise } from '../helpers/core-utils'
+import { execCLI } from '../tests/utils/index'
 
 program
   .option('-u, --url <url>', 'Server url')
similarity index 97%
rename from server/tests/real-world/tools/upload.ts
rename to server/tools/upload.ts
index 81bc0d415a7dc09ef72c7a1c575d90e2d58330ad..db59bbdffa9e424bd2bef9c631cd261136135818 100644 (file)
@@ -5,7 +5,7 @@ import { promisify } from 'util'
 
 const accessPromise = promisify(access)
 
-import { uploadVideo } from '../../utils'
+import { uploadVideo } from '../tests/utils/index'
 
 program
   .option('-u, --url <url>', 'Server url')
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"