Skip to content

Commit 929aa86

Browse files
authored
Merge pull request #2 from ScrapeGraphAI/feat/add-time-range-to-searchscraper
feat: add timeRange option to searchScraper for date-filtered search
2 parents d81242c + 2ccf57d commit 929aa86

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,35 @@ const prompt = 'Best restaurants near me';
438438
})();
439439
```
440440

441+
#### Time Range Filter
442+
443+
Use `timeRange` to filter search results by date range:
444+
445+
```javascript
446+
import { searchScraper } from 'scrapegraph-js';
447+
448+
const apiKey = 'your-api-key';
449+
const prompt = 'Latest news about AI';
450+
451+
(async () => {
452+
try {
453+
const response = await searchScraper(apiKey, prompt, 5, null, null, {
454+
timeRange: 'past_week' // Only results from the past week
455+
});
456+
console.log(response.result);
457+
} catch (error) {
458+
console.error('Error:', error);
459+
}
460+
})();
461+
```
462+
463+
Available time range options:
464+
- `past_hour` - Results from the past hour
465+
- `past_24_hours` - Results from the past 24 hours
466+
- `past_week` - Results from the past week
467+
- `past_month` - Results from the past month
468+
- `past_year` - Results from the past year
469+
441470
### Crawl API
442471

443472
Start a crawl job to extract structured data from a website and its linked pages, using a custom schema.
@@ -1145,6 +1174,7 @@ Searches and extracts information from multiple web sources using AI.
11451174
- `extractionMode` (boolean): Whether to use AI extraction
11461175
- `renderHeavyJs` (boolean): Whether to render heavy JavaScript
11471176
- `locationGeoCode` (string): The geo code of the location to search in (e.g., "us", "gb", "de")
1177+
- `timeRange` (string): The date range to filter results. Options: "past_hour", "past_24_hours", "past_week", "past_month", "past_year"
11481178
- `mock` (boolean): Override mock mode for this request
11491179

11501180
**Returns:** Promise that resolves to an object containing:

src/searchScraper.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ import { getMockResponse } from './utils/mockResponse.js';
2222
* AI extraction costs 10 credits per page, markdown conversion costs 2 credits per page.
2323
* @param {boolean} [options.stealth=false] - Enable stealth mode to avoid bot detection
2424
* @param {string} [options.locationGeoCode=null] - The geo code of the location to search in (e.g., "us", "gb", "de")
25+
* @param {string} [options.timeRange=null] - The date range to search in. Valid values: "past_hour", "past_24_hours", "past_week", "past_month", "past_year"
2526
* @returns {Promise<string>} Extracted data in JSON format matching the provided schema
2627
* @throws - Will throw an error in case of an HTTP failure.
2728
*/
2829
export async function searchScraper(apiKey, prompt, numResults = 3, schema = null, userAgent = null, options = {}) {
29-
const { mock = null, renderHeavyJs = false, extractionMode = true, stealth = false, locationGeoCode = null } = options;
30+
const { mock = null, renderHeavyJs = false, extractionMode = true, stealth = false, locationGeoCode = null, timeRange = null } = options;
3031

3132
// Check if mock mode is enabled
3233
const useMock = mock !== null ? mock : isMockEnabled();
@@ -66,6 +67,10 @@ export async function searchScraper(apiKey, prompt, numResults = 3, schema = nul
6667
payload.location_geo_code = locationGeoCode;
6768
}
6869

70+
if (timeRange) {
71+
payload.time_range = timeRange;
72+
}
73+
6974
if (schema) {
7075
if (schema instanceof ZodType) {
7176
payload.output_schema = zodToJsonSchema(schema);

0 commit comments

Comments
 (0)