discogs-marketplace-api-nodejs 1.7.3 → 1.8.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 CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Another (better ?) NodeJs library to fetch data from Discogs marketplace. 💿
4
4
 
5
- ## Installation (with npm)
5
+ ## 📂 Installation (with npm)
6
6
 
7
7
  Run the following command in your project:
8
8
 
@@ -10,7 +10,7 @@ Run the following command in your project:
10
10
  npm install discogs-marketplace-api-nodejs
11
11
  ```
12
12
 
13
- ## Quick Start
13
+ ## ▶️ Quick Start
14
14
 
15
15
  Import `DiscogsMarketplace` into your project:
16
16
 
@@ -22,7 +22,7 @@ import { DiscogsMarketplace } from 'discogs-marketplace-api-nodejs'
22
22
  const DiscogsMarketplace = require('discogs-marketplace-api-nodejs').DiscogsMarketplace
23
23
  ```
24
24
 
25
- ## Examples
25
+ ## 🤔 Examples
26
26
 
27
27
  - Get top 250 items, of the 2nd page, filtered by Rock, listed by newest, for a given artist
28
28
 
@@ -102,7 +102,7 @@ const result = await DiscogsMarketplace.search({
102
102
  })
103
103
  ```
104
104
 
105
- ## Data format
105
+ ## 📃 Data format
106
106
 
107
107
  You can provide parameters to `DiscogsMarketplace.search` function according to this interface:
108
108
 
@@ -278,7 +278,7 @@ interface OutputErrorInterface {
278
278
  }
279
279
  ```
280
280
 
281
- ## How to contribute
281
+ ## 💡 How to contribute
282
282
 
283
283
  There is a [devcontainer](https://code.visualstudio.com/docs/devcontainers/containers) on that project already configured, feel free to use it.
284
284
 
@@ -290,7 +290,17 @@ npm install
290
290
 
291
291
  You can open a pull request with your new additions.
292
292
 
293
- ## Know issues
293
+ ## 🐛 Known issues/problems
294
+
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).
294
304
 
295
305
  ### Linkedom
296
306
 
@@ -14,7 +14,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const user_agents_1 = __importDefault(require("user-agents"));
16
16
  const linkedom_1 = require("linkedom");
17
- const axios_1 = __importDefault(require("axios"));
17
+ const playwright_chromium_1 = require("playwright-chromium");
18
18
  const data_1 = require("./data/index.js");
19
19
  /**
20
20
  * Discogs Marketplace
@@ -25,13 +25,41 @@ class Marketplace {
25
25
  * @returns Items found
26
26
  */
27
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
- var _a, _b, _c, _d;
28
+ var _a, _b, _c, _d, _e;
29
29
  return __awaiter(this, void 0, void 0, function* () {
30
30
  try {
31
- const config = {
32
- url: this.generateUrl({ searchType, seller, lang }),
33
- method: 'GET',
34
- params: {
31
+ /** Init browser */
32
+ const browser = yield playwright_chromium_1.chromium.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] });
33
+ /** Init context */
34
+ const browserContext = yield browser.newContext({
35
+ userAgent: new user_agents_1.default({ platform: 'Win32' }).toString(),
36
+ extraHTTPHeaders: {
37
+ 'X-PJAX': 'true',
38
+ },
39
+ });
40
+ /** Init page */
41
+ const browserPage = yield browserContext.newPage();
42
+ /** Block useless resources */
43
+ yield browserPage.route('**/*', route => [
44
+ 'stylesheet',
45
+ 'image',
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({
35
63
  [searchType]: searchValue,
36
64
  currency,
37
65
  genre,
@@ -48,37 +76,37 @@ class Marketplace {
48
76
  limit,
49
77
  page,
50
78
  sort,
51
- },
52
- paramsSerializer: {
53
- serialize: this.serializeParams,
54
- },
55
- headers: {
56
- // cspell: disable-next-line
57
- 'X-PJAX': 'true',
58
- 'User-Agent': new user_agents_1.default({ platform: 'Win32' }).toString(),
59
- },
60
- };
61
- const { data: bodyHTML } = yield axios_1.default.request(config);
62
- return this.getData((0, linkedom_1.parseHTML)(bodyHTML).document, axios_1.default.getUri(config), {
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(), {
63
96
  searchType,
64
97
  searchValue,
65
98
  limit,
66
99
  page,
67
100
  });
68
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
69
101
  }
70
102
  catch (error) {
71
- const { response, message } = error;
72
- if ((response === null || response === void 0 ? void 0 : response.data) && response.status) {
73
- // eslint-disable-next-line @typescript-eslint/no-throw-literal
74
- throw {
75
- message: (_d = (_c = (_b = (_a = (0, linkedom_1.parseHTML)(response.data).document) === null || _a === void 0 ? void 0 : _a.querySelector('h1 + p')) === null || _b === void 0 ? void 0 : _b.innerHTML) === null || _c === void 0 ? void 0 : _c.trim()) !== null && _d !== void 0 ? _d : 'An error occurred',
76
- code: response.status,
77
- };
103
+ if (error.message && error.code) {
104
+ // Rethrow error
105
+ throw error;
78
106
  }
79
107
  // eslint-disable-next-line @typescript-eslint/no-throw-literal
80
108
  throw {
81
- message: message || 'An error occurred',
109
+ message: (error === null || error === void 0 ? void 0 : error.message) || 'An error occurred',
82
110
  code: 500,
83
111
  };
84
112
  }
@@ -163,7 +191,7 @@ class Marketplace {
163
191
  static getData(document, urlGenerated, { searchType = 'q', searchValue = undefined, limit = 25, page = 1 }) {
164
192
  var _a, _b, _c, _d, _e;
165
193
  const totalItems = parseFloat(((_d = (_c = (_b = (_a = document
166
- .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');
194
+ .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');
167
195
  return {
168
196
  items: ((_e = [...document.querySelectorAll('table.table_block tbody tr')]) === null || _e === void 0 ? void 0 : _e.map(el => {
169
197
  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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "discogs-marketplace-api-nodejs",
3
- "version": "1.7.3",
3
+ "version": "1.8.0",
4
4
  "description": "Another (better ?) NodeJs library to fetch data from Discogs marketplace",
5
5
  "keywords": [
6
6
  "discogs",
@@ -33,8 +33,8 @@
33
33
  "node": ">=14.0.0"
34
34
  },
35
35
  "dependencies": {
36
- "axios": "^1.6.2",
37
36
  "linkedom": "^0.16.4",
37
+ "playwright-chromium": "^1.40.0",
38
38
  "user-agents": "^1.1.24"
39
39
  },
40
40
  "devDependencies": {