Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
58 changes: 58 additions & 0 deletions src/__tests__/commands/scrape.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,64 @@ describe('executeScrape', () => {
maxAge: 3600,
});
});

it('should include location parameter with country and languages', async () => {
const mockResponse = { markdown: '# Test' };
mockClient.scrape.mockResolvedValue(mockResponse);

await executeScrape({
url: 'https://example.com',
location: { country: 'US', languages: ['en'] },
});

expect(mockClient.scrape).toHaveBeenCalledWith('https://example.com', {
formats: ['markdown'],
location: { country: 'US', languages: ['en'] },
});
});

it('should include location parameter with only country', async () => {
const mockResponse = { markdown: '# Test' };
mockClient.scrape.mockResolvedValue(mockResponse);

await executeScrape({
url: 'https://example.com',
location: { country: 'DE' },
});

expect(mockClient.scrape).toHaveBeenCalledWith('https://example.com', {
formats: ['markdown'],
location: { country: 'DE' },
});
});

it('should include location parameter with only languages', async () => {
const mockResponse = { markdown: '# Test' };
mockClient.scrape.mockResolvedValue(mockResponse);

await executeScrape({
url: 'https://example.com',
location: { languages: ['en', 'es'] },
});

expect(mockClient.scrape).toHaveBeenCalledWith('https://example.com', {
formats: ['markdown'],
location: { languages: ['en', 'es'] },
});
});

it('should not include location parameter when not provided', async () => {
const mockResponse = { markdown: '# Test' };
mockClient.scrape.mockResolvedValue(mockResponse);

await executeScrape({
url: 'https://example.com',
});

expect(mockClient.scrape).toHaveBeenCalledWith('https://example.com', {
formats: ['markdown'],
});
});
});

describe('Response handling', () => {
Expand Down
64 changes: 64 additions & 0 deletions src/__tests__/utils/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,5 +347,69 @@ describe('Option Parsing Utilities', () => {
timing: true,
});
});

it('should parse location with country and languages', () => {
const options = {
url: 'https://example.com',
country: 'US',
languages: 'en,es',
};

const result = parseScrapeOptions(options);

expect(result.location).toEqual({
country: 'US',
languages: ['en', 'es'],
});
});

it('should parse location with only country', () => {
const options = {
url: 'https://example.com',
country: 'DE',
};

const result = parseScrapeOptions(options);

expect(result.location).toEqual({
country: 'DE',
});
});

it('should parse location with only languages', () => {
const options = {
url: 'https://example.com',
languages: 'en,fr,de',
};

const result = parseScrapeOptions(options);

expect(result.location).toEqual({
languages: ['en', 'fr', 'de'],
});
});

it('should not set location when neither country nor languages provided', () => {
const options = {
url: 'https://example.com',
};

const result = parseScrapeOptions(options);

expect(result.location).toBeUndefined();
});

it('should trim whitespace from language codes', () => {
const options = {
url: 'https://example.com',
languages: ' en , es , fr ',
};

const result = parseScrapeOptions(options);

expect(result.location).toEqual({
languages: ['en', 'es', 'fr'],
});
});
});
});
6 changes: 6 additions & 0 deletions src/commands/scrape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
ScrapeOptions,
ScrapeResult,
ScrapeFormat,
ScrapeLocation,
} from '../types/scrape';
import { getClient } from '../utils/client';
import { handleScrapeOutput } from '../utils/output';
Expand Down Expand Up @@ -77,6 +78,7 @@ export async function executeScrape(
includeTags?: string[];
excludeTags?: string[];
maxAge?: number;
location?: ScrapeLocation;
} = {
formats,
};
Expand All @@ -101,6 +103,10 @@ export async function executeScrape(
scrapeParams.maxAge = options.maxAge;
}

if (options.location) {
scrapeParams.location = options.location;
}

// Execute scrape with timing - only wrap the scrape call in try-catch
const requestStartTime = Date.now();

Expand Down
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ function createScrapeCommand(): Command {
'Maximum age of cached content in milliseconds',
parseInt
)
.option(
'--country <code>',
'ISO country code for geo-targeted scraping (e.g., US, DE, BR)'
)
.option(
'--languages <codes>',
'Comma-separated language codes for scraping (e.g., en,es)'
)
.action(async (positionalUrl, positionalFormats, options) => {
// Use positional URL if provided, otherwise use --url option
const url = positionalUrl || options.url;
Expand Down
9 changes: 9 additions & 0 deletions src/types/scrape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ export type ScrapeFormat =
| 'attributes'
| 'branding';

export interface ScrapeLocation {
/** ISO 3166-1 alpha-2 country code (e.g., 'US', 'DE', 'BR') */
country?: string;
/** List of language codes (e.g., ['en', 'es']) */
languages?: string[];
}

export interface ScrapeOptions {
/** URL to scrape */
url: string;
Expand Down Expand Up @@ -44,6 +51,8 @@ export interface ScrapeOptions {
timing?: boolean;
/** Maximum age of cached content in milliseconds (API-level caching) */
maxAge?: number;
/** Location settings for geo-targeted scraping */
location?: ScrapeLocation;
}

export interface ScrapeResult {
Expand Down
21 changes: 20 additions & 1 deletion src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
* Option parsing utilities
*/

import type { ScrapeOptions, ScrapeFormat } from '../types/scrape';
import type {
ScrapeOptions,
ScrapeFormat,
ScrapeLocation,
} from '../types/scrape';

/**
* Valid scrape format values
Expand Down Expand Up @@ -72,6 +76,20 @@ export function parseScrapeOptions(options: any): ScrapeOptions {
formats = parseFormats(options.format);
}

// Build location object if country or languages are provided
let location: ScrapeLocation | undefined;
if (options.country || options.languages) {
location = {};
if (options.country) {
location.country = options.country;
}
if (options.languages) {
location.languages = options.languages
.split(',')
.map((l: string) => l.trim());
}
}

return {
url: options.url,
formats,
Expand All @@ -91,5 +109,6 @@ export function parseScrapeOptions(options: any): ScrapeOptions {
json: options.json,
timing: options.timing,
maxAge: options.maxAge,
location,
};
}