discogs-marketplace-api-nodejs 1.10.0 → 1.11.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.
Files changed (37) hide show
  1. package/README.MD +47 -39
  2. package/dist/data/{countries.data.d.ts → country.data.d.ts} +5 -2
  3. package/dist/data/{countries.data.js → country.data.js} +7 -4
  4. package/dist/data/{currencies.data.d.ts → currency.data.d.ts} +5 -2
  5. package/dist/data/{currencies.data.js → currency.data.js} +2 -2
  6. package/dist/index.d.ts +11 -3
  7. package/dist/index.js +6 -2
  8. package/dist/interfaces/{input.interface.d.ts → search-params.interface.d.ts} +39 -25
  9. package/dist/interfaces/{output-success.interface.d.ts → search-result.interface.d.ts} +19 -17
  10. package/dist/scrape.d.ts +10 -0
  11. package/dist/scrape.js +126 -0
  12. package/dist/search.d.ts +11 -0
  13. package/dist/search.js +154 -0
  14. package/dist/types/condition.type.d.ts +1 -2
  15. package/dist/types/currency.type.d.ts +1 -2
  16. package/dist/types/format-description.type.d.ts +1 -2
  17. package/dist/types/format.type.d.ts +1 -2
  18. package/dist/types/from.type.d.ts +1 -2
  19. package/dist/types/genre.type.d.ts +1 -2
  20. package/dist/types/lang.type.d.ts +1 -2
  21. package/dist/types/limit.type.d.ts +1 -2
  22. package/dist/types/search-type.type.d.ts +1 -2
  23. package/dist/types/sort.type.d.ts +1 -2
  24. package/dist/types/style.type.d.ts +1 -2
  25. package/package.json +25 -23
  26. package/dist/data/index.d.ts +0 -3
  27. package/dist/data/index.js +0 -10
  28. package/dist/interfaces/index.d.ts +0 -4
  29. package/dist/interfaces/output-error.interface.d.ts +0 -10
  30. package/dist/interfaces/output-error.interface.js +0 -2
  31. package/dist/interfaces/output-success.interface.js +0 -2
  32. package/dist/marketplace.d.ts +0 -27
  33. package/dist/marketplace.js +0 -291
  34. package/dist/types/index.d.ts +0 -12
  35. package/dist/types/index.js +0 -2
  36. /package/dist/interfaces/{index.js → search-params.interface.js} +0 -0
  37. /package/dist/interfaces/{input.interface.js → search-result.interface.js} +0 -0
@@ -1,291 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const user_agents_1 = __importDefault(require("user-agents"));
7
- const playwright_chromium_1 = require("playwright-chromium");
8
- const data_1 = require("./data/index.js");
9
- /**
10
- * Discogs Marketplace
11
- */
12
- class Marketplace {
13
- /**
14
- * Search elements on discogs
15
- * @returns Items found
16
- */
17
- 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', }) {
18
- try {
19
- /** Url to call */
20
- const url = [
21
- this.generateUrl({ searchType, seller, lang }),
22
- this.serializeParams({
23
- [searchType]: searchValue,
24
- currency,
25
- genre,
26
- style: (style === null || style === void 0 ? void 0 : style.length) ? style : undefined,
27
- format: (format === null || format === void 0 ? void 0 : format.length) ? format : undefined,
28
- format_desc: (formatDescription === null || formatDescription === void 0 ? void 0 : formatDescription.length) ? formatDescription : undefined,
29
- condition: (condition === null || condition === void 0 ? void 0 : condition.length) ? condition : undefined,
30
- year: year && !years ? year : undefined,
31
- year1: (years === null || years === void 0 ? void 0 : years.min) && !year ? years === null || years === void 0 ? void 0 : years.min : undefined,
32
- year2: (years === null || years === void 0 ? void 0 : years.max) && !year ? years === null || years === void 0 ? void 0 : years.max : undefined,
33
- audio: isAudioSample ? 1 : undefined,
34
- offers: isMakeAnOfferOnly ? 1 : undefined,
35
- ships_from: (from === null || from === void 0 ? void 0 : from.length) ? from : undefined,
36
- limit,
37
- page,
38
- sort,
39
- }),
40
- ].join('?');
41
- /** Init browser */
42
- const browser = await playwright_chromium_1.chromium.launch({
43
- args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-gl-drawing-for-tests'],
44
- });
45
- /** Init context */
46
- const browserContext = await browser.newContext({
47
- userAgent: new user_agents_1.default({ platform: 'Win32' }).toString(),
48
- extraHTTPHeaders: {
49
- 'X-PJAX': 'true',
50
- },
51
- javaScriptEnabled: false,
52
- });
53
- /** Init page */
54
- const browserPage = await browserContext.newPage();
55
- /** Block useless resources */
56
- await browserPage.route('**/*', route => [
57
- 'stylesheet',
58
- 'image',
59
- 'media',
60
- 'font',
61
- 'script',
62
- 'texttrack',
63
- 'xhr',
64
- 'fetch',
65
- 'eventsource',
66
- 'websocket',
67
- 'manifest',
68
- 'other',
69
- ].includes(route.request().resourceType())
70
- ? route.abort()
71
- : route.continue());
72
- /** Init page */
73
- const response = await browserPage.goto(url, { waitUntil: 'domcontentloaded' });
74
- /** Status code from the page */
75
- const status = (response === null || response === void 0 ? void 0 : response.status()) || 500;
76
- // See current issue: https://github.com/evanw/esbuild/issues/2605
77
- // eslint-disable-next-line no-underscore-dangle, @typescript-eslint/no-explicit-any, no-return-assign
78
- await browserPage.evaluate(() => { var _a; var _b; return ((_a = (_b = window).__name) !== null && _a !== void 0 ? _a : (_b.__name = (func) => func)); });
79
- /** If error, reject */
80
- if (status >= 400) {
81
- // eslint-disable-next-line @typescript-eslint/no-throw-literal
82
- throw {
83
- message: (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(); })) || 'An error occurred',
84
- code: status,
85
- };
86
- }
87
- /** Url generated by browser */
88
- const urlGenerated = browserPage.url();
89
- /**
90
- * Result from scraping.
91
- * Scraping inside Playwright is faster (x2) compare to exporting HTML outside and parsing it with another library.
92
- */
93
- const result = await browserPage.evaluate(this.getResult, {
94
- input: {
95
- page,
96
- limit,
97
- searchValue,
98
- searchType,
99
- },
100
- urlGenerated,
101
- CURRENCIES: data_1.CURRENCIES,
102
- COUNTRIES: data_1.COUNTRIES,
103
- });
104
- /** Close context/browser */
105
- await browserContext.close();
106
- await browser.close();
107
- return result;
108
- }
109
- catch (error) {
110
- if (error.message && error.code) {
111
- // Rethrow error
112
- throw error;
113
- }
114
- // eslint-disable-next-line @typescript-eslint/no-throw-literal
115
- throw {
116
- message: (error === null || error === void 0 ? void 0 : error.message) || 'An error occurred',
117
- code: 500,
118
- };
119
- }
120
- }
121
- /**
122
- * Generate URL to be parsed
123
- * @returns Url
124
- */
125
- static generateUrl({ searchType, seller, lang }) {
126
- let url = `https://www.discogs.com/${lang}/`;
127
- if (seller) {
128
- url += `seller/${seller}/`;
129
- url += searchType === 'user' ? 'mywants' : 'profile';
130
- }
131
- else {
132
- url += 'sell/';
133
- url += searchType === 'user' ? 'mywants' : 'list';
134
- }
135
- return url;
136
- }
137
- /**
138
- * Serialize params URL
139
- * @param params Object of GET parameters
140
- * @returns Url
141
- */
142
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
143
- static serializeParams(params) {
144
- const param = [];
145
- // eslint-disable-next-line no-restricted-syntax
146
- for (const key in params) {
147
- if (params[key] === undefined || params[key] === null) {
148
- // eslint-disable-next-line no-continue
149
- continue;
150
- }
151
- else if (Array.isArray(params[key])) {
152
- /** Custom handle for array */
153
- if (params[key].length > 0) {
154
- // eslint-disable-next-line no-restricted-syntax
155
- for (const el of params[key]) {
156
- param.push(`${key}=${encodeURIComponent(el)}`);
157
- }
158
- }
159
- else {
160
- // eslint-disable-next-line no-continue
161
- continue;
162
- }
163
- }
164
- else {
165
- param.push(`${key}=${encodeURIComponent(params[key])}`);
166
- }
167
- }
168
- return param.join('&');
169
- }
170
- /**
171
- * Parse HTML to clean result
172
- * @returns Items found
173
- */
174
- static getResult({ input: { page, limit, searchValue, searchType }, urlGenerated, CURRENCIES, COUNTRIES, }) {
175
- var _a, _b, _c, _d, _e;
176
- /**
177
- * Function to convert currency to ISO name
178
- * @param value String to clean
179
- * @returns Cleanup string
180
- */
181
- const convertCurrency = (value) => {
182
- var _a;
183
- if (!value) {
184
- return value;
185
- }
186
- const currencyFound = (_a = Object.keys(CURRENCIES).find(key => value.includes(key))) !== null && _a !== void 0 ? _a : '';
187
- if (!currencyFound) {
188
- return value;
189
- }
190
- const currencyClean = CURRENCIES[currencyFound];
191
- const amount = value
192
- .replace(currencyClean !== 'JPY' ? /[.](?=.*[.])/g : /\./g, '') // Remove all dot but last, except if JPY
193
- .replace(currencyFound, '') // Remove original currency
194
- .replace(/\s/g, ''); // Remove spaces
195
- return `${amount} ${currencyClean}`.replace(/\s\s+/g, ' '); // Remove useless spaces
196
- };
197
- const totalItems = parseFloat(((_d = (_c = (_b = (_a = document
198
- .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');
199
- return {
200
- items: ((_e = [...document.querySelectorAll('table.table_block tbody tr')]) === null || _e === void 0 ? void 0 : _e.map(el => {
201
- 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, _59;
202
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
203
- const shipping = convertCurrency((_g = (_f = (_e = (_d = (_c = (_b = (_a = el
204
- .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 : '');
205
- 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);
206
- 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);
207
- 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 : '';
208
- const countryCode = COUNTRIES[countryName];
209
- const releaseId = Number.parseInt((_w = (_v = (_u = (_t = (_s = (_r = el
210
- .querySelector('a.item_release_link')) === null || _r === void 0 ? void 0 : _r.getAttribute('href')) === null || _s === void 0 ? void 0 : _s.split('release/')) === null || _t === void 0 ? void 0 : _t.pop()) === null || _u === void 0 ? void 0 : _u.split('-')) === null || _v === void 0 ? void 0 : _v.shift()) !== null && _w !== void 0 ? _w : '0', 10);
211
- const itemId = Number.parseInt((_z = (_y = (_x = el.querySelector('a.item_description_title')) === null || _x === void 0 ? void 0 : _x.getAttribute('href')) === null || _y === void 0 ? void 0 : _y.split('/').pop()) !== null && _z !== void 0 ? _z : '0', 10);
212
- const notes = Number.parseInt((_6 = (_5 = (_4 = (_3 = (_2 = (_1 = (_0 = el
213
- .querySelector('.seller_info li:nth-child(2) .section_link')) === null || _0 === void 0 ? void 0 : _0.textContent) === null || _1 === void 0 ? void 0 : _1.replace(/\s+/g, ' ')) === null || _2 === void 0 ? void 0 : _2.trim()) === null || _3 === void 0 ? void 0 : _3.split(' ')) === null || _4 === void 0 ? void 0 : _4[0]) === null || _5 === void 0 ? void 0 : _5.replace(/,/g, '')) !== null && _6 !== void 0 ? _6 : '', 10);
214
- const originalTitle = (_7 = el.querySelector('a.item_description_title')) === null || _7 === void 0 ? void 0 : _7.textContent;
215
- const firstIndexOfDash = (_8 = originalTitle === null || originalTitle === void 0 ? void 0 : originalTitle.indexOf(' - ')) !== null && _8 !== void 0 ? _8 : -1;
216
- const lastIndexOfParenthesis = (_9 = originalTitle === null || originalTitle === void 0 ? void 0 : originalTitle.lastIndexOf(' (')) !== null && _9 !== void 0 ? _9 : -1;
217
- return {
218
- id: Number.isNaN(itemId) ? 0 : itemId,
219
- title: {
220
- original: originalTitle !== null && originalTitle !== void 0 ? originalTitle : '',
221
- artist: (_10 = originalTitle === null || originalTitle === void 0 ? void 0 : originalTitle.substring(0, firstIndexOfDash)) !== null && _10 !== void 0 ? _10 : '',
222
- item: firstIndexOfDash > -1 && lastIndexOfParenthesis > -1
223
- ? ((_11 = originalTitle === null || originalTitle === void 0 ? void 0 : originalTitle.substring(firstIndexOfDash + 3, lastIndexOfParenthesis)) !== null && _11 !== void 0 ? _11 : '')
224
- : '',
225
- formats: lastIndexOfParenthesis > -1
226
- ? ((_13 = (_12 = originalTitle === null || originalTitle === void 0 ? void 0 : originalTitle.substring(lastIndexOfParenthesis + 2, originalTitle.length - 1)) === null || _12 === void 0 ? void 0 : _12.split(', ')) !== null && _13 !== void 0 ? _13 : [])
227
- : [],
228
- },
229
- url: `https://www.discogs.com${(_14 = el
230
- .querySelector('a.item_description_title')) === null || _14 === void 0 ? void 0 : _14.getAttribute('href')}`,
231
- labels: (_16 = (_15 = [...el.querySelectorAll(".label_and_cat a[href^='https://www.discogs.com/']")]) === null || _15 === void 0 ? void 0 : _15.map(x => { var _a; return (_a = x === null || x === void 0 ? void 0 : x.textContent) !== null && _a !== void 0 ? _a : ''; })) === null || _16 === void 0 ? void 0 : _16.filter((value, index, self) => self.indexOf(value) === index),
232
- catnos: (_21 = (_20 = (_19 = (_18 = (_17 = el
233
- // cspell: disable-next-line
234
- .querySelector('.label_and_cat .item_catno')) === null || _17 === void 0 ? void 0 : _17.textContent) === null || _18 === void 0 ? void 0 : _18.replace(/\s+/g, ' ')) === null || _19 === void 0 ? void 0 : _19.split(', ')) === null || _20 === void 0 ? void 0 : _20.filter(x => x !== 'none')) !== null && _21 !== void 0 ? _21 : [],
235
- imageUrl: (_23 = (_22 = el.querySelector('.marketplace_image')) === null || _22 === void 0 ? void 0 : _22.getAttribute('data-src')) !== null && _23 !== void 0 ? _23 : '',
236
- description: (_28 = (_27 = (_26 = (_25 = (_24 = el.querySelector('.item_condition')) === null || _24 === void 0 ? void 0 : _24.nextElementSibling) === null || _25 === void 0 ? void 0 : _25.textContent) === null || _26 === void 0 ? void 0 : _26.replace(/\s+/g, ' ')) === null || _27 === void 0 ? void 0 : _27.trim()) !== null && _28 !== void 0 ? _28 : '',
237
- isAcceptingOffer: ((_32 = (_31 = (_30 = (_29 = el.querySelector('.item_add_to_cart p a strong')) === null || _29 === void 0 ? void 0 : _29.textContent) === null || _30 === void 0 ? void 0 : _30.split('/')) === null || _31 === void 0 ? void 0 : _31.length) !== null && _32 !== void 0 ? _32 : 0) > 1,
238
- isAvailable: !((_33 = el.classList) === null || _33 === void 0 ? void 0 : _33.contains('unavailable')),
239
- condition: {
240
- media: {
241
- full: (_37 = (_36 = (_35 = (_34 = el.querySelector('.item_condition span:nth-child(3)')) === null || _34 === void 0 ? void 0 : _34.textContent) === null || _35 === void 0 ? void 0 : _35.replace(/\s+/g, ' ')) === null || _36 === void 0 ? void 0 : _36.trim()) !== null && _37 !== void 0 ? _37 : '',
242
- short: (_43 = (_42 = (_41 = (_40 = (_39 = (_38 = el
243
- .querySelector('.item_condition span:nth-child(3)')) === null || _38 === void 0 ? void 0 : _38.textContent) === null || _39 === void 0 ? void 0 : _39.replace(/\s+/g, ' ')) === null || _40 === void 0 ? void 0 : _40.trim()) === null || _41 === void 0 ? void 0 : _41.match(/\(([^)]+)\)/)) === null || _42 === void 0 ? void 0 : _42[1]) !== null && _43 !== void 0 ? _43 : '',
244
- },
245
- sleeve: {
246
- full: (_45 = (_44 = el.querySelector('.item_condition span.item_sleeve_condition')) === null || _44 === void 0 ? void 0 : _44.textContent) !== null && _45 !== void 0 ? _45 : '',
247
- short: (_49 = (_48 = (_47 = (_46 = el
248
- .querySelector('.item_condition span.item_sleeve_condition')) === null || _46 === void 0 ? void 0 : _46.textContent) === null || _47 === void 0 ? void 0 : _47.match(/\(([^)]+)\)/)) === null || _48 === void 0 ? void 0 : _48[1]) !== null && _49 !== void 0 ? _49 : '',
249
- },
250
- },
251
- seller: {
252
- name: (_51 = (_50 = el.querySelector('.seller_info a')) === null || _50 === void 0 ? void 0 : _50.textContent) !== null && _51 !== void 0 ? _51 : '',
253
- url: `https://www.discogs.com${(_52 = el.querySelector('.seller_info a')) === null || _52 === void 0 ? void 0 : _52.getAttribute('href')}`,
254
- score: (_54 = (_53 = el.querySelector('.seller_info li:nth-child(2) strong')) === null || _53 === void 0 ? void 0 : _53.textContent) !== null && _54 !== void 0 ? _54 : '',
255
- notes: Number.isNaN(notes) ? 0 : notes,
256
- },
257
- price: {
258
- base: convertCurrency((_58 = (_57 = (_56 = (_55 = el.querySelector('.price')) === null || _55 === void 0 ? void 0 : _55.textContent) === null || _56 === void 0 ? void 0 : _56.replace(/\s+/g, ' ')) === null || _57 === void 0 ? void 0 : _57.replace(/,/, '.')) !== null && _58 !== void 0 ? _58 : ''),
259
- shipping: Number.isNaN(parseFloat(shipping)) ? null : shipping,
260
- },
261
- country: {
262
- name: countryName,
263
- code: countryCode,
264
- },
265
- community: {
266
- have: Number.isNaN(have) ? 0 : have,
267
- want: Number.isNaN(want) ? 0 : want,
268
- },
269
- release: {
270
- id: Number.isNaN(releaseId) ? 0 : releaseId,
271
- url: `https://www.discogs.com${(_59 = el.querySelector('a.item_release_link')) === null || _59 === void 0 ? void 0 : _59.getAttribute('href')}`,
272
- },
273
- };
274
- })) || [],
275
- page: {
276
- current: page,
277
- total: Math.ceil(totalItems / limit),
278
- },
279
- result: {
280
- total: totalItems,
281
- perPage: limit,
282
- },
283
- search: {
284
- value: searchValue !== null && searchValue !== void 0 ? searchValue : '',
285
- type: searchType,
286
- },
287
- urlGenerated: urlGenerated !== null && urlGenerated !== void 0 ? urlGenerated : '',
288
- };
289
- }
290
- }
291
- exports.default = Marketplace;
@@ -1,12 +0,0 @@
1
- import type LimitType from '../types/limit.type.js';
2
- import type GenreType from '../types/genre.type.js';
3
- import type CurrencyType from '../types/currency.type.js';
4
- import type FromType from '../types/from.type.js';
5
- import type StyleType from '../types/style.type.js';
6
- import type FormatType from '../types/format.type.js';
7
- import type ConditionType from '../types/condition.type.js';
8
- import type SortType from '../types/sort.type.js';
9
- import type LangType from '../types/lang.type.js';
10
- import type FormatDescriptionType from '../types/format-description.type.js';
11
- import type SearchTypeType from '../types/search-type.type.js';
12
- export type { LimitType, GenreType, CurrencyType, FromType, StyleType, FormatType, ConditionType, SortType, LangType, FormatDescriptionType, SearchTypeType, };
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });