Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.
TypeScript apifyautomationcrawlercrawlingheadlessheadless-chromejavascriptnodejsnpmplaywrightpuppeteerscraperscrapingtypescriptweb-crawlerweb-crawlingweb-scraping
Added country parameter to getApifyProxyUrl function.
Petr Pateka840d08d4c0f
diff --git a/src/actor.js b/src/actor.jsindex 0f880ffc6..2db1e4155 100644--- a/src/actor.js+++ b/src/actor.js@@ -4,7 +4,7 @@ import log from 'apify-shared/log';import { checkParamOrThrow } from 'apify-client/build/utils';import { APIFY_PROXY_VALUE_REGEX } from 'apify-shared/regexs';import { ENV_VARS, INTEGER_ENV_VARS, LOCAL_ENV_VARS, ACT_JOB_TERMINAL_STATUSES, ACT_JOB_STATUSES } from 'apify-shared/consts';-import { EXIT_CODES } from './constants';+import { EXIT_CODES, COUNTRY_CODE_REGEX } from './constants';import { initializeEvents, stopEvents } from './events';import { apifyClient, addCharsetToContentType, sleep, snakeCaseToCamelCase, isAtHome } from './utils';import { maybeStringify } from './key_value_store';@@ -733,6 +733,7 @@ export const getApifyProxyUrl = (options = {}) => {const {groups,session,+ country,password = process.env[ENV_VARS.PROXY_PASSWORD],hostname = process.env[ENV_VARS.PROXY_HOSTNAME] || LOCAL_ENV_VARS[ENV_VARS.PROXY_HOSTNAME],port = parseInt(process.env[ENV_VARS.PROXY_PORT] || LOCAL_ENV_VARS[ENV_VARS.PROXY_PORT], 10),@@ -741,22 +742,27 @@ export const getApifyProxyUrl = (options = {}) => {// parameters so we need to override this in error messages.groupsParamName = 'opts.groups',sessionParamName = 'opts.session',+ countryParamName = 'opts.country',} = options;const getMissingParamErrorMgs = (param, env) => `Apify Proxy ${param} must be provided as parameter or "${env}" environment variable!`;const throwInvalidProxyValueError = (param) => {throw new Error(`The "${param}" option can only contain the following characters: 0-9, a-z, A-Z, ".", "_" and "~"`);};+ const throwInvalidCountryCode = (code) => {+ throw new Error(`The "${code}" option must be a valid two letter country code according to ISO 3166-1 alpha-2`);+ };checkParamOrThrow(groups, groupsParamName, 'Maybe [String]');checkParamOrThrow(session, sessionParamName, 'Maybe Number | String');+ checkParamOrThrow(country, countryParamName, 'Maybe String');checkParamOrThrow(password, 'opts.password', 'String', getMissingParamErrorMgs('password', ENV_VARS.PROXY_PASSWORD));checkParamOrThrow(hostname, 'opts.hostname', 'String', getMissingParamErrorMgs('hostname', ENV_VARS.PROXY_HOSTNAME));checkParamOrThrow(port, 'opts.port', 'Number', getMissingParamErrorMgs('port', ENV_VARS.PROXY_PORT));let username;- if (groups || session) {+ if (groups || session || country) {const parts = [];if (groups && groups.length) {@@ -767,6 +773,10 @@ export const getApifyProxyUrl = (options = {}) => {if (!APIFY_PROXY_VALUE_REGEX.test(session)) throwInvalidProxyValueError('session');parts.push(`session-${session}`);}+ if (country) {+ if (!COUNTRY_CODE_REGEX.test(country)) throwInvalidCountryCode(country);+ parts.push(`country-${country}`);+ }username = parts.join(',');} else {diff --git a/src/constants.js b/src/constants.jsindex 94a0f00b0..fd0ed84d1 100644--- a/src/constants.js+++ b/src/constants.js@@ -121,3 +121,5 @@ export const APIFY_API_BASE_URL = 'https://api.apify.com/v2';* @type {number}*/export const BASIC_CRAWLER_TIMEOUT_MULTIPLIER = 10;++export const COUNTRY_CODE_REGEX = /^[A-Z]{2}$/;diff --git a/test/actor.js b/test/actor.jsindex 2b127bfb8..61af8c3bd 100644--- a/test/actor.js+++ b/test/actor.js@@ -1046,6 +1046,12 @@ describe('Apify.getApifyProxyUrl()', () => {process.env[ENV_VARS.PROXY_HOSTNAME] = 'my.host.com';process.env[ENV_VARS.PROXY_PORT] = 123;+ expect(Apify.getApifyProxyUrl({+ session: 'XYZ',+ groups: ['g1', 'g2', 'g3'],+ country: 'US',+ })).to.be.eql('http://groups-g1+g2+g3,session-XYZ,country-US:[email protected]:123');+expect(Apify.getApifyProxyUrl({session: 'XYZ',groups: ['g1', 'g2', 'g3'],@@ -1059,8 +1065,11 @@ describe('Apify.getApifyProxyUrl()', () => {session: 'XYZ',})).to.be.eql('http://session-XYZ:[email protected]:123');+ expect(Apify.getApifyProxyUrl({ country: 'US' })).to.be.eql('http://country-US:[email protected]:123');+expect(Apify.getApifyProxyUrl()).to.be.eql('http://auto:[email protected]:123');+delete process.env[ENV_VARS.PROXY_PASSWORD];delete process.env[ENV_VARS.PROXY_HOSTNAME];delete process.env[ENV_VARS.PROXY_PORT];@@ -1142,6 +1151,16 @@ describe('Apify.getApifyProxyUrl()', () => {expect(() => Apify.getApifyProxyUrl({ groups: ['ffff', 'fff', 'cc$c'] })).to.throw();expect(() => Apify.getApifyProxyUrl({ apifyProxyGroups: [new Date()] })).to.throw();++ expect(() => Apify.getApifyProxyUrl({ country: new Date() })).to.throw();+ expect(() => Apify.getApifyProxyUrl({ country: 'aa' })).to.throw();+ expect(() => Apify.getApifyProxyUrl({ country: 'aB' })).to.throw();+ expect(() => Apify.getApifyProxyUrl({ country: 'Ba' })).to.throw();+ expect(() => Apify.getApifyProxyUrl({ country: '11' })).to.throw();+ expect(() => Apify.getApifyProxyUrl({ country: 'DDDD' })).to.throw();+ expect(() => Apify.getApifyProxyUrl({ country: 'dddd' })).to.throw();+ expect(() => Apify.getApifyProxyUrl({ country: 1111 })).to.throw();+delete process.env[ENV_VARS.PROXY_PASSWORD];delete process.env[ENV_VARS.PROXY_HOSTNAME];delete process.env[ENV_VARS.PROXY_PORT];