Skip to content

Commit

Permalink
Revert "Revert "Merge pull request #60 from DaddyFrosty/main""
Browse files Browse the repository at this point in the history
This reverts commit 1dbe282.
  • Loading branch information
fawazahmed0 committed Nov 8, 2021
1 parent d369619 commit 729baec
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
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

0 comments on commit 729baec

Please sign in to comment.