Skip to content

Commit

Permalink
revert: allow users to still use USER_AGENT
Browse files Browse the repository at this point in the history
Removed from documentation, but older users can utilize
until fully removed in 4.0.0
  • Loading branch information
jef committed Dec 11, 2020
1 parent 7bfcc81 commit 3386e8f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,6 +3,7 @@
.vscode/
build/
node_modules/
src/config/*.yaml

.env
dotenv
Expand Down
3 changes: 2 additions & 1 deletion src/config.ts
Expand Up @@ -173,7 +173,8 @@ const browser = {
process.env.PAGE_SLEEP_MAX,
5000
),
open: envOrBoolean(process.env.OPEN_BROWSER)
open: envOrBoolean(process.env.OPEN_BROWSER),
userAgent: ''
};

const docker = envOrBoolean(process.env.DOCKER);
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Expand Up @@ -48,6 +48,8 @@ async function main() {
headless: config.browser.isHeadless
});

config.browser.userAgent = await browser.userAgent();

for (const store of storeList.values()) {
logger.debug('store links', {meta: {links: store.links}});
if (store.setupAction !== undefined) {
Expand Down
2 changes: 1 addition & 1 deletion src/store/lookup.ts
Expand Up @@ -163,7 +163,7 @@ async function lookup(browser: Browser, store: Store) {
await page.setRequestInterception(true);

page.setDefaultNavigationTimeout(config.page.timeout);
await page.setUserAgent(await getRandomUserAgent(browser));
await page.setUserAgent(await getRandomUserAgent());

let adBlockRequestHandler: any;
let pageProxy;
Expand Down
19 changes: 16 additions & 3 deletions src/util.ts
Expand Up @@ -58,7 +58,7 @@ export async function usingPage<T>(
): Promise<T> {
const page = await browser.newPage();
page.setDefaultNavigationTimeout(config.page.timeout);
await page.setUserAgent(await getRandomUserAgent(browser));
await page.setUserAgent(await getRandomUserAgent());

try {
return await cb(page, browser);
Expand All @@ -79,11 +79,24 @@ export async function closePage(page: Page) {
await page.close();
}

export async function getRandomUserAgent(browser: Browser): Promise<string> {
export async function getRandomUserAgent(): Promise<string> {
const deprecatedUserAgent = (process.env.USER_AGENT
? process.env.USER_AGENT.includes('\n')
? process.env.USER_AGENT.split('\n')
: process.env.USER_AGENT.split(',')
: []
).map((s) => s.trim());

if (deprecatedUserAgent.length > 0) {
return deprecatedUserAgent[
Math.floor(Math.random() * deprecatedUserAgent.length)
];
}

const userAgent =
getRandom((ua) => {
return ua.browserName === 'Chrome' && ua.browserVersion > '20';
}) ?? (await browser.userAgent());
}) ?? config.browser.userAgent;

logger.debug('user agent', userAgent);

Expand Down

2 comments on commit 3386e8f

@Vuand
Copy link

@Vuand Vuand commented on 3386e8f Dec 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this remove capatcha, 503 and 403 errors? How do I fix this?

Screenshot (2)

@jef
Copy link
Owner Author

@jef jef commented on 3386e8f Dec 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is another commit past this one. I would try to pull latest. It should help out.

Please sign in to comment.