discogs-marketplace-api-nodejs 1.8.0 → 1.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.MD +47 -11
- package/dist/interfaces/input.interface.d.ts +11 -0
- package/dist/marketplace.d.ts +10 -8
- package/dist/marketplace.js +176 -115
- package/package.json +2 -1
package/README.MD
CHANGED
|
@@ -4,10 +4,15 @@ Another (better ?) NodeJs library to fetch data from Discogs marketplace. 💿
|
|
|
4
4
|
|
|
5
5
|
## 📂 Installation (with npm)
|
|
6
6
|
|
|
7
|
+
This library works with two different `strategies` to scrape data: with a classic HTTP fetch request (Axios) or by creating a new instance of a browser (Playwright). Take a look at the "[About strategies](#🤠-about-strategies)" chapter to learn more.
|
|
8
|
+
|
|
7
9
|
Run the following command in your project:
|
|
8
10
|
|
|
9
11
|
```sh
|
|
10
12
|
npm install discogs-marketplace-api-nodejs
|
|
13
|
+
|
|
14
|
+
# Or to avoid installing Playwright
|
|
15
|
+
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 npm install discogs-marketplace-api-nodejs
|
|
11
16
|
```
|
|
12
17
|
|
|
13
18
|
## ▶️ Quick Start
|
|
@@ -102,6 +107,31 @@ const result = await DiscogsMarketplace.search({
|
|
|
102
107
|
})
|
|
103
108
|
```
|
|
104
109
|
|
|
110
|
+
## 🤠 About strategies
|
|
111
|
+
|
|
112
|
+
> Which `strategy` should I choose?
|
|
113
|
+
|
|
114
|
+
This project provides two ways to scrape data:
|
|
115
|
+
|
|
116
|
+
- `fetch` (default): Get the result with a classic HTTP fetch request (Axios).
|
|
117
|
+
- For the moment, this is the simplest and fastest solution to choose.
|
|
118
|
+
- `browser`: Get the result by creating a new instance of a browser (Playwright) to scrape the page.
|
|
119
|
+
- This can help bypass Cloudflare (or other) protection for some cases.
|
|
120
|
+
- Slightly slower than `fetch` (see [benchmarks](https://github.com/KirianCaumes/Discogs-Marketplace-API-NodeJS/releases/tag/v1.8.1))
|
|
121
|
+
|
|
122
|
+
**Both strategies will give you the exact same result.**
|
|
123
|
+
|
|
124
|
+
I choose to maintain this two solutions, because over time Discogs is changing: one day they activated a protection that prevented a `fetch` solution from working, so I implemented to `browser` solution. But a few days later, they disabled that protection and the original solution became the best again.
|
|
125
|
+
|
|
126
|
+
With both strategies, you are free to choose the one that suits you!
|
|
127
|
+
|
|
128
|
+
```js
|
|
129
|
+
const result = await DiscogsMarketplace.search({
|
|
130
|
+
// ...
|
|
131
|
+
strategy: 'fetch' // or 'browser'
|
|
132
|
+
})
|
|
133
|
+
```
|
|
134
|
+
|
|
105
135
|
## 📃 Data format
|
|
106
136
|
|
|
107
137
|
You can provide parameters to `DiscogsMarketplace.search` function according to this interface:
|
|
@@ -198,6 +228,12 @@ interface InputInterface {
|
|
|
198
228
|
* Default to `en`.
|
|
199
229
|
*/
|
|
200
230
|
lang?: LangType
|
|
231
|
+
/**
|
|
232
|
+
* Strategy to scrape data:
|
|
233
|
+
* - `fetch` (default): Get the result with a classic HTTP fetch request (Axios).
|
|
234
|
+
* - `browser`: Get the result by creating a new instance of a browser (Playwright) to scrape the page. This can help bypass Cloudflare (or other) protection for some cases.
|
|
235
|
+
*/
|
|
236
|
+
strategy?: 'fetch' | 'browser'
|
|
201
237
|
}
|
|
202
238
|
```
|
|
203
239
|
|
|
@@ -292,16 +328,6 @@ You can open a pull request with your new additions.
|
|
|
292
328
|
|
|
293
329
|
## 🐛 Known issues/problems
|
|
294
330
|
|
|
295
|
-
### Playwright
|
|
296
|
-
|
|
297
|
-
Install system dependencies:
|
|
298
|
-
|
|
299
|
-
```sh
|
|
300
|
-
sudo npx playwright install-deps chromium
|
|
301
|
-
```
|
|
302
|
-
|
|
303
|
-
More information [here](https://playwright.dev/docs/browsers#install-system-dependencies).
|
|
304
|
-
|
|
305
331
|
### Linkedom
|
|
306
332
|
|
|
307
333
|
If you are using this library on a Typescript project, you might encounter issues with `linkedom`.
|
|
@@ -316,4 +342,14 @@ To fix it, use this in command line: `--skipLibCheck` or add that in you `tsconf
|
|
|
316
342
|
}
|
|
317
343
|
```
|
|
318
344
|
|
|
319
|
-
|
|
345
|
+
### Playwright
|
|
346
|
+
|
|
347
|
+
Install system dependencies:
|
|
348
|
+
|
|
349
|
+
```sh
|
|
350
|
+
sudo npx playwright install-deps chromium
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
More information [here](https://playwright.dev/docs/browsers#install-system-dependencies).
|
|
354
|
+
|
|
355
|
+
👉 If you find another problem, feel free to open an issue.
|
|
@@ -75,23 +75,34 @@ interface InputInterface {
|
|
|
75
75
|
seller?: string;
|
|
76
76
|
/**
|
|
77
77
|
* Sort elements by.
|
|
78
|
+
*
|
|
78
79
|
* Default to `Listed Newest`.
|
|
79
80
|
*/
|
|
80
81
|
sort?: SortType;
|
|
81
82
|
/**
|
|
82
83
|
* Limit of elements to search (25 | 50 | 100 | 250).
|
|
84
|
+
*
|
|
83
85
|
* Default to `25`.
|
|
84
86
|
*/
|
|
85
87
|
limit?: LimitType;
|
|
86
88
|
/**
|
|
87
89
|
* Page (Must be < 401 or discogs will return an error 404).
|
|
90
|
+
*
|
|
88
91
|
* Default to `1`.
|
|
89
92
|
*/
|
|
90
93
|
page?: number;
|
|
91
94
|
/**
|
|
92
95
|
* Lang to use for Discogs.
|
|
96
|
+
*
|
|
93
97
|
* Default to `en`.
|
|
94
98
|
*/
|
|
95
99
|
lang?: LangType;
|
|
100
|
+
/**
|
|
101
|
+
* Strategy to scrape data:
|
|
102
|
+
* - `fetch` (default): Get the result with a classic HTTP fetch request (Axios).
|
|
103
|
+
* - `browser`: Get the result by creating a new instance of a browser (Playwright) to scrape the page.
|
|
104
|
+
* This can help bypass Cloudflare (or other) protection for some cases.
|
|
105
|
+
*/
|
|
106
|
+
strategy?: 'fetch' | 'browser';
|
|
96
107
|
}
|
|
97
108
|
export default InputInterface;
|
package/dist/marketplace.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export default abstract class Marketplace {
|
|
|
7
7
|
* Search elements on discogs
|
|
8
8
|
* @returns Items found
|
|
9
9
|
*/
|
|
10
|
-
static search({ searchType, searchValue, currency, genre, style, format, formatDescription, condition, year, years, isAudioSample, isMakeAnOfferOnly, from, seller, sort, limit, page, lang, }: InputInterface): Promise<OutputSuccessInterface>;
|
|
10
|
+
static search({ searchType, searchValue, currency, genre, style, format, formatDescription, condition, year, years, isAudioSample, isMakeAnOfferOnly, from, seller, sort, limit, page, lang, strategy, }: InputInterface): Promise<OutputSuccessInterface>;
|
|
11
11
|
/**
|
|
12
12
|
* Generate URL to be parsed
|
|
13
13
|
* @returns Url
|
|
@@ -20,16 +20,18 @@ export default abstract class Marketplace {
|
|
|
20
20
|
*/
|
|
21
21
|
private static serializeParams;
|
|
22
22
|
/**
|
|
23
|
-
*
|
|
24
|
-
* @param
|
|
25
|
-
* @returns Cleanup string
|
|
23
|
+
* Get result with a classic HTTP fetch request (Axios)
|
|
24
|
+
* @param url Url
|
|
26
25
|
*/
|
|
27
|
-
private static
|
|
26
|
+
private static getResultWithFetch;
|
|
27
|
+
/**
|
|
28
|
+
* Get result by creating a new instance of a navigator (Playwright) to crawl the page
|
|
29
|
+
* @param url Url
|
|
30
|
+
*/
|
|
31
|
+
private static getResultWithBrowser;
|
|
28
32
|
/**
|
|
29
33
|
* Parse HTML to clean result
|
|
30
|
-
* @param document Document to parse
|
|
31
|
-
* @param urlGenerated Url generated
|
|
32
34
|
* @returns Items found
|
|
33
35
|
*/
|
|
34
|
-
private static
|
|
36
|
+
private static getResult;
|
|
35
37
|
}
|
package/dist/marketplace.js
CHANGED
|
@@ -1,20 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
14
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
6
|
const user_agents_1 = __importDefault(require("user-agents"));
|
|
16
|
-
const linkedom_1 = require("linkedom");
|
|
17
7
|
const playwright_chromium_1 = require("playwright-chromium");
|
|
8
|
+
const linkedom_1 = require("linkedom");
|
|
9
|
+
const axios_1 = __importDefault(require("axios"));
|
|
18
10
|
const data_1 = require("./data/index.js");
|
|
19
11
|
/**
|
|
20
12
|
* Discogs Marketplace
|
|
@@ -24,93 +16,60 @@ class Marketplace {
|
|
|
24
16
|
* Search elements on discogs
|
|
25
17
|
* @returns Items found
|
|
26
18
|
*/
|
|
27
|
-
static search({ searchType = 'q', searchValue = undefined, currency = undefined, genre = undefined, style = [], format = [], formatDescription = [], condition = [], year = undefined, years = undefined, isAudioSample = false, isMakeAnOfferOnly = false, from = undefined, seller = undefined, sort = 'Listed Newest', limit = 25, page = 1, lang = 'en', }) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
'media',
|
|
47
|
-
'font',
|
|
48
|
-
'script',
|
|
49
|
-
'texttrack',
|
|
50
|
-
'xhr',
|
|
51
|
-
'fetch',
|
|
52
|
-
'eventsource',
|
|
53
|
-
'websocket',
|
|
54
|
-
'manifest',
|
|
55
|
-
'other',
|
|
56
|
-
].includes(route.request().resourceType())
|
|
57
|
-
? route.abort()
|
|
58
|
-
: route.continue());
|
|
59
|
-
/** Url to call */
|
|
60
|
-
const url = [
|
|
61
|
-
this.generateUrl({ searchType, seller, lang }),
|
|
62
|
-
this.serializeParams({
|
|
63
|
-
[searchType]: searchValue,
|
|
64
|
-
currency,
|
|
65
|
-
genre,
|
|
66
|
-
style: (style === null || style === void 0 ? void 0 : style.length) ? style : undefined,
|
|
67
|
-
format: (format === null || format === void 0 ? void 0 : format.length) ? format : undefined,
|
|
68
|
-
format_desc: (formatDescription === null || formatDescription === void 0 ? void 0 : formatDescription.length) ? formatDescription : undefined,
|
|
69
|
-
condition: (condition === null || condition === void 0 ? void 0 : condition.length) ? condition : undefined,
|
|
70
|
-
year: year && !years ? year : undefined,
|
|
71
|
-
year1: (years === null || years === void 0 ? void 0 : years.min) && !year ? years === null || years === void 0 ? void 0 : years.min : undefined,
|
|
72
|
-
year2: (years === null || years === void 0 ? void 0 : years.max) && !year ? years === null || years === void 0 ? void 0 : years.max : undefined,
|
|
73
|
-
audio: isAudioSample ? 1 : undefined,
|
|
74
|
-
offers: isMakeAnOfferOnly ? 1 : undefined,
|
|
75
|
-
ships_from: (from === null || from === void 0 ? void 0 : from.length) ? from : undefined,
|
|
76
|
-
limit,
|
|
77
|
-
page,
|
|
78
|
-
sort,
|
|
79
|
-
}),
|
|
80
|
-
].join('?');
|
|
81
|
-
/** Init page */
|
|
82
|
-
const response = yield browserPage.goto(url, { waitUntil: 'domcontentloaded' });
|
|
83
|
-
/** Get HTML */
|
|
84
|
-
const bodyHTML = yield browserPage.content();
|
|
85
|
-
/** Close browser */
|
|
86
|
-
yield browser.close();
|
|
87
|
-
/** If error, reject */
|
|
88
|
-
if (((_a = response === null || response === void 0 ? void 0 : response.status()) !== null && _a !== void 0 ? _a : 0) >= 400) {
|
|
89
|
-
// eslint-disable-next-line @typescript-eslint/no-throw-literal
|
|
90
|
-
throw {
|
|
91
|
-
message: (_e = (_d = (_c = (_b = (0, linkedom_1.parseHTML)(bodyHTML).document) === null || _b === void 0 ? void 0 : _b.querySelector('h1 + p')) === null || _c === void 0 ? void 0 : _c.innerHTML) === null || _d === void 0 ? void 0 : _d.trim()) !== null && _e !== void 0 ? _e : 'An error occurred',
|
|
92
|
-
code: (response === null || response === void 0 ? void 0 : response.status()) || 500,
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
return this.getData((0, linkedom_1.parseHTML)(bodyHTML).document, browserPage.url(), {
|
|
96
|
-
searchType,
|
|
97
|
-
searchValue,
|
|
19
|
+
static async search({ searchType = 'q', searchValue = undefined, currency = undefined, genre = undefined, style = [], format = [], formatDescription = [], condition = [], year = undefined, years = undefined, isAudioSample = false, isMakeAnOfferOnly = false, from = undefined, seller = undefined, sort = 'Listed Newest', limit = 25, page = 1, lang = 'en', strategy = 'fetch', }) {
|
|
20
|
+
try {
|
|
21
|
+
/** Url to call */
|
|
22
|
+
const url = [
|
|
23
|
+
this.generateUrl({ searchType, seller, lang }),
|
|
24
|
+
this.serializeParams({
|
|
25
|
+
[searchType]: searchValue,
|
|
26
|
+
currency,
|
|
27
|
+
genre,
|
|
28
|
+
style: (style === null || style === void 0 ? void 0 : style.length) ? style : undefined,
|
|
29
|
+
format: (format === null || format === void 0 ? void 0 : format.length) ? format : undefined,
|
|
30
|
+
format_desc: (formatDescription === null || formatDescription === void 0 ? void 0 : formatDescription.length) ? formatDescription : undefined,
|
|
31
|
+
condition: (condition === null || condition === void 0 ? void 0 : condition.length) ? condition : undefined,
|
|
32
|
+
year: year && !years ? year : undefined,
|
|
33
|
+
year1: (years === null || years === void 0 ? void 0 : years.min) && !year ? years === null || years === void 0 ? void 0 : years.min : undefined,
|
|
34
|
+
year2: (years === null || years === void 0 ? void 0 : years.max) && !year ? years === null || years === void 0 ? void 0 : years.max : undefined,
|
|
35
|
+
audio: isAudioSample ? 1 : undefined,
|
|
36
|
+
offers: isMakeAnOfferOnly ? 1 : undefined,
|
|
37
|
+
ships_from: (from === null || from === void 0 ? void 0 : from.length) ? from : undefined,
|
|
98
38
|
limit,
|
|
99
39
|
page,
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
40
|
+
sort,
|
|
41
|
+
}),
|
|
42
|
+
].join('?');
|
|
43
|
+
/** Result got by different strategy type */
|
|
44
|
+
const result = await (async () => {
|
|
45
|
+
switch (strategy) {
|
|
46
|
+
case 'fetch':
|
|
47
|
+
return this.getResultWithFetch({
|
|
48
|
+
input: { page, limit, searchValue, searchType },
|
|
49
|
+
dependencies: { url },
|
|
50
|
+
});
|
|
51
|
+
case 'browser':
|
|
52
|
+
return this.getResultWithBrowser({
|
|
53
|
+
input: { page, limit, searchValue, searchType },
|
|
54
|
+
dependencies: { url },
|
|
55
|
+
});
|
|
56
|
+
default:
|
|
57
|
+
throw new Error('Unreachable');
|
|
106
58
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
59
|
+
})();
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
if (error.message && error.code) {
|
|
64
|
+
// Rethrow error
|
|
65
|
+
throw error;
|
|
112
66
|
}
|
|
113
|
-
|
|
67
|
+
// eslint-disable-next-line @typescript-eslint/no-throw-literal
|
|
68
|
+
throw {
|
|
69
|
+
message: (error === null || error === void 0 ? void 0 : error.message) || 'An error occurred',
|
|
70
|
+
code: 500,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
114
73
|
}
|
|
115
74
|
/**
|
|
116
75
|
* Generate URL to be parsed
|
|
@@ -162,46 +121,148 @@ class Marketplace {
|
|
|
162
121
|
return param.join('&');
|
|
163
122
|
}
|
|
164
123
|
/**
|
|
165
|
-
*
|
|
166
|
-
* @param
|
|
167
|
-
* @returns Cleanup string
|
|
124
|
+
* Get result with a classic HTTP fetch request (Axios)
|
|
125
|
+
* @param url Url
|
|
168
126
|
*/
|
|
169
|
-
static
|
|
170
|
-
var _a;
|
|
171
|
-
|
|
172
|
-
|
|
127
|
+
static async getResultWithFetch({ input: { page, limit, searchValue, searchType }, dependencies: { url }, }) {
|
|
128
|
+
var _a, _b, _c;
|
|
129
|
+
const { data: bodyHTML, status } = await axios_1.default.get(url, {
|
|
130
|
+
headers: {
|
|
131
|
+
// cspell: disable-next-line
|
|
132
|
+
'X-PJAX': 'true',
|
|
133
|
+
'User-Agent': new user_agents_1.default({ platform: 'Win32' }).toString(),
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
/** Generate document */
|
|
137
|
+
const { document } = (0, linkedom_1.parseHTML)(bodyHTML);
|
|
138
|
+
/** If error, reject */
|
|
139
|
+
if (status >= 400) {
|
|
140
|
+
// eslint-disable-next-line @typescript-eslint/no-throw-literal
|
|
141
|
+
throw {
|
|
142
|
+
message: (_c = (_b = (_a = document === null || document === void 0 ? void 0 : document.querySelector('h1 + p')) === null || _a === void 0 ? void 0 : _a.innerHTML) === null || _b === void 0 ? void 0 : _b.trim()) !== null && _c !== void 0 ? _c : 'An error occurred',
|
|
143
|
+
code: status,
|
|
144
|
+
};
|
|
173
145
|
}
|
|
174
|
-
const
|
|
175
|
-
|
|
176
|
-
|
|
146
|
+
const result = this.getResult({
|
|
147
|
+
input: { page, limit, searchValue, searchType },
|
|
148
|
+
dependencies: { urlGenerated: url, CURRENCIES: data_1.CURRENCIES, COUNTRIES: data_1.COUNTRIES },
|
|
149
|
+
document,
|
|
150
|
+
});
|
|
151
|
+
return result;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Get result by creating a new instance of a navigator (Playwright) to crawl the page
|
|
155
|
+
* @param url Url
|
|
156
|
+
*/
|
|
157
|
+
static async getResultWithBrowser({ input: { page, limit, searchValue, searchType }, dependencies: { url }, }) {
|
|
158
|
+
var _a;
|
|
159
|
+
/** Init browser */
|
|
160
|
+
const browser = await playwright_chromium_1.chromium.launch({
|
|
161
|
+
args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-gl-drawing-for-tests'],
|
|
162
|
+
});
|
|
163
|
+
/** Init context */
|
|
164
|
+
const browserContext = await browser.newContext({
|
|
165
|
+
userAgent: new user_agents_1.default({ platform: 'Win32' }).toString(),
|
|
166
|
+
extraHTTPHeaders: {
|
|
167
|
+
'X-PJAX': 'true',
|
|
168
|
+
},
|
|
169
|
+
javaScriptEnabled: false,
|
|
170
|
+
});
|
|
171
|
+
/** Init page */
|
|
172
|
+
const browserPage = await browserContext.newPage();
|
|
173
|
+
/** Block useless resources */
|
|
174
|
+
await browserPage.route('**/*', route => [
|
|
175
|
+
'stylesheet',
|
|
176
|
+
'image',
|
|
177
|
+
'media',
|
|
178
|
+
'font',
|
|
179
|
+
'script',
|
|
180
|
+
'texttrack',
|
|
181
|
+
'xhr',
|
|
182
|
+
'fetch',
|
|
183
|
+
'eventsource',
|
|
184
|
+
'websocket',
|
|
185
|
+
'manifest',
|
|
186
|
+
'other',
|
|
187
|
+
].includes(route.request().resourceType())
|
|
188
|
+
? route.abort()
|
|
189
|
+
: route.continue());
|
|
190
|
+
/** Init page */
|
|
191
|
+
const response = await browserPage.goto(url, { waitUntil: 'domcontentloaded' });
|
|
192
|
+
/** Status code from the page */
|
|
193
|
+
const status = (response === null || response === void 0 ? void 0 : response.status()) || 500;
|
|
194
|
+
/** If error, reject */
|
|
195
|
+
if (status >= 400) {
|
|
196
|
+
// eslint-disable-next-line @typescript-eslint/no-throw-literal
|
|
197
|
+
throw {
|
|
198
|
+
message: (_a = (await browserPage.evaluate(() => { var _a, _b; return (_b = (_a = document === null || document === void 0 ? void 0 : document.querySelector('h1 + p')) === null || _a === void 0 ? void 0 : _a.innerHTML) === null || _b === void 0 ? void 0 : _b.trim(); }))) !== null && _a !== void 0 ? _a : 'An error occurred',
|
|
199
|
+
code: status,
|
|
200
|
+
};
|
|
177
201
|
}
|
|
178
|
-
|
|
179
|
-
const
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
202
|
+
/** Url generated by browser */
|
|
203
|
+
const urlGenerated = browserPage.url();
|
|
204
|
+
/**
|
|
205
|
+
* Result from scraping.
|
|
206
|
+
* Scraping inside Playwright is faster (x2) compare to exporting HTML outside and parsing it with another library.
|
|
207
|
+
*/
|
|
208
|
+
const result = await browserPage.evaluate(this.getResult, {
|
|
209
|
+
input: {
|
|
210
|
+
page,
|
|
211
|
+
limit,
|
|
212
|
+
searchValue,
|
|
213
|
+
searchType,
|
|
214
|
+
},
|
|
215
|
+
dependencies: {
|
|
216
|
+
urlGenerated,
|
|
217
|
+
CURRENCIES: data_1.CURRENCIES,
|
|
218
|
+
COUNTRIES: data_1.COUNTRIES,
|
|
219
|
+
},
|
|
220
|
+
});
|
|
221
|
+
/** Close context/browser */
|
|
222
|
+
await browserContext.close();
|
|
223
|
+
await browser.close();
|
|
224
|
+
return result;
|
|
184
225
|
}
|
|
185
226
|
/**
|
|
186
227
|
* Parse HTML to clean result
|
|
187
|
-
* @param document Document to parse
|
|
188
|
-
* @param urlGenerated Url generated
|
|
189
228
|
* @returns Items found
|
|
190
229
|
*/
|
|
191
|
-
static
|
|
230
|
+
static getResult({ input: { page, limit, searchValue, searchType }, dependencies: { urlGenerated, CURRENCIES, COUNTRIES }, document = globalThis.document, // Note: this default value will only work with Playwright
|
|
231
|
+
}) {
|
|
192
232
|
var _a, _b, _c, _d, _e;
|
|
233
|
+
/**
|
|
234
|
+
* Function to convert currency to ISO name
|
|
235
|
+
* @param value String to clean
|
|
236
|
+
* @returns Cleanup string
|
|
237
|
+
*/
|
|
238
|
+
const convertCurrency = (value) => {
|
|
239
|
+
var _a;
|
|
240
|
+
if (!value) {
|
|
241
|
+
return value;
|
|
242
|
+
}
|
|
243
|
+
const currencyFound = (_a = Object.keys(CURRENCIES).find(key => value.includes(key))) !== null && _a !== void 0 ? _a : '';
|
|
244
|
+
if (!currencyFound) {
|
|
245
|
+
return value;
|
|
246
|
+
}
|
|
247
|
+
const currencyClean = CURRENCIES[currencyFound];
|
|
248
|
+
const amount = value
|
|
249
|
+
.replace(currencyClean !== 'JPY' ? /[.](?=.*[.])/g : /\./g, '') // Remove all dot but last, except if JPY
|
|
250
|
+
.replace(currencyFound, '') // Remove original currency
|
|
251
|
+
.replace(/\s/g, ''); // Remove spaces
|
|
252
|
+
return `${amount} ${currencyClean}`.replace(/\s\s+/g, ' '); // Remove useless spaces
|
|
253
|
+
};
|
|
193
254
|
const totalItems = parseFloat(((_d = (_c = (_b = (_a = document
|
|
194
255
|
.querySelector('.pagination_total')) === null || _a === void 0 ? void 0 : _a.textContent) === null || _b === void 0 ? void 0 : _b.split(' ')) === null || _c === void 0 ? void 0 : _c.filter(x => x).pop()) === null || _d === void 0 ? void 0 : _d.replace(/(,|\.|\s)/g, '')) || '0');
|
|
195
256
|
return {
|
|
196
257
|
items: ((_e = [...document.querySelectorAll('table.table_block tbody tr')]) === null || _e === void 0 ? void 0 : _e.map(el => {
|
|
197
258
|
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58;
|
|
198
259
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
199
|
-
const shipping =
|
|
260
|
+
const shipping = convertCurrency((_g = (_f = (_e = (_d = (_c = (_b = (_a = el
|
|
200
261
|
.querySelector('.item_shipping')) === null || _a === void 0 ? void 0 : _a.childNodes) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.textContent) === null || _d === void 0 ? void 0 : _d.replace(/(\s+|\+)/g, ' ')) === null || _e === void 0 ? void 0 : _e.trim()) === null || _f === void 0 ? void 0 : _f.replace(',', '.')) !== null && _g !== void 0 ? _g : '');
|
|
201
262
|
const have = Number.parseInt((_j = (_h = el.querySelector('.community_summary .community_result:nth-child(1) .community_number')) === null || _h === void 0 ? void 0 : _h.textContent) !== null && _j !== void 0 ? _j : '', 10);
|
|
202
263
|
const want = Number.parseInt((_l = (_k = el.querySelector('.community_summary .community_result:nth-child(2) .community_number')) === null || _k === void 0 ? void 0 : _k.textContent) !== null && _l !== void 0 ? _l : '', 10);
|
|
203
264
|
const countryName = (_q = (_p = (_o = (_m = el.querySelector('.seller_info li:nth-child(3)')) === null || _m === void 0 ? void 0 : _m.textContent) === null || _o === void 0 ? void 0 : _o.split(':')) === null || _p === void 0 ? void 0 : _p.pop()) !== null && _q !== void 0 ? _q : '';
|
|
204
|
-
const countryCode =
|
|
265
|
+
const countryCode = COUNTRIES[countryName];
|
|
205
266
|
const releaseId = Number.parseInt((_v = (_u = (_t = (_s = (_r = el.querySelector('a.item_release_link')) === null || _r === void 0 ? void 0 : _r.href.split('release/')) === null || _s === void 0 ? void 0 : _s.pop()) === null || _t === void 0 ? void 0 : _t.split('-')) === null || _u === void 0 ? void 0 : _u.shift()) !== null && _v !== void 0 ? _v : '0', 10);
|
|
206
267
|
const itemId = Number.parseInt((_y = (_x = (_w = el.querySelector('a.item_description_title')) === null || _w === void 0 ? void 0 : _w.href) === null || _x === void 0 ? void 0 : _x.split('/').pop()) !== null && _y !== void 0 ? _y : '0', 10);
|
|
207
268
|
const notes = Number.parseInt((_5 = (_4 = (_3 = (_2 = (_1 = (_0 = (_z = el
|
|
@@ -249,7 +310,7 @@ class Marketplace {
|
|
|
249
310
|
notes: Number.isNaN(notes) ? 0 : notes,
|
|
250
311
|
},
|
|
251
312
|
price: {
|
|
252
|
-
base:
|
|
313
|
+
base: convertCurrency((_57 = (_56 = (_55 = (_54 = el.querySelector('.price')) === null || _54 === void 0 ? void 0 : _54.textContent) === null || _55 === void 0 ? void 0 : _55.replace(/\s+/g, ' ')) === null || _56 === void 0 ? void 0 : _56.replace(/,/, '.')) !== null && _57 !== void 0 ? _57 : ''),
|
|
253
314
|
shipping: Number.isNaN(parseFloat(shipping)) ? null : shipping,
|
|
254
315
|
},
|
|
255
316
|
country: {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "discogs-marketplace-api-nodejs",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.0",
|
|
4
4
|
"description": "Another (better ?) NodeJs library to fetch data from Discogs marketplace",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"discogs",
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
"node": ">=14.0.0"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
+
"axios": "^1.6.2",
|
|
36
37
|
"linkedom": "^0.16.4",
|
|
37
38
|
"playwright-chromium": "^1.40.0",
|
|
38
39
|
"user-agents": "^1.1.24"
|