1 import * as program from 'commander'
2 import { createWriteStream } from 'fs'
3 import { join } from 'path'
4 import { cursorTo } from 'readline'
5 import * as youtubeDL from 'youtube-dl'
6 import { VideoPrivacy } from '../../shared/models/videos'
7 import { unlinkPromise } from '../helpers/core-utils'
8 import { getClient, getVideoCategories, login, searchVideo, uploadVideo } from '../tests/utils'
11 .option('-u, --url <url>', 'Server url')
12 .option('-U, --username <username>', 'Username')
13 .option('-p, --password <token>', 'Password')
14 .option('-y, --youtube-url <youtubeUrl>', 'Youtube URL')
19 !program['username'] ||
20 !program['password'] ||
21 !program['youtubeUrl']
23 throw new Error('All arguments are required.')
26 run().catch(err => console.error(err))
28 let accessToken: string
30 async function run () {
31 const res = await getClient(program['url'])
33 id: res.body.client_id,
34 secret: res.body.client_secret
38 username: program['username'],
39 password: program['password']
42 const res2 = await login(program['url'], client, user)
43 accessToken = res2.body.access_token
45 youtubeDL.getInfo(program['youtubeUrl'], [ '-j', '--flat-playlist' ], async (err, info) => {
48 // Normalize utf8 fields
49 info = info.map(i => normalizeObject(i))
51 const videos = info.map(i => {
52 return { url: 'https://www.youtube.com/watch?v=' + i.id, name: i.title }
55 console.log('Will download and upload %d videos.\n', videos.length)
57 for (const video of videos) {
58 await processVideo(video)
61 console.log('I\'m finished!')
66 function processVideo (video: { name: string, url: string }) {
67 return new Promise(async res => {
68 const result = await searchVideo(program['url'], video.name)
70 console.log('############################################################\n')
72 if (result.body.total !== 0) {
73 console.log('Video "%s" already exists, don\'t reupload it.\n', video.name)
77 const path = join(__dirname, new Date().getTime() + '.mp4')
79 console.log('Downloading video "%s"...', video.name)
81 youtubeDL.exec(video.url, [ '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]', '-o', path ], {}, async (err, output) => {
82 if (err) return console.error(err)
84 console.log(output.join('\n'))
86 youtubeDL.getInfo(video.url, async (err, videoInfo) => {
87 if (err) return console.error(err)
89 await uploadVideoOnPeerTube(normalizeObject(videoInfo), path)
97 async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string) {
98 const category = await getCategory(videoInfo.categories)
99 const licence = getLicence(videoInfo.license)
102 const videoAttributes = {
103 name: videoInfo.title,
108 commentsEnabled: true,
109 description: videoInfo.description,
110 tags: videoInfo.tags.slice(0, 5),
111 privacy: VideoPrivacy.PUBLIC,
115 console.log('\nUploading on PeerTube video "%s".', videoAttributes.name)
116 await uploadVideo(program['url'], accessToken, videoAttributes)
117 await unlinkPromise(videoPath)
118 console.log('Uploaded video "%s"!\n', videoAttributes.name)
121 async function getCategory (categories: string[]) {
122 const categoryString = categories[0]
124 if (categoryString === 'News & Politics') return 11
126 const res = await getVideoCategories(program['url'])
127 const categoriesServer = res.body
129 for (const key of Object.keys(categoriesServer)) {
130 const categoryServer = categoriesServer[key]
131 if (categoryString.toLowerCase() === categoryServer.toLowerCase()) return parseInt(key, 10)
137 function getLicence (licence: string) {
138 if (licence.indexOf('Creative Commons Attribution licence') !== -1) return 1
143 function normalizeObject (obj: any) {
144 const newObj: any = {}
146 for (const key of Object.keys(obj)) {
148 if (key === 'resolution') continue
150 const value = obj[key]
152 if (typeof value === 'string') {
153 newObj[key] = value.normalize()