From a7fea183f0f69104b209e7bfdd6435be28165f22 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 9 Feb 2018 16:47:06 +0100 Subject: Begin import script with youtube-dl --- server/tools/get-access-token.ts | 48 ++++++++++++ server/tools/import-youtube.ts | 155 +++++++++++++++++++++++++++++++++++++++ server/tools/upload-directory.ts | 82 +++++++++++++++++++++ server/tools/upload.ts | 85 +++++++++++++++++++++ 4 files changed, 370 insertions(+) create mode 100644 server/tools/get-access-token.ts create mode 100644 server/tools/import-youtube.ts create mode 100644 server/tools/upload-directory.ts create mode 100644 server/tools/upload.ts (limited to 'server/tools') diff --git a/server/tools/get-access-token.ts b/server/tools/get-access-token.ts new file mode 100644 index 000000000..66fa70814 --- /dev/null +++ b/server/tools/get-access-token.ts @@ -0,0 +1,48 @@ +import * as program from 'commander' + +import { + getClient, + serverLogin +} from '../tests/utils/index' + +program + .option('-u, --url ', 'Server url') + .option('-n, --username ', 'Username') + .option('-p, --password ', '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 index 000000000..b4405c452 --- /dev/null +++ b/server/tools/import-youtube.ts @@ -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 ', 'Server url') + .option('-U, --username ', 'Username') + .option('-p, --password ', 'Password') + .option('-y, --youtube-url ', '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 index 000000000..c0094f852 --- /dev/null +++ b/server/tools/upload-directory.ts @@ -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 ', 'Server url') + .option('-U, --username ', 'Username') + .option('-p, --password ', 'Password') + .option('-i, --input ', 'Videos directory absolute path') + .option('-d, --description ', 'Video descriptions') + .option('-c, --category ', 'Video categories') + .option('-l, --licence ', 'Video licences') + .option('-t, --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 index 000000000..db59bbdff --- /dev/null +++ b/server/tools/upload.ts @@ -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 ', 'Server url') + .option('-a, --access-token ', 'Access token') + .option('-n, --name ', 'Video name') + .option('-N, --nsfw', 'Video is Not Safe For Work') + .option('-c, --category ', 'Category number') + .option('-l, --licence ', 'Licence number') + .option('-L, --language ', 'Language number') + .option('-d, --description ', 'Video description') + .option('-t, --tags ', 'Video tags', list) + .option('-f, --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.`) + }) +} -- cgit v1.2.3