Skip to content

Commit

Permalink
peertube-import-videos: various code style improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
fflorent committed Dec 31, 2020
1 parent 704590c commit 9227a97
Showing 1 changed file with 48 additions and 54 deletions.
102 changes: 48 additions & 54 deletions server/tools/peertube-import-videos.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { registerTSPaths } from '../helpers/register-ts-paths'
registerTSPaths()

import * as program from 'commander'
import { accessSync, constants } from 'fs'
import { join } from 'path'
import { promisify } from 'util'

import * as program from 'commander'
import { remove } from 'fs-extra'
import { truncate } from 'lodash'
import * as prompt from 'prompt'

import { doRequestAndSaveToFile } from '../helpers/requests'
import { CONSTRAINTS_FIELDS } from '../initializers/constants'
import { getClient, getVideoCategories, login, searchVideoWithSort, uploadVideo } from '../../shared/extra-utils/index'
import { truncate } from 'lodash'
import * as prompt from 'prompt'
import { accessSync, constants } from 'fs'
import { remove } from 'fs-extra'
import { sha256 } from '../helpers/core-utils'
import { buildOriginallyPublishedAt, safeGetYoutubeDL } from '../helpers/youtube-dl'
import { buildCommonVideoOptions, buildVideoAttributesFromCommander, getLogger, getServerCredentials } from './cli'
Expand Down Expand Up @@ -115,72 +118,63 @@ async function run (url: string, user: UserInfo) {
})
}

function processVideo (parameters: {
async function processVideo (parameters: {
cwd: string
url: string
user: { username: string, password: string }
youtubeInfo: any
}) {
const { youtubeInfo, cwd, url, user } = parameters

return new Promise(async res => {
log.debug('Fetching object.', youtubeInfo)
log.debug('Fetching object.', youtubeInfo)

const videoInfo = await fetchObject(youtubeInfo)
log.debug('Fetched object.', videoInfo)
const videoInfo = await fetchObject(youtubeInfo)
log.debug('Fetched object.', videoInfo)

if (program['since']) {
if (buildOriginallyPublishedAt(videoInfo).getTime() < program['since'].getTime()) {
log.info('Video "%s" has been published before "%s", don\'t upload it.\n',
videoInfo.title, formatDate(program['since']))
return res()
}
if (program['since']) {
if (buildOriginallyPublishedAt(videoInfo).getTime() < program['since'].getTime()) {
log.info('Video "%s" has been published before "%s", don\'t upload it.\n',
videoInfo.title, formatDate(program['since']))
return
}
if (program['until']) {
if (buildOriginallyPublishedAt(videoInfo).getTime() > program['until'].getTime()) {
log.info('Video "%s" has been published after "%s", don\'t upload it.\n',
videoInfo.title, formatDate(program['until']))
return res()
}
}
if (program['until']) {
if (buildOriginallyPublishedAt(videoInfo).getTime() > program['until'].getTime()) {
log.info('Video "%s" has been published after "%s", don\'t upload it.\n',
videoInfo.title, formatDate(program['until']))
return
}
}

const result = await searchVideoWithSort(url, videoInfo.title, '-match')

log.info('############################################################\n')
const result = await searchVideoWithSort(url, videoInfo.title, '-match')

if (result.body.data.find(v => v.name === videoInfo.title)) {
log.info('Video "%s" already exists, don\'t reupload it.\n', videoInfo.title)
return res()
}
log.info('############################################################\n')

const path = join(cwd, sha256(videoInfo.url) + '.mp4')
if (result.body.data.find(v => v.name === videoInfo.title)) {
log.info('Video "%s" already exists, don\'t reupload it.\n', videoInfo.title)
return
}

log.info('Downloading video "%s"...', videoInfo.title)
const path = join(cwd, sha256(videoInfo.url) + '.mp4')

const options = [ '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best', ...command.args, '-o', path ]
try {
const youtubeDL = await safeGetYoutubeDL()
youtubeDL.exec(videoInfo.url, options, processOptions, async (err, output) => {
if (err) {
log.error(err)
return res()
}
log.info('Downloading video "%s"...', videoInfo.title)

log.info(output.join('\n'))
await uploadVideoOnPeerTube({
cwd,
url,
user,
videoInfo: normalizeObject(videoInfo),
videoPath: path
})
return res()
})
} catch (err) {
log.error(err.message)
return res()
}
})
const options = [ '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best', ...command.args, '-o', path ]
try {
const youtubeDL = await safeGetYoutubeDL()
const youtubeDLExec = promisify(youtubeDL.exec).bind(youtubeDL)
const output = await youtubeDLExec(videoInfo.url, options, processOptions)
log.info(output.join('\n'))
await uploadVideoOnPeerTube({
cwd,
url,
user,
videoInfo: normalizeObject(videoInfo),
videoPath: path
})
} catch (err) {
log.error(err.message)
}
}

async function uploadVideoOnPeerTube (parameters: {
Expand Down

0 comments on commit 9227a97

Please sign in to comment.