X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Ftools%2Fimport-videos.ts;h=2f38ea7c77e9f079db666035fd5cafe365f80b71;hb=c49db162ee3ad0acf89f4e71ac0083b30ab798ea;hp=268101b41ab282b13ee687a9243ecec97591ae6e;hpb=61b3e146e16e997ea539cd4610af10d4b681e04a;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/tools/import-videos.ts b/server/tools/import-videos.ts index 268101b41..2f38ea7c7 100644 --- a/server/tools/import-videos.ts +++ b/server/tools/import-videos.ts @@ -1,3 +1,6 @@ +// 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' @@ -6,6 +9,7 @@ import { unlinkPromise } from '../helpers/core-utils' 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 ', 'Server url') @@ -53,7 +57,10 @@ async function run () { 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[] @@ -69,7 +76,6 @@ async function run () { await processVideo(info, program['language']) } - // https://www.youtube.com/watch?v=2Upx39TBc1s console.log('I\'m finished!') process.exit(0) }) @@ -96,15 +102,21 @@ function processVideo (info: any, languageCode: number) { 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() - }) + } }) } @@ -114,7 +126,7 @@ async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, languag 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) } @@ -130,13 +142,18 @@ async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, languag } 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, @@ -148,7 +165,7 @@ async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, languag 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) @@ -156,7 +173,8 @@ async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, languag await uploadVideo(program['url'], accessToken, videoAttributes) } else { - throw err + console.log(err.message) + process.exit(1) } } @@ -227,9 +245,18 @@ function fetchObject (info: any) { } 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 +}