Skip to content

Commit

Permalink
Add logging and correct usage of the user temporary folder, see puppe…
Browse files Browse the repository at this point in the history
  • Loading branch information
emdin committed Sep 5, 2020
1 parent db5b3c5 commit d38c158
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 32 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ node_modules
npm-debug.log
public
_redirects
config.js*
tmp
*.log
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"dependencies": {
"puppeteer": "^5.2.1",
"puppeteer-extra": "^3.1.13",
"puppeteer-extra-plugin-stealth": "^2.5.0"
"puppeteer-extra-plugin-stealth": "^2.5.0",
"simple-node-logger": "^18.12.24"
}
}
92 changes: 61 additions & 31 deletions share-youtube-video.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,76 @@
const fs = require('fs');
const path = require('path');

const puppeteer = require('puppeteer-extra');

const log = require('simple-node-logger').createSimpleFileLogger('project.log');

const config = require('./config.js');
const { URL_GOOGLE_ACCOUNTS, URL_YOUTUBE_STUDIO_VIDEO, GOOGLE_USER, GOOGLE_PASSWORD, USER_TO_SHARE } = config;

const StealthPlugin = require('puppeteer-extra-plugin-stealth')
puppeteer.use(StealthPlugin());

const dataDir = path.resolve(__dirname, 'tmp');

fs.access(dataDir, fs.constants.W_OK, function(err) {
if(err){
log.error(`Can't write in the folder ${dataDir}, please make sure script has access rights`);
process.exit(1);
}
});

log.info(`Using folder ${dataDir}`);

async function openSharingVideoWindow (browser, page, log) {
await page.goto(URL_YOUTUBE_STUDIO_VIDEO);
await page.waitFor(1000);

log.info('Open studio')

url = await page.url();

log.info('Curently on page ' + url);

await page.waitForSelector('ytcp-icon-button[id="overflow-menu-button"]');
await page.click('ytcp-icon-button[id="overflow-menu-button"]');
await page.waitFor(300);

const elements = await page.$x('//yt-formatted-string[text()="Share privately"]')
await elements[0].click();

const newPagePromise = new Promise(x => browser.once('targetcreated', target => x(target.page())));
const popup = await newPagePromise;

url = await popup.url();
log.info('Currently on page ', url);

await page.goto(url);
await page.bringToFront();
await page.waitFor(1000);
}

puppeteer.launch({headless: false, product: 'chrome'}).then(async browser => {
puppeteer.launch({ headless: true, product: 'chrome', userDataDir: dataDir}).then(async browser => {

const page = await browser.newPage();

console.log('Create browser');
log.info('Create browser');

await page.setViewport({ width: 1280, height: 800 })

await page.goto(URL_YOUTUBE_STUDIO_VIDEO);

console.log('Try to authorize');
log.info('Open studio');

await page.waitFor(3000);

let url = await page.url();

console.log('Arrive to the page ' + url);
log.info('Currently on page ' + url);

if (url.indexOf('ServiceLogin')>0) {

console.log('Need to authorize');
log.info('Need to authorize');

await page.waitForSelector('input[type="email"]')

Expand All @@ -40,42 +84,28 @@ puppeteer.launch({headless: false, product: 'chrome'}).then(async browser => {
await page.type('input[type="password"]', GOOGLE_PASSWORD);
await page.waitFor(1000);

console.log('Authorize');
log.info('Authorizing');

await page.keyboard.press('Enter');
await page.waitFor(1000);
}

await page.goto(URL_YOUTUBE_STUDIO_VIDEO);
await page.waitFor(1000);

console.log('Open studio')

url = await page.url();

console.log('Arrive to the page ' + url);

await page.waitForSelector('ytcp-icon-button[id="overflow-menu-button"]');
await page.click('ytcp-icon-button[id="overflow-menu-button"]');
await page.waitFor(300);

const elements = await page.$x('//yt-formatted-string[text()="Share privately"]')
await elements[0].click();

const newPagePromise = new Promise(x => browser.once('targetcreated', target => x(target.page())));
const popup = await newPagePromise;

url = await popup.url();
console.log('Change video access level', url);

await page.goto(url);
await page.bringToFront();
await page.waitFor(1000);
openSharingVideoWindow(browser, page, log);

await page.waitForSelector('textarea.metadata-share-contacts');
await page.type('textarea.metadata-share-contacts', USER_TO_SHARE, {delay: 20});
await page.click('button.sharing-dialog-ok');
await page.waitFor(3000);

openSharingVideoWindow(browser, page, log);
await page.waitForSelector('div.acl-target-list-inner-container');

if (await page.waitForSelector(`div[title="${USER_TO_SHARE}"]`, { timeout: 3000 }) === null) {
log.error(`Video is not shared with ${USER_TO_SHARE}, please try again`);
} else {
log.info(`All good, video is shared with ${USER_TO_SHARE}`);
}

await browser.close();

});

0 comments on commit d38c158

Please sign in to comment.