Begin import script with youtube-dl

pull/287/head
Chocobozzz 2018-02-09 16:47:06 +01:00
parent 1185c246c5
commit a7fea183f0
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
7 changed files with 198 additions and 7 deletions

View File

@ -135,7 +135,8 @@
"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

View 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])

View 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')

View File

@ -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
}

View 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')

View 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')

View File

@ -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"