+// FIXME: https://github.com/nodejs/node/pull/16853
+require('tls').DEFAULT_ECDH_CURVE = 'auto'
+
import * as program from 'commander'
import { join } from 'path'
import * as youtubeDL from 'youtube-dl'
import { doRequestAndSaveToFile } from '../helpers/requests'
import { CONSTRAINTS_FIELDS } from '../initializers'
import { getClient, getVideoCategories, login, searchVideo, uploadVideo } from '../tests/utils'
+import { truncate } from 'lodash'
program
.option('-u, --url <url>', 'Server url')
const options = [ '-j', '--flat-playlist', '--playlist-reverse' ]
youtubeDL.getInfo(program['targetUrl'], options, processOptions, async (err, info) => {
- if (err) throw err
+ if (err) {
+ console.log(err.message)
+ process.exit(1)
+ }
let infoArray: any[]
await processVideo(info, program['language'])
}
- // https://www.youtube.com/watch?v=2Upx39TBc1s
console.log('I\'m finished!')
process.exit(0)
})
console.log('Downloading video "%s"...', videoInfo.title)
const options = [ '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best', '-o', path ]
- youtubeDL.exec(videoInfo.url, options, processOptions, async (err, output) => {
- if (err) return console.error(err)
-
- console.log(output.join('\n'))
-
- await uploadVideoOnPeerTube(normalizeObject(videoInfo), path, languageCode)
-
+ try {
+ youtubeDL.exec(videoInfo.url, options, processOptions, async (err, output) => {
+ if (err) {
+ console.error(err)
+ return res()
+ }
+
+ console.log(output.join('\n'))
+ await uploadVideoOnPeerTube(normalizeObject(videoInfo), path, languageCode)
+ return res()
+ })
+ } catch (err) {
+ console.log(err.message)
return res()
- })
+ }
})
}
let tags = []
if (Array.isArray(videoInfo.tags)) {
tags = videoInfo.tags
- .filter(t => t.length < CONSTRAINTS_FIELDS.VIDEOS.TAG.max)
+ .filter(t => t.length < CONSTRAINTS_FIELDS.VIDEOS.TAG.max && t.length > CONSTRAINTS_FIELDS.VIDEOS.TAG.min)
.map(t => t.normalize())
.slice(0, 5)
}
}
const videoAttributes = {
- name: videoInfo.title,
+ name: truncate(videoInfo.title, {
+ 'length': CONSTRAINTS_FIELDS.VIDEOS.NAME.max,
+ 'separator': /,? +/,
+ 'omission': ' […]'
+ }),
category,
licence,
language,
- nsfw: false,
+ nsfw: isNSFW(videoInfo),
commentsEnabled: true,
- description: videoInfo.description,
+ description: videoInfo.description || undefined,
+ support: undefined,
tags,
privacy: VideoPrivacy.PUBLIC,
fixture: videoPath,
try {
await uploadVideo(program['url'], accessToken, videoAttributes)
} catch (err) {
- if (err.message.indexOf('401')) {
+ if (err.message.indexOf('401') !== -1) {
console.log('Got 401 Unauthorized, token may have expired, renewing token and retry.')
const res = await login(program['url'], client, user)
await uploadVideo(program['url'], accessToken, videoAttributes)
} else {
- throw err
+ console.log(err.message)
+ process.exit(1)
}
}
}
function buildUrl (info: any) {
+ const webpageUrl = info.webpage_url as string
+ if (webpageUrl && webpageUrl.match(/^https?:\/\//)) return webpageUrl
+
const url = info.url as string
- if (url && url.match(/^https?:\/\//)) return info.url
+ if (url && url.match(/^https?:\/\//)) return url
// It seems youtube-dl does not return the video url
return 'https://www.youtube.com/watch?v=' + info.id
}
+
+function isNSFW (info: any) {
+ if (info.age_limit && info.age_limit >= 16) return true
+
+ return false
+}