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 console.error('All arguments are required.')
27 run().catch(err => console.error(err))
29 let accessToken: string
31 async function run () {
32 const res = await getClient(program['url'])
34 id: res.body.client_id,
35 secret: res.body.client_secret
39 username: program['username'],
40 password: program['password']
43 const res2 = await login(program['url'], client, user)
44 accessToken = res2.body.access_token
46 youtubeDL.getInfo(program['youtubeUrl'], [ '-j', '--flat-playlist' ], async (err, info) => {
49 // Normalize utf8 fields
50 info = info.map(i => normalizeObject(i))
52 const videos = info.map(i => {
53 return { url: 'https://www.youtube.com/watch?v=' + i.id, name: i.title }
56 console.log('Will download and upload %d videos.\n', videos.length)
58 for (const video of videos) {
59 await processVideo(video)
62 console.log('I\'m finished!')
67 function processVideo (video: { name: string, url: string }) {
68 return new Promise(async res => {
69 const result = await searchVideo(program['url'], video.name)
71 console.log('############################################################\n')
73 if (result.body.total !== 0) {
74 console.log('Video "%s" already exists, don\'t reupload it.\n', video.name)
78 const path = join(__dirname, new Date().getTime() + '.mp4')
80 console.log('Downloading video "%s"...', video.name)
82 youtubeDL.exec(video.url, [ '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]', '-o', path ], {}, async (err, output) => {
83 if (err) return console.error(err)
85 console.log(output.join('\n'))
87 youtubeDL.getInfo(video.url, async (err, videoInfo) => {
88 if (err) return console.error(err)
90 await uploadVideoOnPeerTube(normalizeObject(videoInfo), path)
98 async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string) {
99 const category = await getCategory(videoInfo.categories)
100 const licence = getLicence(videoInfo.license)
103 const videoAttributes = {
104 name: videoInfo.title,
109 commentsEnabled: true,
110 description: videoInfo.description,
111 tags: videoInfo.tags.slice(0, 5),
112 privacy: VideoPrivacy.PUBLIC,
116 console.log('\nUploading on PeerTube video "%s".', videoAttributes.name)
117 await uploadVideo(program['url'], accessToken, videoAttributes)
118 await unlinkPromise(videoPath)
119 console.log('Uploaded video "%s"!\n', videoAttributes.name)
122 async function getCategory (categories: string[]) {
123 const categoryString = categories[0]
125 if (categoryString === 'News & Politics') return 11
127 const res = await getVideoCategories(program['url'])
128 const categoriesServer = res.body
130 for (const key of Object.keys(categoriesServer)) {
131 const categoryServer = categoriesServer[key]
132 if (categoryString.toLowerCase() === categoryServer.toLowerCase()) return parseInt(key, 10)
138 function getLicence (licence: string) {
139 if (licence.indexOf('Creative Commons Attribution licence') !== -1) return 1
144 function normalizeObject (obj: any) {
145 const newObj: any = {}
147 for (const key of Object.keys(obj)) {
149 if (key === 'resolution') continue
151 const value = obj[key]
153 if (typeof value === 'string') {
154 newObj[key] = value.normalize()