discogs-marketplace-api-nodejs 1.3.1 → 1.3.5

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 (62) hide show
  1. package/LICENSE +20 -20
  2. package/README.MD +217 -197
  3. package/dist/enum/ECurrency.d.ts +18 -19
  4. package/dist/enum/ECurrency.js +21 -23
  5. package/dist/enum/EFormat.d.ts +59 -60
  6. package/dist/enum/EFormat.js +62 -64
  7. package/dist/enum/EFormatDescription.d.ts +144 -145
  8. package/dist/enum/EFormatDescription.js +147 -149
  9. package/dist/enum/EFrom.d.ts +251 -252
  10. package/dist/enum/EFrom.js +255 -256
  11. package/dist/enum/EFromIso.d.ts +316 -316
  12. package/dist/enum/EFromIso.js +321 -321
  13. package/dist/enum/EGenre.d.ts +21 -22
  14. package/dist/enum/EGenre.js +24 -26
  15. package/dist/enum/ELang.d.ts +15 -16
  16. package/dist/enum/ELang.js +18 -20
  17. package/dist/enum/EMediaCondition.d.ts +14 -15
  18. package/dist/enum/EMediaCondition.js +17 -19
  19. package/dist/enum/ESort.d.ts +62 -63
  20. package/dist/enum/ESort.js +65 -67
  21. package/dist/enum/EStyle.d.ts +255 -256
  22. package/dist/enum/EStyle.js +258 -260
  23. package/dist/enum/EType.d.ts +30 -31
  24. package/dist/enum/EType.js +33 -35
  25. package/dist/enum/iso/ECountryCode.d.ts +255 -0
  26. package/dist/enum/iso/ECountryCode.js +258 -0
  27. package/dist/enum/iso/ECountryName.d.ts +255 -0
  28. package/dist/enum/iso/ECountryName.js +258 -0
  29. package/dist/enum/iso/EDiscogsCountryNameToIsoCode.d.ts +69 -0
  30. package/dist/enum/iso/EDiscogsCountryNameToIsoCode.js +72 -0
  31. package/dist/enum/iso/EDiscogsCurrencyToIsoCode.d.ts +23 -0
  32. package/dist/enum/iso/EDiscogsCurrencyToIsoCode.js +26 -0
  33. package/dist/enum/iso/translate/EDiscogsCountryNameToIsoCode.d.ts +69 -0
  34. package/dist/enum/iso/translate/EDiscogsCountryNameToIsoCode.js +72 -0
  35. package/dist/enum/iso/translate/EDiscogsCurrencyToIsoCode.d.ts +23 -0
  36. package/dist/enum/iso/translate/EDiscogsCurrencyToIsoCode.js +26 -0
  37. package/dist/enum/iso/translates/EDiscogsCountryNameToIsoCode.d.ts +69 -0
  38. package/dist/enum/iso/translates/EDiscogsCountryNameToIsoCode.js +72 -0
  39. package/dist/enum/iso/translates/EDiscogsCurrencyToIsoCode.d.ts +23 -0
  40. package/dist/enum/iso/translates/EDiscogsCurrencyToIsoCode.js +26 -0
  41. package/dist/index.d.ts +22 -22
  42. package/dist/index.js +40 -39
  43. package/dist/interface/IDiscogsMarketplace.d.ts +11 -12
  44. package/dist/interface/IDiscogsMarketplace.js +7 -7
  45. package/dist/interface/IInput.d.ts +89 -89
  46. package/dist/interface/IInput.js +17 -6
  47. package/dist/interface/IItem.d.ts +54 -48
  48. package/dist/interface/IItem.js +7 -2
  49. package/dist/interface/IOutputError.d.ts +7 -7
  50. package/dist/interface/IOutputError.js +2 -2
  51. package/dist/interface/IOutputSuccess.d.ts +21 -21
  52. package/dist/interface/IOutputSuccess.js +7 -6
  53. package/dist/interface/IYears.d.ts +7 -7
  54. package/dist/interface/IYears.js +2 -2
  55. package/dist/marketplace.d.ts +56 -70
  56. package/dist/marketplace.js +290 -286
  57. package/dist/type/TLimit.d.ts +5 -6
  58. package/dist/type/TLimit.js +2 -2
  59. package/package.json +52 -45
  60. package/.gitattributes +0 -1
  61. package/notes.md +0 -9
  62. package/tsconfig.build.json +0 -40
@@ -1,286 +1,290 @@
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
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- const EFromIso_1 = require("./enum/EFromIso");
16
- const ELang_1 = require("./enum/ELang");
17
- const ESort_1 = require("./enum/ESort");
18
- const EType_1 = require("./enum/EType");
19
- const IInput_1 = __importDefault(require("./interface/IInput"));
20
- const IOutputError_1 = __importDefault(require("./interface/IOutputError"));
21
- const IOutputSuccess_1 = __importDefault(require("./interface/IOutputSuccess"));
22
- const IYears_1 = __importDefault(require("./interface/IYears"));
23
- const jsdom_1 = require("jsdom");
24
- const playwright_chromium_1 = require("playwright-chromium");
25
- const user_agents_1 = __importDefault(require("user-agents"));
26
- /**
27
- * Discogs Marketplace
28
- * @class
29
- * @implements {IInput}
30
- */
31
- class Marketplace {
32
- constructor({ searchType = EType_1.EType.STRING, searchValue = undefined, currency = undefined, genre = undefined, style = [], format = [], formatDescription = [], mediaCondition = [], year = undefined, years = undefined, isAudioSample = false, isMakeAnOfferOnly = false, from = undefined, seller = undefined, sort = ESort_1.ESort.LISTED_NEWEST, limit = 25, page = 1, lang = ELang_1.ELang.ENGLISH }) {
33
- /** Url generated by Puppetter */
34
- this.url = '';
35
- this.searchType = searchType;
36
- this.searchValue = searchValue;
37
- this.currency = currency;
38
- this.genre = genre;
39
- this.style = style;
40
- this.format = format;
41
- this.formatDescription = formatDescription;
42
- this.mediaCondition = mediaCondition;
43
- this.year = year;
44
- this.years = years;
45
- this.isAudioSample = isAudioSample;
46
- this.isMakeAnOfferOnly = isMakeAnOfferOnly;
47
- this.from = from;
48
- this.seller = seller;
49
- this.sort = sort;
50
- this.limit = limit;
51
- this.page = page;
52
- this.lang = lang;
53
- }
54
- /**
55
- * Search elements on discogs
56
- * @returns {Promise<IOutputSuccess>} Items found
57
- */
58
- search() {
59
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
60
- return __awaiter(this, void 0, void 0, function* () {
61
- try {
62
- /** Init browser */
63
- const browser = yield playwright_chromium_1.chromium.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] });
64
- /** Init context */
65
- const context = yield browser.newContext({
66
- userAgent: new user_agents_1.default().toString(),
67
- });
68
- /** Init page */
69
- const page = yield context.newPage();
70
- /** Block useless ressources */
71
- yield page.route('**/*', route => ([
72
- 'stylesheet',
73
- 'image',
74
- 'media',
75
- 'font',
76
- 'script',
77
- 'texttrack',
78
- 'xhr',
79
- 'fetch',
80
- 'eventsource',
81
- 'websocket',
82
- 'manifest',
83
- 'other',
84
- ].includes(route.request().resourceType()) ? route.abort() : route.continue()));
85
- /** Init page */
86
- const response = yield page.goto(`${this.generateUrl()}?${this.serializeParams({
87
- [this.searchType]: this.searchValue,
88
- currency: this.currency,
89
- genre: this.genre,
90
- style: ((_a = this.style) === null || _a === void 0 ? void 0 : _a.length) ? this.style : undefined,
91
- format: ((_b = this.format) === null || _b === void 0 ? void 0 : _b.length) ? (_c = this.format) === null || _c === void 0 ? void 0 : _c[0] : undefined,
92
- format_desc: ((_d = this.formatDescription) === null || _d === void 0 ? void 0 : _d.length) ? this.formatDescription[0] : undefined,
93
- condition: ((_e = this.mediaCondition) === null || _e === void 0 ? void 0 : _e.length) ? this.mediaCondition[0] : undefined,
94
- year: this.year && !this.years ? this.year : undefined,
95
- year1: ((_f = this.years) === null || _f === void 0 ? void 0 : _f.min) && !this.year ? (_g = this.years) === null || _g === void 0 ? void 0 : _g.min : undefined,
96
- year2: ((_h = this.years) === null || _h === void 0 ? void 0 : _h.max) && !this.year ? (_j = this.years) === null || _j === void 0 ? void 0 : _j.max : undefined,
97
- audio: this.isAudioSample ? 1 : 0,
98
- offers: this.isMakeAnOfferOnly ? 1 : 0,
99
- ships_from: ((_k = this.from) === null || _k === void 0 ? void 0 : _k.length) ? this.from : undefined,
100
- limit: this.limit,
101
- page: this.page,
102
- sort: this.sort,
103
- })}`);
104
- /** Get url */
105
- this.url = page.url();
106
- /** Get HTML */
107
- const bodyHTML = yield page.evaluate(() => document.body.innerHTML);
108
- /** Close browser */
109
- yield browser.close();
110
- /** If error, reject */
111
- if (((_l = response === null || response === void 0 ? void 0 : response.status()) !== null && _l !== void 0 ? _l : 0) >= 400)
112
- throw { document: bodyHTML, status: (_m = response === null || response === void 0 ? void 0 : response.status()) !== null && _m !== void 0 ? _m : 0 };
113
- return this.getItem((new jsdom_1.JSDOM(bodyHTML)).window.document);
114
- }
115
- catch (error) {
116
- if ((error === null || error === void 0 ? void 0 : error.document) && error.status)
117
- throw {
118
- message: (_s = (_r = (_q = (_p = (_o = (new jsdom_1.JSDOM(error === null || error === void 0 ? void 0 : error.document))) === null || _o === void 0 ? void 0 : _o.window) === null || _p === void 0 ? void 0 : _p.querySelector('h1 + p')) === null || _q === void 0 ? void 0 : _q.innerHTML) === null || _r === void 0 ? void 0 : _r.trim()) !== null && _s !== void 0 ? _s : 'An error occured',
119
- code: error.status
120
- };
121
- throw {
122
- message: 'An error occured',
123
- code: 500
124
- };
125
- }
126
- });
127
- }
128
- /**
129
- * Generate URL to be parsed
130
- * @returns {string} Url
131
- */
132
- generateUrl() {
133
- let url = `https://www.discogs.com/${this.lang}/`;
134
- if (this.seller) {
135
- url += `seller/${this.seller}/`;
136
- url += this.searchType === EType_1.EType.USER ? `mywants` : 'profile';
137
- }
138
- else {
139
- url += `sell/`;
140
- url += this.searchType === EType_1.EType.USER ? `mywants` : `list`;
141
- }
142
- return url;
143
- }
144
- /**
145
- * Serialize params URL
146
- * @param {any} params Object of GET parameters
147
- * @returns {string} Url
148
- */
149
- serializeParams(params) {
150
- let param = [];
151
- for (let key in params) {
152
- if (params[key] === undefined || params[key] === null) {
153
- continue;
154
- }
155
- else if (Array.isArray(params[key])) {
156
- /** Custom handle for array */
157
- if (params[key].length > 0) {
158
- for (const el of params[key]) {
159
- param.push(key + '=' + encodeURIComponent(el));
160
- }
161
- }
162
- else {
163
- continue;
164
- }
165
- }
166
- else {
167
- param.push(key + '=' + encodeURIComponent(params[key]));
168
- }
169
- }
170
- return param.join('&');
171
- }
172
- /**
173
- * Function to convert devise to ISO name
174
- * @param str String to clean
175
- * @returns {string} Cleanup string
176
- */
177
- convertDevise(str) {
178
- if (!str)
179
- return str;
180
- const data = {
181
- '\$US': 'USD',
182
- '£GB': 'GBP',
183
- '\$CA': 'CAD',
184
- '\$AU': 'AUD',
185
- 'JPY': 'JPY',
186
- 'CHF': 'CHF',
187
- 'MX\$': 'MXN',
188
- '\$NZ': 'NZD',
189
- 'SEK': 'SEK',
190
- 'ZAR': 'ZAR',
191
- 'CA\$': 'AUD',
192
- 'A\$': 'AUD',
193
- 'R\$': 'BRL',
194
- '€': 'EUR',
195
- '\$': 'USD',
196
- '£': 'GBP',
197
- '¥': 'JPY',
198
- };
199
- Object.keys(data).forEach(x => {
200
- if (str.includes(`${x}`)) {
201
- str = str.replace(`${x}`, '');
202
- str = `${str} ${data[x]}`.replace(/\s\s+/g, ' '); //Replace multiple space by one
203
- }
204
- });
205
- return str;
206
- }
207
- /**
208
- * Parse HTML to clean result
209
- * @param {Document} document Document to parse
210
- * @returns {IOutputSuccess} Items found
211
- */
212
- getItem(document) {
213
- var _a, _b, _c, _d, _e;
214
- const totalItems = parseFloat(((_d = (_c = (_b = (_a = document.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');
215
- return {
216
- result: ((_e = [...document.querySelectorAll('table.table_block tbody tr')]) === null || _e === void 0 ? void 0 : _e.map(el => {
217
- 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;
218
- const shipping = this.convertDevise((_f = (_e = (_d = (_c = (_b = (_a = el.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(',', '.'));
219
- const have = parseInt((_g = el.querySelector('.community_summary .community_result:nth-child(1) .community_number')) === null || _g === void 0 ? void 0 : _g.textContent);
220
- const want = parseInt((_h = el.querySelector('.community_summary .community_result:nth-child(2) .community_number')) === null || _h === void 0 ? void 0 : _h.textContent);
221
- const country = (_l = (_k = (_j = el.querySelector('.seller_info li:nth-child(3)')) === null || _j === void 0 ? void 0 : _j.textContent) === null || _k === void 0 ? void 0 : _k.split(':')) === null || _l === void 0 ? void 0 : _l[1];
222
- const releaseId = parseInt((_r = (_q = (_p = (_o = (_m = el.querySelector('a.item_release_link')) === null || _m === void 0 ? void 0 : _m.href.split('release/')) === null || _o === void 0 ? void 0 : _o.pop()) === null || _p === void 0 ? void 0 : _p.split('-')) === null || _q === void 0 ? void 0 : _q.shift()) !== null && _r !== void 0 ? _r : '0');
223
- const itemId = parseInt((_u = (_t = (_s = el.querySelector('a.item_description_title')) === null || _s === void 0 ? void 0 : _s.href) === null || _t === void 0 ? void 0 : _t.split('/').pop()) !== null && _u !== void 0 ? _u : '0');
224
- return {
225
- itemId: isNaN(itemId) ? 0 : itemId,
226
- title: {
227
- original: (_v = el.querySelector('a.item_description_title')) === null || _v === void 0 ? void 0 : _v.textContent,
228
- artist: (_y = (_x = (_w = el.querySelector('a.item_description_title')) === null || _w === void 0 ? void 0 : _w.textContent) === null || _x === void 0 ? void 0 : _x.split(/ - (.+)/)) === null || _y === void 0 ? void 0 : _y[0],
229
- item: (_1 = (_0 = (_z = el.querySelector('a.item_description_title')) === null || _z === void 0 ? void 0 : _z.textContent) === null || _0 === void 0 ? void 0 : _0.split(/ - (.+)/)) === null || _1 === void 0 ? void 0 : _1[1],
230
- },
231
- url: `https://www.discogs.com${(_2 = el.querySelector('a.item_description_title')) === null || _2 === void 0 ? void 0 : _2.href}`,
232
- labels: [...new Set((_3 = [...el.querySelectorAll('.label_and_cat a[href^=\'https://www.discogs.com/\']')]) === null || _3 === void 0 ? void 0 : _3.map(x => x === null || x === void 0 ? void 0 : x.textContent))],
233
- catnos: (_7 = (_6 = (_5 = (_4 = el.querySelector('.label_and_cat .item_catno')) === null || _4 === void 0 ? void 0 : _4.textContent) === null || _5 === void 0 ? void 0 : _5.replace(/\s+/g, ' ')) === null || _6 === void 0 ? void 0 : _6.split(', ')) === null || _7 === void 0 ? void 0 : _7.filter(x => x !== 'none'),
234
- imageUrl: (_8 = el.querySelector('.marketplace_image')) === null || _8 === void 0 ? void 0 : _8.getAttribute('data-src'),
235
- description: (_11 = (_10 = (_9 = el.querySelector('.item_description > p.hide_mobile:not(.label_and_cat)')) === null || _9 === void 0 ? void 0 : _9.textContent) === null || _10 === void 0 ? void 0 : _10.replace(/\s+/g, ' ')) === null || _11 === void 0 ? void 0 : _11.trim(),
236
- isAcceptingOffer: ((_14 = (_13 = (_12 = el.querySelector('.item_add_to_cart p a strong')) === null || _12 === void 0 ? void 0 : _12.textContent) === null || _13 === void 0 ? void 0 : _13.split('/')) === null || _14 === void 0 ? void 0 : _14.length) > 1,
237
- isAvailable: !((_15 = el.classList) === null || _15 === void 0 ? void 0 : _15.contains('unavailable')),
238
- condition: {
239
- media: {
240
- full: (_18 = (_17 = (_16 = el.querySelector('.item_condition span:nth-child(3)')) === null || _16 === void 0 ? void 0 : _16.textContent) === null || _17 === void 0 ? void 0 : _17.replace(/\s+/g, ' ')) === null || _18 === void 0 ? void 0 : _18.trim(),
241
- short: (_23 = (_22 = (_21 = (_20 = (_19 = el.querySelector('.item_condition span:nth-child(3)')) === null || _19 === void 0 ? void 0 : _19.textContent) === null || _20 === void 0 ? void 0 : _20.replace(/\s+/g, ' ')) === null || _21 === void 0 ? void 0 : _21.trim()) === null || _22 === void 0 ? void 0 : _22.match(/\(([^)]+)\)/)) === null || _23 === void 0 ? void 0 : _23[1]
242
- },
243
- sleeve: {
244
- full: (_24 = el.querySelector('.item_condition span.item_sleeve_condition')) === null || _24 === void 0 ? void 0 : _24.textContent,
245
- short: (_27 = (_26 = (_25 = el.querySelector('.item_condition span.item_sleeve_condition')) === null || _25 === void 0 ? void 0 : _25.textContent) === null || _26 === void 0 ? void 0 : _26.match(/\(([^)]+)\)/)) === null || _27 === void 0 ? void 0 : _27[1]
246
- }
247
- },
248
- seller: {
249
- name: (_28 = el.querySelector('.seller_info a')) === null || _28 === void 0 ? void 0 : _28.textContent,
250
- score: (_29 = el.querySelector('.seller_info li:nth-child(2) strong')) === null || _29 === void 0 ? void 0 : _29.textContent,
251
- notes: (_34 = (_33 = (_32 = (_31 = (_30 = el.querySelector('.seller_info li:nth-child(2) .section_link')) === null || _30 === void 0 ? void 0 : _30.textContent) === null || _31 === void 0 ? void 0 : _31.replace(/\s+/g, ' ')) === null || _32 === void 0 ? void 0 : _32.trim()) === null || _33 === void 0 ? void 0 : _33.split(' ')) === null || _34 === void 0 ? void 0 : _34[0]
252
- },
253
- price: {
254
- base: this.convertDevise((_37 = (_36 = (_35 = el.querySelector('.price')) === null || _35 === void 0 ? void 0 : _35.textContent) === null || _36 === void 0 ? void 0 : _36.replace(/\s+/g, ' ')) === null || _37 === void 0 ? void 0 : _37.replace(/,/, '.')),
255
- shipping: isNaN(parseFloat(shipping)) ? null : shipping,
256
- },
257
- from: {
258
- countryName: country,
259
- isoCountryName: (_39 = (_38 = EFromIso_1.EFromIso[country]) !== null && _38 !== void 0 ? _38 : country) !== null && _39 !== void 0 ? _39 : '',
260
- isoCode: EFromIso_1.EFromIsoCode[(_41 = (_40 = EFromIso_1.EFromIso[country]) !== null && _40 !== void 0 ? _40 : country) !== null && _41 !== void 0 ? _41 : ''],
261
- },
262
- community: {
263
- have: isNaN(have) ? 0 : have,
264
- want: isNaN(want) ? 0 : want
265
- },
266
- releaseUrl: `https://www.discogs.com${(_42 = el.querySelector('a.item_release_link')) === null || _42 === void 0 ? void 0 : _42.href}`,
267
- releaseId: isNaN(releaseId) ? 0 : releaseId,
268
- };
269
- })) || [],
270
- page: {
271
- current: this.page,
272
- total: Math.ceil(totalItems / this.limit)
273
- },
274
- item: {
275
- total: totalItems,
276
- perPage: this.limit,
277
- },
278
- search: {
279
- value: this.searchValue,
280
- type: this.searchType
281
- },
282
- urlGenerated: this.url
283
- };
284
- }
285
- }
286
- exports.default = Marketplace;
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const ECountryCode_1 = __importDefault(require("./enum/iso/ECountryCode"));
16
+ const ECountryName_1 = __importDefault(require("./enum/iso/ECountryName"));
17
+ const ECurrency_1 = __importDefault(require("./enum/ECurrency"));
18
+ const EFormat_1 = __importDefault(require("./enum/EFormat"));
19
+ const EFormatDescription_1 = __importDefault(require("./enum/EFormatDescription"));
20
+ const EFrom_1 = __importDefault(require("./enum/EFrom"));
21
+ const EGenre_1 = __importDefault(require("./enum/EGenre"));
22
+ const ELang_1 = __importDefault(require("./enum/ELang"));
23
+ const EMediaCondition_1 = __importDefault(require("./enum/EMediaCondition"));
24
+ const ESort_1 = __importDefault(require("./enum/ESort"));
25
+ const EStyle_1 = __importDefault(require("./enum/EStyle"));
26
+ const EType_1 = __importDefault(require("./enum/EType"));
27
+ const IInput_1 = __importDefault(require("./interface/IInput"));
28
+ const IOutputError_1 = __importDefault(require("./interface/IOutputError"));
29
+ const IOutputSuccess_1 = __importDefault(require("./interface/IOutputSuccess"));
30
+ const IYears_1 = __importDefault(require("./interface/IYears"));
31
+ const TLimit_1 = __importDefault(require("./type/TLimit"));
32
+ const playwright_chromium_1 = require("playwright-chromium");
33
+ const user_agents_1 = __importDefault(require("user-agents"));
34
+ const EDiscogsCurrencyToIsoCode_1 = __importDefault(require("./enum/iso/translate/EDiscogsCurrencyToIsoCode"));
35
+ const EDiscogsCountryNameToIsoCode_1 = __importDefault(require("./enum/iso/translate/EDiscogsCountryNameToIsoCode"));
36
+ const linkedom_1 = require("linkedom");
37
+ /**
38
+ * Discogs Marketplace
39
+ */
40
+ class Marketplace {
41
+ constructor({ searchType = EType_1.default.STRING, searchValue = undefined, currency = undefined, genre = undefined, style = [], format = [], formatDescription = [], mediaCondition = [], year = undefined, years = undefined, isAudioSample = false, isMakeAnOfferOnly = false, from = undefined, seller = undefined, sort = ESort_1.default.LISTED_NEWEST, limit = 25, page = 1, lang = ELang_1.default.ENGLISH, }) {
42
+ /** Url generated by Playwright */
43
+ this.url = '';
44
+ this.searchType = searchType;
45
+ this.searchValue = searchValue;
46
+ this.currency = currency;
47
+ this.genre = genre;
48
+ this.style = style;
49
+ this.format = format;
50
+ this.formatDescription = formatDescription;
51
+ this.mediaCondition = mediaCondition;
52
+ this.year = year;
53
+ this.years = years;
54
+ this.isAudioSample = isAudioSample;
55
+ this.isMakeAnOfferOnly = isMakeAnOfferOnly;
56
+ this.from = from;
57
+ this.seller = seller;
58
+ this.sort = sort;
59
+ this.limit = limit;
60
+ this.page = page;
61
+ this.lang = lang;
62
+ }
63
+ /**
64
+ * Search elements on discogs
65
+ * @returns Items found
66
+ */
67
+ search() {
68
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
69
+ return __awaiter(this, void 0, void 0, function* () {
70
+ try {
71
+ /** Init browser */
72
+ const browser = yield playwright_chromium_1.chromium.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] });
73
+ /** Init context */
74
+ const context = yield browser.newContext({
75
+ userAgent: new user_agents_1.default().toString(),
76
+ });
77
+ /** Init page */
78
+ const page = yield context.newPage();
79
+ /** Block useless ressources */
80
+ yield page.route('**/*', route => ([
81
+ 'stylesheet',
82
+ 'image',
83
+ 'media',
84
+ 'font',
85
+ 'script',
86
+ 'texttrack',
87
+ 'xhr',
88
+ 'fetch',
89
+ 'eventsource',
90
+ 'websocket',
91
+ 'manifest',
92
+ 'other',
93
+ ].includes(route.request().resourceType()) ? route.abort() : route.continue()));
94
+ /** Init page */
95
+ const response = yield page.goto(`${this.generateUrl()}?${this.serializeParams({
96
+ [this.searchType]: this.searchValue,
97
+ currency: this.currency,
98
+ genre: this.genre,
99
+ style: ((_a = this.style) === null || _a === void 0 ? void 0 : _a.length) ? this.style : undefined,
100
+ format: ((_b = this.format) === null || _b === void 0 ? void 0 : _b.length) ? (_c = this.format) === null || _c === void 0 ? void 0 : _c[0] : undefined,
101
+ format_desc: ((_d = this.formatDescription) === null || _d === void 0 ? void 0 : _d.length) ? this.formatDescription[0] : undefined,
102
+ condition: ((_e = this.mediaCondition) === null || _e === void 0 ? void 0 : _e.length) ? this.mediaCondition[0] : undefined,
103
+ year: this.year && !this.years ? this.year : undefined,
104
+ year1: ((_f = this.years) === null || _f === void 0 ? void 0 : _f.min) && !this.year ? (_g = this.years) === null || _g === void 0 ? void 0 : _g.min : undefined,
105
+ year2: ((_h = this.years) === null || _h === void 0 ? void 0 : _h.max) && !this.year ? (_j = this.years) === null || _j === void 0 ? void 0 : _j.max : undefined,
106
+ audio: this.isAudioSample ? 1 : 0,
107
+ offers: this.isMakeAnOfferOnly ? 1 : 0,
108
+ ships_from: ((_k = this.from) === null || _k === void 0 ? void 0 : _k.length) ? this.from : undefined,
109
+ limit: this.limit,
110
+ page: this.page,
111
+ sort: this.sort,
112
+ })}`);
113
+ /** Get url */
114
+ this.url = page.url();
115
+ /** Get HTML */
116
+ const bodyHTML = yield page.evaluate(() => { var _a; return (_a = document.querySelector('table.table_block')) === null || _a === void 0 ? void 0 : _a.outerHTML; });
117
+ /** Close browser */
118
+ yield browser.close();
119
+ /** If error, reject */
120
+ if (((_l = response === null || response === void 0 ? void 0 : response.status()) !== null && _l !== void 0 ? _l : 0) >= 400)
121
+ // eslint-disable-next-line @typescript-eslint/no-throw-literal
122
+ throw { document: bodyHTML, status: (_m = response === null || response === void 0 ? void 0 : response.status()) !== null && _m !== void 0 ? _m : 0 };
123
+ return this.getItem((0, linkedom_1.parseHTML)(bodyHTML).document);
124
+ }
125
+ catch (error) {
126
+ if ((error === null || error === void 0 ? void 0 : error.document) && error.status)
127
+ // eslint-disable-next-line @typescript-eslint/no-throw-literal
128
+ throw {
129
+ message: (_r = (_q = (_p = (_o = (0, linkedom_1.parseHTML)(error.document).document) === null || _o === void 0 ? void 0 : _o.querySelector('h1 + p')) === null || _p === void 0 ? void 0 : _p.innerHTML) === null || _q === void 0 ? void 0 : _q.trim()) !== null && _r !== void 0 ? _r : 'An error occured',
130
+ code: error.status,
131
+ };
132
+ // eslint-disable-next-line @typescript-eslint/no-throw-literal
133
+ throw {
134
+ message: 'An error occured',
135
+ code: 500,
136
+ };
137
+ }
138
+ });
139
+ }
140
+ /**
141
+ * Generate URL to be parsed
142
+ * @returns Url
143
+ */
144
+ generateUrl() {
145
+ let url = `https://www.discogs.com/${this.lang}/`;
146
+ if (this.seller) {
147
+ url += `seller/${this.seller}/`;
148
+ url += this.searchType === EType_1.default.USER ? 'mywants' : 'profile';
149
+ }
150
+ else {
151
+ url += 'sell/';
152
+ url += this.searchType === EType_1.default.USER ? 'mywants' : 'list';
153
+ }
154
+ return url;
155
+ }
156
+ /**
157
+ * Serialize params URL
158
+ * @param params Object of GET parameters
159
+ * @returns Url
160
+ */
161
+ // eslint-disable-next-line class-methods-use-this
162
+ serializeParams(params) {
163
+ const param = [];
164
+ // eslint-disable-next-line no-restricted-syntax
165
+ for (const key in params)
166
+ if (params[key] === undefined || params[key] === null)
167
+ // eslint-disable-next-line no-continue
168
+ continue;
169
+ else if (Array.isArray(params[key]))
170
+ /** Custom handle for array */
171
+ if (params[key].length > 0)
172
+ // eslint-disable-next-line no-restricted-syntax
173
+ for (const el of params[key])
174
+ param.push(`${key}=${encodeURIComponent(el)}`);
175
+ else
176
+ // eslint-disable-next-line no-continue
177
+ continue;
178
+ else
179
+ param.push(`${key}=${encodeURIComponent(params[key])}`);
180
+ return param.join('&');
181
+ }
182
+ /**
183
+ * Function to convert devise to ISO name
184
+ * @param str String to clean
185
+ * @returns Cleanup string
186
+ */
187
+ // eslint-disable-next-line class-methods-use-this
188
+ convertDevise(value) {
189
+ var _a;
190
+ if (!value)
191
+ return value;
192
+ const currency = (_a = Object.keys(EDiscogsCurrencyToIsoCode_1.default).find(key => value.includes(key))) !== null && _a !== void 0 ? _a : '';
193
+ if (!currency)
194
+ return value;
195
+ return `${value.replace(currency, '')} ${EDiscogsCurrencyToIsoCode_1.default[currency]}`.replace(/\s\s+/g, ' ');
196
+ }
197
+ /**
198
+ * Parse HTML to clean result
199
+ * @param document Document to parse
200
+ * @returns Items found
201
+ */
202
+ getItem(document) {
203
+ var _a, _b, _c, _d, _e, _f;
204
+ const totalItems = parseFloat(((_d = (_c = (_b = (_a = document.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');
205
+ return {
206
+ items: ((_e = [...document.querySelectorAll('table.table_block tbody tr')]) === null || _e === void 0 ? void 0 : _e.map(el => {
207
+ 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;
208
+ const shipping = this.convertDevise((_g = (_f = (_e = (_d = (_c = (_b = (_a = el.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 : '');
209
+ 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);
210
+ 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);
211
+ 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[1]) !== null && _q !== void 0 ? _q : '';
212
+ const isoCountryCode = (Object.keys(ECountryCode_1.default).find((key) => (ECountryName_1.default[key] === countryName))
213
+ || EDiscogsCountryNameToIsoCode_1.default[countryName]);
214
+ 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);
215
+ 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);
216
+ const notes = Number.parseInt((_5 = (_4 = (_3 = (_2 = (_1 = (_0 = (_z = el.querySelector('.seller_info li:nth-child(2) .section_link')) === null || _z === void 0 ? void 0 : _z.textContent) === null || _0 === void 0 ? void 0 : _0.replace(/\s+/g, ' ')) === null || _1 === void 0 ? void 0 : _1.trim()) === null || _2 === void 0 ? void 0 : _2.split(' ')) === null || _3 === void 0 ? void 0 : _3[0]) === null || _4 === void 0 ? void 0 : _4.replace(/,/g, '')) !== null && _5 !== void 0 ? _5 : '', 10);
217
+ const originalTitle = (_6 = el.querySelector('a.item_description_title')) === null || _6 === void 0 ? void 0 : _6.textContent;
218
+ const firstIndexOfDash = (_7 = originalTitle === null || originalTitle === void 0 ? void 0 : originalTitle.indexOf(' - ')) !== null && _7 !== void 0 ? _7 : -1;
219
+ const lastIndexOfParenthesis = (_8 = originalTitle === null || originalTitle === void 0 ? void 0 : originalTitle.lastIndexOf(' (')) !== null && _8 !== void 0 ? _8 : -1;
220
+ return {
221
+ itemId: Number.isNaN(itemId) ? 0 : itemId,
222
+ title: {
223
+ original: originalTitle !== null && originalTitle !== void 0 ? originalTitle : '',
224
+ artist: (_9 = originalTitle === null || originalTitle === void 0 ? void 0 : originalTitle.substring(0, firstIndexOfDash)) !== null && _9 !== void 0 ? _9 : '',
225
+ item: firstIndexOfDash > -1 && lastIndexOfParenthesis > -1
226
+ ? (_10 = originalTitle === null || originalTitle === void 0 ? void 0 : originalTitle.substring(firstIndexOfDash + 3, lastIndexOfParenthesis)) !== null && _10 !== void 0 ? _10 : ''
227
+ : '',
228
+ formats: lastIndexOfParenthesis > -1
229
+ ? ((_12 = (_11 = originalTitle === null || originalTitle === void 0 ? void 0 : originalTitle.substring(lastIndexOfParenthesis + 2, originalTitle.length - 1)) === null || _11 === void 0 ? void 0 : _11.split(', ')) !== null && _12 !== void 0 ? _12 : [])
230
+ : [],
231
+ },
232
+ url: `https://www.discogs.com${(_13 = el.querySelector('a.item_description_title')) === null || _13 === void 0 ? void 0 : _13.href}`,
233
+ labels: (_15 = (_14 = [...el.querySelectorAll('.label_and_cat a[href^=\'https://www.discogs.com/\']')]) === null || _14 === void 0 ? void 0 : _14.map(x => { var _a; return (_a = x === null || x === void 0 ? void 0 : x.textContent) !== null && _a !== void 0 ? _a : ''; })) === null || _15 === void 0 ? void 0 : _15.filter((value, index, self) => self.indexOf(value) === index),
234
+ catnos: (_20 = (_19 = (_18 = (_17 = (_16 = el.querySelector('.label_and_cat .item_catno')) === null || _16 === void 0 ? void 0 : _16.textContent) === null || _17 === void 0 ? void 0 : _17.replace(/\s+/g, ' ')) === null || _18 === void 0 ? void 0 : _18.split(', ')) === null || _19 === void 0 ? void 0 : _19.filter(x => x !== 'none')) !== null && _20 !== void 0 ? _20 : [],
235
+ imageUrl: (_22 = (_21 = el.querySelector('.marketplace_image')) === null || _21 === void 0 ? void 0 : _21.getAttribute('data-src')) !== null && _22 !== void 0 ? _22 : '',
236
+ description: (_27 = (_26 = (_25 = (_24 = (_23 = el.querySelector('.item_condition')) === null || _23 === void 0 ? void 0 : _23.nextElementSibling) === null || _24 === void 0 ? void 0 : _24.textContent) === null || _25 === void 0 ? void 0 : _25.replace(/\s+/g, ' ')) === null || _26 === void 0 ? void 0 : _26.trim()) !== null && _27 !== void 0 ? _27 : '',
237
+ isAcceptingOffer: ((_31 = (_30 = (_29 = (_28 = el.querySelector('.item_add_to_cart p a strong')) === null || _28 === void 0 ? void 0 : _28.textContent) === null || _29 === void 0 ? void 0 : _29.split('/')) === null || _30 === void 0 ? void 0 : _30.length) !== null && _31 !== void 0 ? _31 : 0) > 1,
238
+ isAvailable: !((_32 = el.classList) === null || _32 === void 0 ? void 0 : _32.contains('unavailable')),
239
+ condition: {
240
+ media: {
241
+ full: (_36 = (_35 = (_34 = (_33 = el.querySelector('.item_condition span:nth-child(3)')) === null || _33 === void 0 ? void 0 : _33.textContent) === null || _34 === void 0 ? void 0 : _34.replace(/\s+/g, ' ')) === null || _35 === void 0 ? void 0 : _35.trim()) !== null && _36 !== void 0 ? _36 : '',
242
+ short: (_42 = (_41 = (_40 = (_39 = (_38 = (_37 = el.querySelector('.item_condition span:nth-child(3)')) === null || _37 === void 0 ? void 0 : _37.textContent) === null || _38 === void 0 ? void 0 : _38.replace(/\s+/g, ' ')) === null || _39 === void 0 ? void 0 : _39.trim()) === null || _40 === void 0 ? void 0 : _40.match(/\(([^)]+)\)/)) === null || _41 === void 0 ? void 0 : _41[1]) !== null && _42 !== void 0 ? _42 : '',
243
+ },
244
+ sleeve: {
245
+ full: (_44 = (_43 = el.querySelector('.item_condition span.item_sleeve_condition')) === null || _43 === void 0 ? void 0 : _43.textContent) !== null && _44 !== void 0 ? _44 : '',
246
+ short: (_48 = (_47 = (_46 = (_45 = el.querySelector('.item_condition span.item_sleeve_condition')) === null || _45 === void 0 ? void 0 : _45.textContent) === null || _46 === void 0 ? void 0 : _46.match(/\(([^)]+)\)/)) === null || _47 === void 0 ? void 0 : _47[1]) !== null && _48 !== void 0 ? _48 : '',
247
+ },
248
+ },
249
+ seller: {
250
+ name: (_50 = (_49 = el.querySelector('.seller_info a')) === null || _49 === void 0 ? void 0 : _49.textContent) !== null && _50 !== void 0 ? _50 : '',
251
+ url: `https://www.discogs.com${(_51 = el.querySelector('.seller_info a')) === null || _51 === void 0 ? void 0 : _51.href}`,
252
+ score: (_53 = (_52 = el.querySelector('.seller_info li:nth-child(2) strong')) === null || _52 === void 0 ? void 0 : _52.textContent) !== null && _53 !== void 0 ? _53 : '',
253
+ notes: Number.isNaN(notes) ? 0 : notes,
254
+ },
255
+ price: {
256
+ base: this.convertDevise((_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 : ''),
257
+ shipping: Number.isNaN(parseFloat(shipping)) ? null : shipping,
258
+ },
259
+ from: {
260
+ countryName,
261
+ isoCountryName: (_58 = ECountryName_1.default[isoCountryCode]) !== null && _58 !== void 0 ? _58 : '',
262
+ isoCountryCode,
263
+ },
264
+ community: {
265
+ have: Number.isNaN(have) ? 0 : have,
266
+ want: Number.isNaN(want) ? 0 : want,
267
+ },
268
+ release: {
269
+ id: Number.isNaN(releaseId) ? 0 : releaseId,
270
+ url: `https://www.discogs.com${(_59 = el.querySelector('a.item_release_link')) === null || _59 === void 0 ? void 0 : _59.href}`,
271
+ },
272
+ };
273
+ })) || [],
274
+ page: {
275
+ current: this.page,
276
+ total: Math.ceil(totalItems / this.limit),
277
+ },
278
+ result: {
279
+ total: totalItems,
280
+ perPage: this.limit,
281
+ },
282
+ search: {
283
+ value: (_f = this.searchValue) !== null && _f !== void 0 ? _f : '',
284
+ type: this.searchType,
285
+ },
286
+ urlGenerated: this.url,
287
+ };
288
+ }
289
+ }
290
+ exports.default = Marketplace;