1 import * as program from 'commander'
2 import { join } from 'path'
3 import * as youtubeDL from 'youtube-dl'
4 import { VideoPrivacy } from '../../shared/models/videos'
5 import { unlinkPromise } from '../helpers/core-utils'
6 import { doRequestAndSaveToFile } from '../helpers/requests'
7 import { CONSTRAINTS_FIELDS } from '../initializers'
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')
15 .option('-l, --language <languageCode>', 'Language code')
20 !program['username'] ||
21 !program['password'] ||
22 !program['youtubeUrl']
24 console.error('All arguments are required.')
28 run().catch(err => console.error(err))
30 let accessToken: string
31 const processOptions = {
36 async function run () {
37 const res = await getClient(program['url'])
39 id: res.body.client_id,
40 secret: res.body.client_secret
44 username: program['username'],
45 password: program['password']
48 const res2 = await login(program['url'], client, user)
49 accessToken = res2.body.access_token
51 const options = [ '-j', '--flat-playlist' ]
52 youtubeDL.getInfo(program['youtubeUrl'], options, processOptions, async (err, info) => {
55 // Normalize utf8 fields
56 info = info.map(i => normalizeObject(i))
58 const videos = info.map(i => {
59 return { url: 'https://www.youtube.com/watch?v=' + i.id, name: i.title }
62 console.log('Will download and upload %d videos.\n', videos.length)
64 for (const video of videos) {
65 await processVideo(video, program['languageCode'])
68 console.log('I\'m finished!')
73 function processVideo (video: { name: string, url: string }, languageCode: number) {
74 return new Promise(async res => {
75 const result = await searchVideo(program['url'], video.name)
77 console.log('############################################################\n')
79 if (result.body.total !== 0) {
80 console.log('Video "%s" already exists, don\'t reupload it.\n', video.name)
84 const path = join(__dirname, new Date().getTime() + '.mp4')
86 console.log('Downloading video "%s"...', video.name)
88 const options = [ '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]', '-o', path ]
89 youtubeDL.exec(video.url, options, processOptions, async (err, output) => {
90 if (err) return console.error(err)
92 console.log(output.join('\n'))
94 youtubeDL.getInfo(video.url, undefined, processOptions, async (err, videoInfo) => {
95 if (err) return console.error(err)
97 await uploadVideoOnPeerTube(normalizeObject(videoInfo), path, languageCode)
105 async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, language?: number) {
106 const category = await getCategory(videoInfo.categories)
107 const licence = getLicence(videoInfo.license)
109 if (Array.isArray(videoInfo.tags)) {
110 tags = videoInfo.tags.filter(t => t.length < CONSTRAINTS_FIELDS.VIDEOS.TAG.max).slice(0, 5)
114 if (videoInfo.thumbnail) {
115 thumbnailfile = join(__dirname, 'thumbnail.jpg')
117 await doRequestAndSaveToFile({
119 uri: videoInfo.thumbnail
123 const videoAttributes = {
124 name: videoInfo.title,
129 commentsEnabled: true,
130 description: videoInfo.description,
132 privacy: VideoPrivacy.PUBLIC,
135 previewfile: thumbnailfile
138 console.log('\nUploading on PeerTube video "%s".', videoAttributes.name)
139 await uploadVideo(program['url'], accessToken, videoAttributes)
141 await unlinkPromise(videoPath)
143 await unlinkPromise(thumbnailfile)
146 console.log('Uploaded video "%s"!\n', videoAttributes.name)
149 async function getCategory (categories: string[]) {
150 const categoryString = categories[0]
152 if (categoryString === 'News & Politics') return 11
154 const res = await getVideoCategories(program['url'])
155 const categoriesServer = res.body
157 for (const key of Object.keys(categoriesServer)) {
158 const categoryServer = categoriesServer[key]
159 if (categoryString.toLowerCase() === categoryServer.toLowerCase()) return parseInt(key, 10)
165 function getLicence (licence: string) {
166 if (licence.indexOf('Creative Commons Attribution licence') !== -1) return 1
171 function normalizeObject (obj: any) {
172 const newObj: any = {}
174 for (const key of Object.keys(obj)) {
176 if (key === 'resolution') continue
178 const value = obj[key]
180 if (typeof value === 'string') {
181 newObj[key] = value.normalize()