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

Add Cookie Handling, Fix Publish Date, and Update Element Selectors #88

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
82 changes: 59 additions & 23 deletions src/anchorfm-pupeteer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ function addUrlToDescription(youtubeVideoInfo) {

async function setPublishDate(page, navigationPromise, date) {
console.log('-- Setting publish date');
const publishDateButtonSelector = '//span[contains(text(),"Publish date:")]/following-sibling::button';
const [publishDateButton] = await page.$x(publishDateButtonSelector);
await publishDateButton.click();
await clickSelector(page,
'#app-content > div > form > div.sc-ecGeiE.crgHTL > div > button',
env.UPLOAD_TIMEOUT)
await navigationPromise;

await resetDatePickerToSelectYears(page, navigationPromise);
await selectYearInDatePicker(page, navigationPromise, date.year);
await selectMonthInDatePicker(page, navigationPromise, date.month);
await selectDayInDatePicker(page, navigationPromise, date.day);

const confirmButtonSelector = '//span[contains(text(),"Confirm")]/parent::button';
const [confirmButton] = await page.$x(confirmButtonSelector);
await confirmButton.click();
await clickSelector(page,
'body > reach-portal > div:nth-child(2) > div > div > div > div.sc-iAKWXU.hVFUbQ > div.sc-cCcXHH.dMPWmq > button.Button-sc-qlcn5g-0.hWxHrB',
env.UPLOAD_TIMEOUT)
await navigationPromise;
}

Expand Down Expand Up @@ -72,13 +72,29 @@ async function selectMonthInDatePicker(page, navigationPromise, month) {

async function selectDayInDatePicker(page, navigationPromise, day) {
const dayWithRemovedZeroPad = parseInt(day, 10);
const tdDay = await page.$(
`tbody > tr > td[data-value="${dayWithRemovedZeroPad}"][class*="rdtDay"]:not([class*="rdtOld"]:not([class*="rtdNew"])`
);
const [tdDay] = await page.$x(`//tbody/tr/td[contains(text(), "${dayWithRemovedZeroPad}") and contains(@class, 'rdtDay') and not(contains(@class, 'rdtNew')) and not(contains(@class, 'rdtOld'))]`);
await tdDay.click();
await navigationPromise;
}

async function clickSelector(page, selector, timeout) {
try {
await page.waitForSelector(selector, { timeout: timeout });
const element = await page.$(selector);
await clickDom(page, element);
} catch (error) {
console.error(`Error clicking selector: ${selector}`);
console.error(error);
}
}

async function clickDom(page, domBtn){
await page.evaluate((elem)=>{
elem.click();
}, domBtn)

}

async function postEpisode(youtubeVideoInfo) {
let browser;
try {
Expand All @@ -100,10 +116,26 @@ async function postEpisode(youtubeVideoInfo) {
and because pupeteer treats the page as loaded(or navigated to)
even when the form is not showed
*/

try{

await clickSelector(page, '#onetrust-pc-btn-handler', env.COOKIE_TIMEOUT);
await clickSelector(page, '#cookie-preferences > div.save-preference-btn-container > button', env.UPLOAD_TIMEOUT);

}catch (err){
console.log('Cookie already accepted')
}
await page.waitForSelector('#email');
await page.type('#email', env.ANCHOR_EMAIL);
await page.type('#password', env.ANCHOR_PASSWORD);
await page.click('button[type=submit]');
await page.evaluate((selector) => {
const button = document.querySelector(selector);
if (button) {
button.click();
} else {
throw new Error('No element found with the selector "${selector}"');
}
}, 'button[type=submit]');
await navigationPromise;
console.log('Logged in');

Expand All @@ -117,12 +149,12 @@ async function postEpisode(youtubeVideoInfo) {
setTimeout(r, 25 * 1000);
});

const saveEpisodeButtonSelector = '//span[contains(text(),"Save")]/parent::button[not(boolean(@disabled))]';
await page.waitForXPath(saveEpisodeButtonSelector, { timeout: env.UPLOAD_TIMEOUT });
const [saveButton] = await page.$x(saveEpisodeButtonSelector);
await saveButton.click();
await clickSelector(page,
'#app-content > div > div > div > div > div.sc-kOJRsK.icMpAT > button:not([disabled]) > span.ButtonInner-sc-14ud5tc-0.cLOWub.encore-bright-accent-set',
env.UPLOAD_TIMEOUT);
await navigationPromise;

console.log('-- Adding title');
await page.waitForSelector('#title', { visible: true });
// Wait some time so any field refresh doesn't mess up with our input
Expand Down Expand Up @@ -155,18 +187,19 @@ async function postEpisode(youtubeVideoInfo) {
await inputEpisodeArt.uploadFile(env.THUMBNAIL_FILE);

console.log('-- Saving uploaded episode art');
const saveThumbnailButtonSelector = '//span[text()="Save"]/parent::button';
await page.waitForXPath(saveThumbnailButtonSelector);
const [saveEpisodeArtButton] = await page.$x(saveThumbnailButtonSelector);
await saveEpisodeArtButton.click();

await clickSelector(page,
'body > reach-portal > div:nth-child(2) > div > div > div > div.sc-iAKWXU.hVFUbQ > div > div.sc-cCcXHH.dMPWmq > button.Button-sc-qlcn5g-0.hWxHrB > span.ButtonInner-sc-14ud5tc-0.cLOWub.encore-bright-accent-set',
env.UPLOAD_TIMEOUT);

await page.waitForXPath('//div[@aria-label="image uploader"]', { hidden: true, timeout: env.UPLOAD_TIMEOUT });
}

const saveDraftOrPublishOrScheduleButtonDescription = getSaveDraftOrPublishOrScheduleButtonDescription();
console.log(`-- ${saveDraftOrPublishOrScheduleButtonDescription.message}`);

const [saveDraftOrPublishOrScheduleButton] = await page.$x(saveDraftOrPublishOrScheduleButtonDescription.xpath);
await saveDraftOrPublishOrScheduleButton.click();
await clickSelector(page,
saveDraftOrPublishOrScheduleButtonDescription.selector,
env.UPLOAD_TIMEOUT)
await navigationPromise;

console.log('Yay');
Expand All @@ -182,19 +215,22 @@ async function postEpisode(youtubeVideoInfo) {
function getSaveDraftOrPublishOrScheduleButtonDescription() {
if (env.SAVE_AS_DRAFT) {
return {
xpath: '//button[text()="Save as draft"]',
selector: '#app-content > div > form > div.sc-kgwpUT.fYJoqV > div.sc-hGVAJH.sc-lfzZHy.dnsAoX.kwvghZ > button.Button-sc-y0gtbx-0.kCUxRI',
xpath: '//button[@class="Button-sc-y0gtbx-0 kCUxRI"]',
message: 'Saving draft',
};
}

if (env.SET_PUBLISH_DATE) {
return {
selector: '#app-content > div > form > div.sc-kgwpUT.fYJoqV > div.sc-hGVAJH.sc-lfzZHy.dnsAoX.kwvghZ > button.Button-sc-qlcn5g-0.hWxHrB > span.ButtonInner-sc-14ud5tc-0.cLOWub.encore-bright-accent-set',
xpath: '//span[text()="Schedule episode"]/parent::button',
message: 'Scheduling',
};
}

return {
selector: '#app-content > div > form > div.sc-kgwpUT.fYJoqV > div.sc-hGVAJH.sc-lfzZHy.dnsAoX.kwvghZ > button.Button-sc-qlcn5g-0.hWxHrB',
xpath: '//span[text()="Publish now"]/parent::button',
message: 'Publishing',
};
Expand Down
4 changes: 3 additions & 1 deletion src/environment-variables/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const defaultValues = {
ANCHOR_EMAIL: '',
ANCHOR_PASSWORD: '',
UPLOAD_TIMEOUT: 60 * 5 * 1000,
SAVE_AS_DRAFT: false,
COOKIE_TIMEOUT: 20 * 1000,
SAVE_AS_DRAFT: true,
LOAD_THUMBNAIL: false,
IS_EXPLICIT: false,
URL_IN_DESCRIPTION: false,
Expand Down Expand Up @@ -58,6 +59,7 @@ module.exports = {
ANCHOR_EMAIL: getEnvironmentVariable('ANCHOR_EMAIL'),
ANCHOR_PASSWORD: getEnvironmentVariable('ANCHOR_PASSWORD'),
UPLOAD_TIMEOUT: getEnvironmentVariable('UPLOAD_TIMEOUT'),
COOKIE_TIMEOUT: getEnvironmentVariable('COOKIE_TIMEOUT'),
SAVE_AS_DRAFT: getBoolean(getEnvironmentVariable('SAVE_AS_DRAFT')),
LOAD_THUMBNAIL: getBoolean(getEnvironmentVariable('LOAD_THUMBNAIL')),
IS_EXPLICIT: getBoolean(getEnvironmentVariable('IS_EXPLICIT')),
Expand Down