discogs-marketplace-api-nodejs 1.2.12 → 1.3.4

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 +203 -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 +291 -278
  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,278 +1,291 @@
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 puppeteer_1 = __importDefault(require("puppeteer"));
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
- return __awaiter(this, void 0, void 0, function* () {
60
- return yield new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
61
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
62
- /** Init browser */
63
- const browser = yield puppeteer_1.default.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] });
64
- const page = yield browser.newPage();
65
- /** Block useless ressources */
66
- yield page.setRequestInterception(true);
67
- page.on('request', (req) => {
68
- ['stylesheet', 'image', 'media', 'font', 'script', 'texttrack', 'xhr', 'fetch', 'eventsource', 'websocket', 'manifest', 'other'].includes(req.resourceType())
69
- ? req.abort()
70
- : req.continue();
71
- });
72
- /** Init user agent */
73
- yield page.setUserAgent((new user_agents_1.default()).toString());
74
- /** Init page */
75
- const response = yield page.goto(`${this.generateUrl()}?${this.serializeParams({
76
- [this.searchType]: this.searchValue,
77
- currency: this.currency,
78
- genre: this.genre,
79
- style: ((_a = this.style) === null || _a === void 0 ? void 0 : _a.length) ? this.style : undefined,
80
- format: ((_b = this.format) === null || _b === void 0 ? void 0 : _b.length) ? (_c = this.format) === null || _c === void 0 ? void 0 : _c[0] : undefined,
81
- format_desc: ((_d = this.formatDescription) === null || _d === void 0 ? void 0 : _d.length) ? this.formatDescription[0] : undefined,
82
- condition: ((_e = this.mediaCondition) === null || _e === void 0 ? void 0 : _e.length) ? this.mediaCondition[0] : undefined,
83
- year: this.year && !this.years ? this.year : undefined,
84
- 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,
85
- 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,
86
- audio: this.isAudioSample ? 1 : 0,
87
- offers: this.isMakeAnOfferOnly ? 1 : 0,
88
- ships_from: ((_k = this.from) === null || _k === void 0 ? void 0 : _k.length) ? this.from : undefined,
89
- limit: this.limit,
90
- page: this.page,
91
- sort: this.sort,
92
- })}`);
93
- /** Get url */
94
- this.url = page.url();
95
- /** Get HTML */
96
- const bodyHTML = yield page.evaluate(() => document.body.innerHTML);
97
- /** Close browser */
98
- yield browser.close();
99
- /** If error, reject */
100
- if (((_l = response === null || response === void 0 ? void 0 : response.headers()) === null || _l === void 0 ? void 0 : _l.status) && parseInt((_m = response === null || response === void 0 ? void 0 : response.headers()) === null || _m === void 0 ? void 0 : _m.status) >= 400)
101
- reject({ document: bodyHTML, status: (_o = response === null || response === void 0 ? void 0 : response.headers()) === null || _o === void 0 ? void 0 : _o.status });
102
- /** resolve */
103
- resolve({ document: bodyHTML });
104
- }))
105
- .then((res) => {
106
- let result = this.getItem((new jsdom_1.JSDOM(res.document)).window.document);
107
- return result;
108
- })
109
- .catch((err) => {
110
- var _a, _b, _c;
111
- const { document } = (new jsdom_1.JSDOM(err.document)).window;
112
- let result = {
113
- message: (_c = (_b = (_a = 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 occured',
114
- code: err.status
115
- };
116
- throw result;
117
- });
118
- });
119
- }
120
- /**
121
- * Generate URL to be parsed
122
- * @returns {string} Url
123
- */
124
- generateUrl() {
125
- let url = `https://www.discogs.com/${this.lang}/`;
126
- if (this.seller) {
127
- url += `seller/${this.seller}/`;
128
- url += this.searchType === EType_1.EType.USER ? `mywants` : 'profile';
129
- }
130
- else {
131
- url += `sell/`;
132
- url += this.searchType === EType_1.EType.USER ? `mywants` : `list`;
133
- }
134
- return url;
135
- }
136
- /**
137
- * Serialize params URL
138
- * @param {any} params Object of GET parameters
139
- * @returns {string} Url
140
- */
141
- serializeParams(params) {
142
- let param = [];
143
- for (let key in params) {
144
- if (params[key] === undefined || params[key] === null) {
145
- continue;
146
- }
147
- else if (Array.isArray(params[key])) {
148
- /** Custom handle for array */
149
- if (params[key].length > 0) {
150
- for (const el of params[key]) {
151
- param.push(key + '=' + encodeURIComponent(el));
152
- }
153
- }
154
- else {
155
- continue;
156
- }
157
- }
158
- else {
159
- param.push(key + '=' + encodeURIComponent(params[key]));
160
- }
161
- }
162
- return param.join('&');
163
- }
164
- /**
165
- * Function to convert devise to ISO name
166
- * @param str String to clean
167
- * @returns {string} Cleanup string
168
- */
169
- convertDevise(str) {
170
- if (!str)
171
- return str;
172
- const data = {
173
- '\$US': 'USD',
174
- '£GB': 'GBP',
175
- '\$CA': 'CAD',
176
- '\$AU': 'AUD',
177
- 'JPY': 'JPY',
178
- 'CHF': 'CHF',
179
- 'MX\$': 'MXN',
180
- '\$NZ': 'NZD',
181
- 'SEK': 'SEK',
182
- 'ZAR': 'ZAR',
183
- 'CA\$': 'AUD',
184
- 'A\$': 'AUD',
185
- 'R\$': 'BRL',
186
- '€': 'EUR',
187
- '\$': 'USD',
188
- '£': 'GBP',
189
- '¥': 'JPY',
190
- };
191
- Object.keys(data).forEach(x => {
192
- if (str.includes(`${x}`)) {
193
- str = str.replace(`${x}`, '');
194
- str = `${str} ${data[x]}`.replace(/\s\s+/g, ' '); //Replace multiple space by one
195
- }
196
- });
197
- return str;
198
- }
199
- /**
200
- * Parse HTML to clean result
201
- * @param {Document} document Document to parse
202
- * @returns {IOutputSuccess} Items found
203
- */
204
- getItem(document) {
205
- var _a, _b, _c, _d, _e;
206
- 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');
207
- return {
208
- result: ((_e = [...document.querySelectorAll('table.table_block tbody tr')]) === null || _e === void 0 ? void 0 : _e.map(el => {
209
- 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;
210
- 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(',', '.'));
211
- const have = parseInt((_g = el.querySelector('.community_summary .community_result:nth-child(1) .community_number')) === null || _g === void 0 ? void 0 : _g.textContent);
212
- const want = parseInt((_h = el.querySelector('.community_summary .community_result:nth-child(2) .community_number')) === null || _h === void 0 ? void 0 : _h.textContent);
213
- 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];
214
- 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');
215
- 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');
216
- return {
217
- itemId: isNaN(itemId) ? 0 : itemId,
218
- title: {
219
- original: (_v = el.querySelector('a.item_description_title')) === null || _v === void 0 ? void 0 : _v.textContent,
220
- 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],
221
- 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],
222
- },
223
- url: `https://www.discogs.com${(_2 = el.querySelector('a.item_description_title')) === null || _2 === void 0 ? void 0 : _2.href}`,
224
- 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))],
225
- 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'),
226
- imageUrl: (_8 = el.querySelector('.marketplace_image')) === null || _8 === void 0 ? void 0 : _8.getAttribute('data-src'),
227
- 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(),
228
- 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,
229
- isAvailable: !((_15 = el.classList) === null || _15 === void 0 ? void 0 : _15.contains('unavailable')),
230
- condition: {
231
- media: {
232
- 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(),
233
- 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]
234
- },
235
- sleeve: {
236
- full: (_24 = el.querySelector('.item_condition span.item_sleeve_condition')) === null || _24 === void 0 ? void 0 : _24.textContent,
237
- 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]
238
- }
239
- },
240
- seller: {
241
- name: (_28 = el.querySelector('.seller_info a')) === null || _28 === void 0 ? void 0 : _28.textContent,
242
- score: (_29 = el.querySelector('.seller_info li:nth-child(2) strong')) === null || _29 === void 0 ? void 0 : _29.textContent,
243
- 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]
244
- },
245
- price: {
246
- 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(/,/, '.')),
247
- shipping: isNaN(parseFloat(shipping)) ? null : shipping,
248
- },
249
- from: {
250
- countryName: country,
251
- isoCountryName: (_39 = (_38 = EFromIso_1.EFromIso[country]) !== null && _38 !== void 0 ? _38 : country) !== null && _39 !== void 0 ? _39 : '',
252
- isoCode: EFromIso_1.EFromIsoCode[(_41 = (_40 = EFromIso_1.EFromIso[country]) !== null && _40 !== void 0 ? _40 : country) !== null && _41 !== void 0 ? _41 : ''],
253
- },
254
- community: {
255
- have: isNaN(have) ? 0 : have,
256
- want: isNaN(want) ? 0 : want
257
- },
258
- releaseUrl: `https://www.discogs.com${(_42 = el.querySelector('a.item_release_link')) === null || _42 === void 0 ? void 0 : _42.href}`,
259
- releaseId: isNaN(releaseId) ? 0 : releaseId,
260
- };
261
- })) || [],
262
- page: {
263
- current: this.page,
264
- total: Math.ceil(totalItems / this.limit)
265
- },
266
- item: {
267
- total: totalItems,
268
- perPage: this.limit,
269
- },
270
- search: {
271
- value: this.searchValue,
272
- type: this.searchType
273
- },
274
- urlGenerated: this.url
275
- };
276
- }
277
- }
278
- 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/translates/EDiscogsCurrencyToIsoCode"));
35
+ const EDiscogsCountryNameToIsoCode_1 = __importDefault(require("./enum/iso/translates/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, _60;
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 = ((_r = Object.keys(ECountryCode_1.default)
213
+ .find((key) => ECountryName_1.default[key] === countryName
214
+ || EDiscogsCountryNameToIsoCode_1.default[countryName])) !== null && _r !== void 0 ? _r : '');
215
+ const releaseId = Number.parseInt((_w = (_v = (_u = (_t = (_s = el.querySelector('a.item_release_link')) === null || _s === void 0 ? void 0 : _s.href.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);
216
+ const itemId = Number.parseInt((_z = (_y = (_x = el.querySelector('a.item_description_title')) === null || _x === void 0 ? void 0 : _x.href) === null || _y === void 0 ? void 0 : _y.split('/').pop()) !== null && _z !== void 0 ? _z : '0', 10);
217
+ const notes = Number.parseInt((_6 = (_5 = (_4 = (_3 = (_2 = (_1 = (_0 = el.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);
218
+ const originalTitle = (_7 = el.querySelector('a.item_description_title')) === null || _7 === void 0 ? void 0 : _7.textContent;
219
+ const firstIndexOfDash = (_8 = originalTitle === null || originalTitle === void 0 ? void 0 : originalTitle.indexOf(' - ')) !== null && _8 !== void 0 ? _8 : -1;
220
+ const lastIndexOfParenthesis = (_9 = originalTitle === null || originalTitle === void 0 ? void 0 : originalTitle.lastIndexOf(' (')) !== null && _9 !== void 0 ? _9 : -1;
221
+ return {
222
+ itemId: Number.isNaN(itemId) ? 0 : itemId,
223
+ title: {
224
+ original: originalTitle !== null && originalTitle !== void 0 ? originalTitle : '',
225
+ artist: (_10 = originalTitle === null || originalTitle === void 0 ? void 0 : originalTitle.substring(0, firstIndexOfDash)) !== null && _10 !== void 0 ? _10 : '',
226
+ item: firstIndexOfDash > -1 && lastIndexOfParenthesis > -1
227
+ ? (_11 = originalTitle === null || originalTitle === void 0 ? void 0 : originalTitle.substring(firstIndexOfDash + 3, lastIndexOfParenthesis)) !== null && _11 !== void 0 ? _11 : ''
228
+ : '',
229
+ formats: lastIndexOfParenthesis > -1
230
+ ? ((_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 : [])
231
+ : [],
232
+ },
233
+ url: `https://www.discogs.com${(_14 = el.querySelector('a.item_description_title')) === null || _14 === void 0 ? void 0 : _14.href}`,
234
+ 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),
235
+ catnos: (_21 = (_20 = (_19 = (_18 = (_17 = el.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 : [],
236
+ imageUrl: (_23 = (_22 = el.querySelector('.marketplace_image')) === null || _22 === void 0 ? void 0 : _22.getAttribute('data-src')) !== null && _23 !== void 0 ? _23 : '',
237
+ 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 : '',
238
+ 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,
239
+ isAvailable: !((_33 = el.classList) === null || _33 === void 0 ? void 0 : _33.contains('unavailable')),
240
+ condition: {
241
+ media: {
242
+ 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 : '',
243
+ short: (_43 = (_42 = (_41 = (_40 = (_39 = (_38 = el.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.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 : '',
248
+ },
249
+ },
250
+ seller: {
251
+ name: (_51 = (_50 = el.querySelector('.seller_info a')) === null || _50 === void 0 ? void 0 : _50.textContent) !== null && _51 !== void 0 ? _51 : '',
252
+ url: `https://www.discogs.com${(_52 = el.querySelector('.seller_info a')) === null || _52 === void 0 ? void 0 : _52.href}`,
253
+ 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 : '',
254
+ notes: Number.isNaN(notes) ? 0 : notes,
255
+ },
256
+ price: {
257
+ base: this.convertDevise((_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 : ''),
258
+ shipping: Number.isNaN(parseFloat(shipping)) ? null : shipping,
259
+ },
260
+ from: {
261
+ countryName,
262
+ isoCountryName: (_59 = ECountryName_1.default[isoCountryCode]) !== null && _59 !== void 0 ? _59 : '',
263
+ isoCountryCode,
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${(_60 = el.querySelector('a.item_release_link')) === null || _60 === void 0 ? void 0 : _60.href}`,
272
+ },
273
+ };
274
+ })) || [],
275
+ page: {
276
+ current: this.page,
277
+ total: Math.ceil(totalItems / this.limit),
278
+ },
279
+ result: {
280
+ total: totalItems,
281
+ perPage: this.limit,
282
+ },
283
+ search: {
284
+ value: (_f = this.searchValue) !== null && _f !== void 0 ? _f : '',
285
+ type: this.searchType,
286
+ },
287
+ urlGenerated: this.url,
288
+ };
289
+ }
290
+ }
291
+ exports.default = Marketplace;