Skip to content

Commit

Permalink
Retry functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
israfil-miya committed Aug 1, 2024
1 parent fc8339f commit 4b40475
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ This method scrapes data from a website based on the provided configuration.
#### Parameters:
The method takes a single parameter:
- `link: string`: The absolute URI of the item you want to scrape.
- `timeoutAmount?: number`: Timeout amount for the request in seconds.

#### Usage:
```ts
Expand Down
45 changes: 20 additions & 25 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import axios, { AxiosError } from 'axios';
import axiosRetry from 'axios-retry';
import { JSDOM } from 'jsdom';

// Change to require if axios-retry import is problematic
const axiosRetry = require('axios-retry').default;

import {
titleCase,
setHeader,
Expand Down Expand Up @@ -35,8 +37,6 @@ export class EshopScraper {
readonly _currencyMap: Map<string[], string>;
readonly _headers: { [key: string]: string }[];
readonly _retry: number;
private _abortController: AbortController;
private _timeout: NodeJS.Timeout | undefined;

// Constructor
constructor({
Expand All @@ -48,7 +48,6 @@ export class EshopScraper {
retry = 2,
}: EshopScraperOptions = {}) {
this._timeoutAmount = timeout * 1000;
this._abortController = new AbortController();

this._webProps =
webProps instanceof Map
Expand Down Expand Up @@ -94,16 +93,18 @@ export class EshopScraper {
});
}

private clearTimeouts() {
if (this._timeout) {
clearTimeout(this._timeout);
this._timeout = undefined;
}
this._abortController.abort();
}

// Method to get data
async getData(link: string): Promise<ResultData> {
async getData(link: string, timeoutAmount?: number): Promise<ResultData> {
const abortController = new AbortController();
timeoutAmount =
typeof timeoutAmount === 'number'
? timeoutAmount * 1000
: this._timeoutAmount;

const timeout = setTimeout(() => {
abortController.abort();
}, timeoutAmount);

try {
if (typeof link !== 'string') {
throw new Error('Link is not provided in the first parameter');
Expand All @@ -119,21 +120,15 @@ export class EshopScraper {
throw new Error(propsData.errorMsg);
}

this._timeout = setTimeout(() => {
this._abortController.abort();
}, this._timeoutAmount);

const { data } = await axios.get(propsData.link, {
signal: this._abortController.signal,
headers: setHeader(this._headers), // Apply random headers
signal: abortController.signal,
headers: setHeader(this._headers),
timeout: this._timeoutAmount,
});
this.clearTimeouts();

let dom: JSDOM = new JSDOM(data, {
runScripts: 'dangerously',
resources: 'usable',
});
clearTimeout(timeout);

let dom: JSDOM = new JSDOM(data);

const priceElement = propsData.selectors.priceSelector
? $(dom, propsData.selectors.priceSelector)
Expand Down Expand Up @@ -165,7 +160,7 @@ export class EshopScraper {
link,
};
} catch (err: any) {
this.clearTimeouts();
clearTimeout(timeout);
if (axios.isAxiosError(err)) {
return {
isError: true,
Expand Down
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"sourceMap": true,
"removeComments": true,
"noImplicitAny": false,
"declaration": true
"declaration": true,
"esModuleInterop": true,
"skipLibCheck": true
},
"include": ["src"],
"exclude": ["node_modules"]
Expand Down
2 changes: 2 additions & 0 deletions tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ export default defineConfig({
shims: true,
skipNodeModulesBundle: true,
clean: true,
sourcemap: true,
target: 'esnext', // Add this line
});

0 comments on commit 4b40475

Please sign in to comment.