Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for onProgress event. #60

Merged
merged 4 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const onVideoUploadSuccess = (videoUrl) => {
// ..do something..
}
// Extra options like tags, thumbnail, language, playlist etc
const video2 = { path: 'video2.mp4', title: 'title 2', description: 'description 2', thumbnail:'thumbnail.png', language: 'english', tags: ['video', 'github'], playlist: 'playlist name', onSuccess:onVideoUploadSuccess, skipProcessingWait: true }
const video2 = { path: 'video2.mp4', title: 'title 2', description: 'description 2', thumbnail:'thumbnail.png', language: 'english', tags: ['video', 'github'], playlist: 'playlist name', onSuccess:onVideoUploadSuccess, skipProcessingWait: true, onProgress: (progress) => { console.log('progress', progress) } }


// Returns uploaded video links in array
Expand Down
12 changes: 12 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ export interface Video {
thumbnail?: string
onSuccess?: Function
skipProcessingWait?: boolean
onProgress?: (arg0: VideoProgress) => void
}

export enum ProgressEnum {
Uploading,
Processing,
Done
}

export interface VideoProgress {
progress: number
stage: ProgressEnum
}

export interface VideoToEdit {
Expand Down
35 changes: 34 additions & 1 deletion src/upload.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Credentials, Video, VideoToEdit, Comment } from './types'
import { Credentials, Video, VideoToEdit, Comment, VideoProgress, ProgressEnum } from './types'
import puppeteer, { PuppeteerExtra } from 'puppeteer-extra'
import { Puppeteer, PuppeteerNode, PuppeteerNodeLaunchOptions, Browser, Page, errors, PuppeteerErrors } from 'puppeteer'
import fs from 'fs-extra'
Expand Down Expand Up @@ -107,14 +107,47 @@ async function uploadVideo(videoJSON: Video) {
selectBtn[0].click() // button that triggers file selection
])
await fileChooser.accept([pathToFile])

// Setup onProgress
let progressChecker: NodeJS.Timer | undefined
let progress: VideoProgress = { progress: 0, stage: ProgressEnum.Uploading };
if (videoJSON.onProgress)
videoJSON.onProgress(progress)
progressChecker = setInterval(async () => {
let curProgress = await page.evaluate(() => {
let items = document.querySelectorAll("span.progress-label.ytcp-video-upload-progress");
for (let i = 0; i < items.length; i++) {
if (items.item(i).textContent!.indexOf("%") === -1) continue;
return items.item(i).textContent;
}
})
if (!progressChecker || !curProgress) return
curProgress = curProgress.split(" ").find(txt => txt.indexOf("%") != -1)
let newProgress = curProgress ? parseInt(curProgress.slice(0, -1)) : 0
if ( progress.progress == newProgress ) return
progress.progress = newProgress
videoJSON.onProgress!(progress)
}, 500)
// Wait for upload to complete
await page.waitForXPath('//*[contains(text(),"Upload complete")]', { timeout: 0 })
if (videoJSON.onProgress) {
progress = { progress: 0, stage: ProgressEnum.Processing }
videoJSON.onProgress(progress)
}

// Wait for upload to go away and processing to start, skip the wait if the user doesn't want it.
if (!videoJSON.skipProcessingWait) {
await page.waitForXPath('//*[contains(text(),"Upload complete")]', { hidden: true, timeout: 0 })
} else {
await sleep(5000)
}
if (videoJSON.onProgress) {
clearInterval(progressChecker)
progressChecker = undefined
progress = { progress: 100, stage: ProgressEnum.Done }
videoJSON.onProgress(progress)
}

// Wait until title & description box pops up
if (thumb) {
const [thumbChooser] = await Promise.all([
Expand Down