From fae39f765b806f166c25b0c72eb0f086a60d2d86 Mon Sep 17 00:00:00 2001 From: Lewis Donovan <23400003+lewisdonovan@users.noreply.github.com> Date: Sun, 27 Oct 2024 20:25:58 +0100 Subject: [PATCH] Add limit prop to config. --- README.md | 5 +++++ src/index.ts | 6 ++++-- src/types.ts | 2 ++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8434f9d..1ca90ba 100644 --- a/README.md +++ b/README.md @@ -147,6 +147,11 @@ Whether or not Puppeteer should run in [headless mode](https://www.browserstack. Defaults to `true` +#### limit +The total number of articles that you would like to be returned. Please note that with higher numbers, the actual returned number may be lower. Typically the max is `99`, but it varies depending on many variables in Puppeteer (such as rate limiting, network conditions etc.). + +Defaults to `99` + ## TypeScript 💙 Google News Scraper includes full [TypeScript](https://typescriptlang.org/) definitions. diff --git a/src/index.ts b/src/index.ts index b34d125..0af19c2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,7 +24,8 @@ const googleNewsScraper = async (userConfig: GNSUserConfig) => { puppeteerHeadlessMode: true, logLevel: 'error', timeframe: '7d', - queryVars: {}, + queryVars: {}, + limit: 99 }, ...userConfig, } as GNSConfig; @@ -133,7 +134,8 @@ const googleNewsScraper = async (userConfig: GNSUserConfig) => { await page.close(); await browser.close() - return results.filter(result => result.title) + const filtered = results.filter(result => result.title); + return config.limit < results.length ? filtered.slice(0, config.limit) : filtered; } diff --git a/src/types.ts b/src/types.ts index 71f8f74..cd70066 100644 --- a/src/types.ts +++ b/src/types.ts @@ -18,6 +18,7 @@ export type GNSUserConfig = SearchParams & { logLevel?: LogLevel; queryVars?: QueryVars; filterWords?: string[]; + limit?: number; }; export type GNSConfig = SearchParams & { @@ -29,6 +30,7 @@ export type GNSConfig = SearchParams & { logLevel: LogLevel; queryVars: QueryVars; filterWords?: string[]; + limit: number; }; export type Article = {