discogs-marketplace-api-nodejs 1.12.0 → 1.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.MD +151 -176
- package/dist/index.d.ts +1 -3
- package/dist/index.js +1 -2
- package/dist/interfaces/api/shop-details-result.api.interface.d.ts +33 -0
- package/dist/interfaces/api/shop-result.api.interface.d.ts +111 -0
- package/dist/interfaces/search-params.interface.d.ts +166 -63
- package/dist/interfaces/search-result.interface.d.ts +39 -26
- package/dist/scrapers/legacy.scraper.d.ts +11 -0
- package/dist/scrapers/legacy.scraper.js +200 -0
- package/dist/scrapers/modern.scraper.d.ts +11 -0
- package/dist/scrapers/modern.scraper.js +167 -0
- package/dist/scrapers/wantlist.scraper.d.ts +11 -0
- package/dist/scrapers/wantlist.scraper.js +53 -0
- package/dist/search.d.ts +6 -6
- package/dist/search.js +21 -134
- package/dist/types/api.type.d.ts +4 -0
- package/dist/types/api.type.js +2 -0
- package/dist/types/from.type.d.ts +2 -1
- package/package.json +2 -2
- package/dist/get-search-params.d.ts +0 -10
- package/dist/get-search-params.js +0 -122
- package/dist/scrape.d.ts +0 -10
- package/dist/scrape.js +0 -126
- package/dist/types/hours-range.type.d.ts +0 -4
- package/dist/types/search-type.type.d.ts +0 -4
- /package/dist/{types/hours-range.type.js → interfaces/api/shop-details-result.api.interface.js} +0 -0
- /package/dist/{types/search-type.type.js → interfaces/api/shop-result.api.interface.js} +0 -0
|
@@ -4,38 +4,70 @@ import type { FormatDescription } from '../types/format-description.type.js';
|
|
|
4
4
|
import type { Format } from '../types/format.type.js';
|
|
5
5
|
import type { From } from '../types/from.type.js';
|
|
6
6
|
import type { Genre } from '../types/genre.type.js';
|
|
7
|
-
import type { HoursRange } from '../types/hours-range.type.js';
|
|
8
7
|
import type { Lang } from '../types/lang.type.js';
|
|
9
8
|
import type { Limit } from '../types/limit.type.js';
|
|
10
|
-
import type {
|
|
9
|
+
import type { Api } from '../types/api.type.js';
|
|
11
10
|
import type { Sort } from '../types/sort.type.js';
|
|
12
11
|
import type { Style } from '../types/style.type.js';
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
interface SearchParamsBase {
|
|
13
|
+
/**
|
|
14
|
+
* List of formats.
|
|
15
|
+
* @example ['Vinyl', 'CD']
|
|
16
|
+
*/
|
|
17
|
+
formats?: Array<Format>;
|
|
18
|
+
/**
|
|
19
|
+
* List of format descriptions.
|
|
20
|
+
* @example ['Limited Edition', 'Numbered']
|
|
21
|
+
*/
|
|
22
|
+
formatDescriptions?: Array<FormatDescription>;
|
|
23
|
+
/**
|
|
24
|
+
* Range of years.
|
|
25
|
+
*/
|
|
26
|
+
years?: {
|
|
27
|
+
/** Minimum year */
|
|
28
|
+
min: number;
|
|
29
|
+
/** Maximum year */
|
|
30
|
+
max: number;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* If true, only listings that accept offers are returned.
|
|
34
|
+
* @default false
|
|
35
|
+
*/
|
|
36
|
+
isMakeAnOfferOnly?: boolean;
|
|
17
37
|
/**
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
|
|
38
|
+
* Number of results to return per page.
|
|
39
|
+
* Allowed values: 25, 50, 100, 250.
|
|
40
|
+
* @default 25
|
|
41
|
+
*/
|
|
42
|
+
limit?: Limit;
|
|
43
|
+
/**
|
|
44
|
+
* Page number of results.
|
|
45
|
+
* Must be less than 401 to avoid Discogs 404 errors.
|
|
46
|
+
* @default 1
|
|
47
|
+
*/
|
|
48
|
+
page?: number;
|
|
49
|
+
}
|
|
50
|
+
interface SearchParamsLegacyBase extends SearchParamsBase {
|
|
51
|
+
/**
|
|
52
|
+
* Api version to use.
|
|
53
|
+
*
|
|
54
|
+
* Choose `legacy` for the legacy API that scrapes HTML page. Required if you want to search on master or label.
|
|
28
55
|
*/
|
|
29
|
-
|
|
56
|
+
api: Extract<Api, 'legacy'>;
|
|
30
57
|
/**
|
|
31
|
-
*
|
|
58
|
+
* Query string to filter.
|
|
32
59
|
*/
|
|
33
|
-
|
|
60
|
+
query?: string;
|
|
34
61
|
/**
|
|
35
62
|
* Currency code for price filtering.
|
|
36
63
|
* @example 'USD'
|
|
37
64
|
*/
|
|
38
65
|
currency?: Currency;
|
|
66
|
+
/**
|
|
67
|
+
* Media conditions.
|
|
68
|
+
* @example 'Mint (M)'
|
|
69
|
+
*/
|
|
70
|
+
condition?: Condition;
|
|
39
71
|
/**
|
|
40
72
|
* Genre filter.
|
|
41
73
|
* @example 'Rock'
|
|
@@ -45,76 +77,147 @@ export default interface SearchParams {
|
|
|
45
77
|
* List of styles.
|
|
46
78
|
* @example ['Death Metal', 'Heavy Metal']
|
|
47
79
|
*/
|
|
48
|
-
|
|
80
|
+
styles?: Array<Style>;
|
|
49
81
|
/**
|
|
50
|
-
*
|
|
51
|
-
* @example
|
|
82
|
+
* Expedition country filter.
|
|
83
|
+
* @example 'France'
|
|
52
84
|
*/
|
|
53
|
-
|
|
85
|
+
from?: From;
|
|
54
86
|
/**
|
|
55
|
-
*
|
|
56
|
-
* @
|
|
87
|
+
* Sort order for the results.
|
|
88
|
+
* @default 'listed,desc'
|
|
57
89
|
*/
|
|
58
|
-
|
|
90
|
+
sort?: Sort;
|
|
91
|
+
/**
|
|
92
|
+
* Seller's name.
|
|
93
|
+
*/
|
|
94
|
+
seller?: string;
|
|
95
|
+
/**
|
|
96
|
+
* User's name.
|
|
97
|
+
*/
|
|
98
|
+
user?: string;
|
|
99
|
+
/**
|
|
100
|
+
* Language code for Discogs interface.
|
|
101
|
+
* @default 'en'
|
|
102
|
+
*/
|
|
103
|
+
lang?: Lang;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Parameters used to search listings on the Discogs Marketplace via new old HTML Scraping.
|
|
107
|
+
*/
|
|
108
|
+
export type SearchParamsLegacy = ({
|
|
109
|
+
/** Master release ID. */
|
|
110
|
+
masterId?: number;
|
|
111
|
+
labelId?: never;
|
|
112
|
+
releaseId?: never;
|
|
113
|
+
artistId?: never;
|
|
114
|
+
} | {
|
|
115
|
+
masterId?: never;
|
|
116
|
+
/** Label ID. */
|
|
117
|
+
labelId?: number;
|
|
118
|
+
releaseId?: never;
|
|
119
|
+
artistId?: never;
|
|
120
|
+
} | {
|
|
121
|
+
masterId?: never;
|
|
122
|
+
labelId?: never;
|
|
123
|
+
/** Release ID. */
|
|
124
|
+
releaseId?: number;
|
|
125
|
+
artistId?: never;
|
|
126
|
+
} | {
|
|
127
|
+
masterId?: never;
|
|
128
|
+
labelId?: never;
|
|
129
|
+
releaseId?: never;
|
|
130
|
+
/** Artist ID. */
|
|
131
|
+
artistId?: number;
|
|
132
|
+
}) & SearchParamsLegacyBase;
|
|
133
|
+
interface SearchParamsModernBase extends SearchParamsBase {
|
|
134
|
+
/**
|
|
135
|
+
* Api version to use.
|
|
136
|
+
*
|
|
137
|
+
* Choose `v2` for the modern API that uses JSON (used in the `https://www.discogs.com/shop/mywants` page).
|
|
138
|
+
*/
|
|
139
|
+
api: Extract<Api, 'v2'>;
|
|
140
|
+
/**
|
|
141
|
+
* List of currency codes for price filtering.
|
|
142
|
+
* @example ['USD']
|
|
143
|
+
*/
|
|
144
|
+
currencies?: Array<Currency>;
|
|
59
145
|
/**
|
|
60
146
|
* List of media conditions.
|
|
61
147
|
* @example ['Mint (M)', 'Very Good Plus (VG+)']
|
|
62
148
|
*/
|
|
63
|
-
|
|
149
|
+
conditions?: Array<Condition>;
|
|
64
150
|
/**
|
|
65
|
-
*
|
|
66
|
-
*
|
|
151
|
+
* List of genres.
|
|
152
|
+
* @example ['Rock']
|
|
67
153
|
*/
|
|
68
|
-
|
|
154
|
+
genres?: Array<Genre>;
|
|
69
155
|
/**
|
|
70
|
-
*
|
|
71
|
-
*
|
|
156
|
+
* Expedition country filter.
|
|
157
|
+
* @example 'France'
|
|
72
158
|
*/
|
|
73
|
-
|
|
74
|
-
/** Minimum year */
|
|
75
|
-
min: number;
|
|
76
|
-
/** Maximum year */
|
|
77
|
-
max: number;
|
|
78
|
-
};
|
|
159
|
+
from?: Array<From>;
|
|
79
160
|
/**
|
|
80
|
-
*
|
|
81
|
-
* @default
|
|
161
|
+
* Sort order for the results.
|
|
162
|
+
* @default 'listed,desc'
|
|
82
163
|
*/
|
|
83
|
-
|
|
164
|
+
sort?: Exclude<Sort, 'label,asc' | 'label,desc'>;
|
|
84
165
|
/**
|
|
85
|
-
*
|
|
86
|
-
* @example 'US'
|
|
166
|
+
* List of artist IDs.
|
|
87
167
|
*/
|
|
88
|
-
|
|
168
|
+
artistIds?: Array<number>;
|
|
89
169
|
/**
|
|
90
|
-
*
|
|
170
|
+
* List of seller IDs.
|
|
171
|
+
*
|
|
172
|
+
* You can easily find someone's ID via the Discogs API: https://api.discogs.com/users/{seller_name}
|
|
91
173
|
*/
|
|
92
|
-
|
|
174
|
+
sellerIds?: Array<number>;
|
|
93
175
|
/**
|
|
94
|
-
*
|
|
95
|
-
* @example '0-24'
|
|
176
|
+
* Price range filter.
|
|
96
177
|
*/
|
|
97
|
-
|
|
178
|
+
priceRange?: {
|
|
179
|
+
/** Minimum price */
|
|
180
|
+
min: number;
|
|
181
|
+
/** Maximum price */
|
|
182
|
+
max: number;
|
|
183
|
+
};
|
|
98
184
|
/**
|
|
99
|
-
*
|
|
100
|
-
* @default
|
|
185
|
+
* Minimum seller rating, from 0 to 100.
|
|
186
|
+
* @default 0
|
|
101
187
|
*/
|
|
102
|
-
|
|
188
|
+
sellerRatingMin?: number;
|
|
103
189
|
/**
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
* @default 25
|
|
190
|
+
* Include generic sleeves.
|
|
191
|
+
* @default true
|
|
107
192
|
*/
|
|
108
|
-
|
|
193
|
+
includeGenericSleeves?: boolean;
|
|
109
194
|
/**
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
* @default 1
|
|
195
|
+
* Include sleeveless media.
|
|
196
|
+
* @default true
|
|
113
197
|
*/
|
|
114
|
-
|
|
198
|
+
includeSleevelessMedia?: boolean;
|
|
115
199
|
/**
|
|
116
|
-
*
|
|
117
|
-
* @default
|
|
200
|
+
* Show unavailable items.
|
|
201
|
+
* @default true
|
|
118
202
|
*/
|
|
119
|
-
|
|
203
|
+
showUnavailable?: boolean;
|
|
204
|
+
/**
|
|
205
|
+
* Minimum seller rating count.
|
|
206
|
+
* @default 0
|
|
207
|
+
*/
|
|
208
|
+
sellerRatingCountMin?: number;
|
|
120
209
|
}
|
|
210
|
+
/**
|
|
211
|
+
* Parameters used to search listings on the Discogs Marketplace via new new modern API.
|
|
212
|
+
*/
|
|
213
|
+
export type SearchParamsModern = ({
|
|
214
|
+
/** User's wantlist name. */
|
|
215
|
+
wantlist?: string;
|
|
216
|
+
releaseIds?: never;
|
|
217
|
+
} | {
|
|
218
|
+
wantlist?: never;
|
|
219
|
+
/** List of release IDs. */
|
|
220
|
+
releaseIds?: Array<number>;
|
|
221
|
+
}) & SearchParamsModernBase;
|
|
222
|
+
export type SearchParams = SearchParamsLegacy | SearchParamsModern;
|
|
223
|
+
export {};
|
|
@@ -8,27 +8,47 @@ export default interface SearchResult {
|
|
|
8
8
|
items: Array<{
|
|
9
9
|
/** Unique identifier for the listing */
|
|
10
10
|
id: number;
|
|
11
|
-
/**
|
|
12
|
-
title:
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
/** Full title of the listing */
|
|
12
|
+
title: string;
|
|
13
|
+
/** Artists details */
|
|
14
|
+
artists: Array<{
|
|
15
|
+
/** Artist ID */
|
|
16
|
+
id: number | null;
|
|
15
17
|
/** Artist name */
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
name: string;
|
|
19
|
+
/** Artist URL */
|
|
20
|
+
url: string | null;
|
|
21
|
+
}>;
|
|
22
|
+
/** Release details */
|
|
23
|
+
release: {
|
|
24
|
+
/** Release ID */
|
|
25
|
+
id: number;
|
|
26
|
+
/** Release name */
|
|
27
|
+
name: string;
|
|
28
|
+
/** Release URL */
|
|
29
|
+
url: string;
|
|
21
30
|
};
|
|
31
|
+
/** List of formats */
|
|
32
|
+
formats: Array<string>;
|
|
33
|
+
/** labels */
|
|
34
|
+
labels: Array<{
|
|
35
|
+
/** Label ID */
|
|
36
|
+
id: number;
|
|
37
|
+
/** Label name */
|
|
38
|
+
name: string;
|
|
39
|
+
/** Label URL */
|
|
40
|
+
url: string;
|
|
41
|
+
}>;
|
|
22
42
|
/** URL to the listing on Discogs */
|
|
23
43
|
url: string;
|
|
24
|
-
/**
|
|
25
|
-
|
|
44
|
+
/** Date the item was listed */
|
|
45
|
+
listedAt: Date | null;
|
|
26
46
|
/** Array of catalog numbers */
|
|
27
47
|
catnos: Array<string>;
|
|
28
48
|
/** URL to the item's image */
|
|
29
|
-
imageUrl: string;
|
|
49
|
+
imageUrl: string | null;
|
|
30
50
|
/** Text description of the item */
|
|
31
|
-
description: string;
|
|
51
|
+
description: string | null;
|
|
32
52
|
/** Indicates if offers are accepted */
|
|
33
53
|
isAcceptingOffer: boolean;
|
|
34
54
|
/** Availability status of the item */
|
|
@@ -45,9 +65,9 @@ export default interface SearchResult {
|
|
|
45
65
|
/** Sleeve condition */
|
|
46
66
|
sleeve: {
|
|
47
67
|
/** Full description */
|
|
48
|
-
full: string;
|
|
68
|
+
full: string | null;
|
|
49
69
|
/** Short code */
|
|
50
|
-
short: string;
|
|
70
|
+
short: string | null;
|
|
51
71
|
};
|
|
52
72
|
};
|
|
53
73
|
/** Seller information */
|
|
@@ -57,9 +77,9 @@ export default interface SearchResult {
|
|
|
57
77
|
/** Seller's profile URL */
|
|
58
78
|
url: string;
|
|
59
79
|
/** Seller rating/score */
|
|
60
|
-
score: string;
|
|
80
|
+
score: string | null;
|
|
61
81
|
/** Number of notes/reviews */
|
|
62
|
-
notes: number;
|
|
82
|
+
notes: number | null;
|
|
63
83
|
};
|
|
64
84
|
/** Pricing details */
|
|
65
85
|
price: {
|
|
@@ -67,12 +87,12 @@ export default interface SearchResult {
|
|
|
67
87
|
* Base price as a string combining amount and currency.
|
|
68
88
|
* @example '12.34 USD'
|
|
69
89
|
*/
|
|
70
|
-
base: `${number} ${CurrencyValues}
|
|
90
|
+
base: `${number} ${CurrencyValues}`;
|
|
71
91
|
/**
|
|
72
92
|
* Shipping cost as a string combining amount and currency.
|
|
73
93
|
* @example '5.67 USD'
|
|
74
94
|
*/
|
|
75
|
-
shipping: `${number} ${CurrencyValues}` |
|
|
95
|
+
shipping: `${number} ${CurrencyValues}` | null;
|
|
76
96
|
};
|
|
77
97
|
/** Shipping origin country */
|
|
78
98
|
country: {
|
|
@@ -91,13 +111,6 @@ export default interface SearchResult {
|
|
|
91
111
|
/** Number of users who want this item */
|
|
92
112
|
want: number;
|
|
93
113
|
};
|
|
94
|
-
/** Release details */
|
|
95
|
-
release: {
|
|
96
|
-
/** Release ID */
|
|
97
|
-
id: number;
|
|
98
|
-
/** Release URL */
|
|
99
|
-
url: string;
|
|
100
|
-
};
|
|
101
114
|
}>;
|
|
102
115
|
/** Pagination information */
|
|
103
116
|
page: {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type SearchResult from '../interfaces/search-result.interface.js';
|
|
2
|
+
import type { SearchParamsLegacy } from '../interfaces/search-params.interface.js';
|
|
3
|
+
/**
|
|
4
|
+
* Parses the page and returns the items found.
|
|
5
|
+
* @param url Url to scrape
|
|
6
|
+
* @returns Items found and total
|
|
7
|
+
*/
|
|
8
|
+
export default function scrapeLegacy({ currency, genre, styles, formats, formatDescriptions, condition, years, isMakeAnOfferOnly, from, sort, limit, page, seller, lang, masterId, labelId, artistId, releaseId, user, query, }: SearchParamsLegacy): Promise<Pick<SearchResult, 'items' | 'urlGenerated'> & {
|
|
9
|
+
/** Total items found */
|
|
10
|
+
total: SearchResult['result']['total'];
|
|
11
|
+
}>;
|
|
@@ -0,0 +1,200 @@
|
|
|
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
|
+
exports.default = scrapeLegacy;
|
|
7
|
+
const linkedom_1 = require("linkedom");
|
|
8
|
+
const currency_data_1 = __importDefault(require("../data/currency.data.js"));
|
|
9
|
+
const country_data_1 = __importDefault(require("../data/country.data.js"));
|
|
10
|
+
const search_1 = require("../search.js");
|
|
11
|
+
/**
|
|
12
|
+
* Generate URL to be parsed
|
|
13
|
+
* @returns Url
|
|
14
|
+
*/
|
|
15
|
+
function generateUrl({ seller, lang, user, }) {
|
|
16
|
+
const baseUrl = `https://www.discogs.com/${lang}`;
|
|
17
|
+
if (seller) {
|
|
18
|
+
const path = user ? 'mywants' : 'profile';
|
|
19
|
+
return `${baseUrl}/seller/${seller}/${path}`;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
return `${baseUrl}/sell/list`;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Converts a currency string to a standardized format.
|
|
27
|
+
* @param value String to clean
|
|
28
|
+
* @returns Cleaned-up string
|
|
29
|
+
*/
|
|
30
|
+
function convertCurrency(value) {
|
|
31
|
+
if (!value) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
const currencyFound = Object.keys(currency_data_1.default).find(key => value.includes(key));
|
|
35
|
+
if (!currencyFound) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
const currencyClean = currency_data_1.default[currencyFound];
|
|
39
|
+
const amount = value
|
|
40
|
+
.replace(currencyClean !== 'JPY' ? /[.](?=.*[.])/g : /\./g, '') // Remove all dots but the last, except for JPY
|
|
41
|
+
.replace(currencyFound, '') // Remove original currency
|
|
42
|
+
.replace(/\s\s+/g, ''); // Remove spaces
|
|
43
|
+
const amountParsed = currencyClean === 'JPY' ? Number.parseInt(amount) : Number.parseFloat(amount);
|
|
44
|
+
if (Number.isNaN(amountParsed)) {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
return currencyClean === 'JPY' ? `${amountParsed} ${currencyClean}` : `${amountParsed.toFixed(2)} ${currencyClean}`;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Safely parses a number from a string.
|
|
51
|
+
* @param value String to parse
|
|
52
|
+
* @returns Parsed number or 0 if invalid
|
|
53
|
+
*/
|
|
54
|
+
function safeParseInt(value) {
|
|
55
|
+
const parsed = Number.parseInt(value ?? '', 10);
|
|
56
|
+
return Number.isNaN(parsed) ? 0 : parsed;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Parses the page and returns the items found.
|
|
60
|
+
* @param url Url to scrape
|
|
61
|
+
* @returns Items found and total
|
|
62
|
+
*/
|
|
63
|
+
async function scrapeLegacy({ currency, genre, styles, formats, formatDescriptions, condition, years, isMakeAnOfferOnly = false, from, sort = search_1.DEFAULT_SORT, limit = search_1.DEFAULT_LIMIT, page = search_1.DEFAULT_PAGE, seller, lang = 'en', masterId, labelId, artistId, releaseId, user, query, }) {
|
|
64
|
+
/** Url to be called by the browser */
|
|
65
|
+
const urlGenerated = [
|
|
66
|
+
generateUrl({ seller, lang, user }),
|
|
67
|
+
new URLSearchParams([
|
|
68
|
+
['master_id', masterId?.toString() ?? ''],
|
|
69
|
+
['label_id', labelId?.toString() ?? ''],
|
|
70
|
+
['artist_id', artistId?.toString() ?? ''],
|
|
71
|
+
['release_id', releaseId?.toString() ?? ''],
|
|
72
|
+
['user', user?.toString() ?? ''],
|
|
73
|
+
['q', query ?? ''],
|
|
74
|
+
['currency', currency ?? ''],
|
|
75
|
+
['genre', genre ?? ''],
|
|
76
|
+
...(styles?.map(s => ['style', s]) ?? []),
|
|
77
|
+
...(formats?.map(f => ['format', f]) ?? []),
|
|
78
|
+
...(formatDescriptions?.map(fd => ['format_desc', fd]) ?? []),
|
|
79
|
+
['condition', condition ?? ''],
|
|
80
|
+
['year1', years?.min ? years.min.toString() : ''],
|
|
81
|
+
['year2', years?.max ? years.max.toString() : ''],
|
|
82
|
+
['offers', isMakeAnOfferOnly ? '1' : ''],
|
|
83
|
+
['ships_from', Object.keys(country_data_1.default).find(key => from === country_data_1.default[key]) ?? ''],
|
|
84
|
+
['limit', limit.toString()],
|
|
85
|
+
['page', page.toString()],
|
|
86
|
+
['sort', sort],
|
|
87
|
+
].filter(([, p]) => !!p)).toString(),
|
|
88
|
+
].join('?');
|
|
89
|
+
const headers = new Headers({
|
|
90
|
+
'User-Agent': 'Discogs',
|
|
91
|
+
'Content-Type': 'application/json',
|
|
92
|
+
});
|
|
93
|
+
// If there a seller and user, we cannot use PJAX otherwise 404 error
|
|
94
|
+
if (!(seller && user)) {
|
|
95
|
+
headers.set('X-PJAX', 'true');
|
|
96
|
+
}
|
|
97
|
+
const response = await globalThis.fetch(urlGenerated, {
|
|
98
|
+
headers,
|
|
99
|
+
referrer: 'https://discogs.com',
|
|
100
|
+
});
|
|
101
|
+
const content = await response.text();
|
|
102
|
+
const { document } = (0, linkedom_1.parseHTML)(content);
|
|
103
|
+
// If error status, reject
|
|
104
|
+
if (response.status >= 400) {
|
|
105
|
+
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
|
106
|
+
const errorMessage = document.querySelector('h1 + p')?.innerHTML.trim() || `An error ${response.status} occurred.`;
|
|
107
|
+
throw new Error(errorMessage);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Extracts and cleans text content from an element.
|
|
111
|
+
* @param selector CSS selector
|
|
112
|
+
* @param context Parent element to query from
|
|
113
|
+
* @returns Cleaned text content or an empty string
|
|
114
|
+
*/
|
|
115
|
+
const getTextContent = (selector, context = document) => context.querySelector(selector)?.textContent?.trim() ?? '';
|
|
116
|
+
const total = safeParseInt(getTextContent('.pagination_total')
|
|
117
|
+
.split(' ')
|
|
118
|
+
.filter(x => x)
|
|
119
|
+
.pop()
|
|
120
|
+
?.replace(/(,|\.|\s)/g, ''));
|
|
121
|
+
const items = [...document.querySelectorAll('table.table_block tbody tr')].map(el => {
|
|
122
|
+
const itemHref = el.querySelector('a.item_description_title')?.getAttribute('href') ?? '';
|
|
123
|
+
const mediaCondition = getTextContent('.item_condition span:nth-child(3)', el).split('\n')[0] ?? '';
|
|
124
|
+
const sleeveCondition = getTextContent('.item_condition span.item_sleeve_condition', el);
|
|
125
|
+
const countryName = getTextContent('.seller_info li:nth-child(3)', el).split(':').pop() ?? '';
|
|
126
|
+
const originalTitle = getTextContent('a.item_description_title', el);
|
|
127
|
+
const firstIndexOfDash = originalTitle.indexOf(' - ');
|
|
128
|
+
const lastIndexOfParenthesis = originalTitle.lastIndexOf(' (');
|
|
129
|
+
const releaseHref = el.querySelector('a.item_release_link')?.getAttribute('href') ?? '';
|
|
130
|
+
return {
|
|
131
|
+
id: safeParseInt(itemHref.split('/').pop()),
|
|
132
|
+
title: originalTitle,
|
|
133
|
+
artists: [
|
|
134
|
+
{
|
|
135
|
+
id: null,
|
|
136
|
+
name: firstIndexOfDash > -1 ? originalTitle.substring(0, firstIndexOfDash) : '',
|
|
137
|
+
url: null,
|
|
138
|
+
},
|
|
139
|
+
],
|
|
140
|
+
release: {
|
|
141
|
+
id: safeParseInt(releaseHref.split('release/').pop()?.split('-').shift()),
|
|
142
|
+
name: firstIndexOfDash > -1 && lastIndexOfParenthesis > -1
|
|
143
|
+
? originalTitle.substring(firstIndexOfDash + 3, lastIndexOfParenthesis)
|
|
144
|
+
: '',
|
|
145
|
+
url: `https://www.discogs.com${releaseHref}`,
|
|
146
|
+
},
|
|
147
|
+
formats: lastIndexOfParenthesis > -1
|
|
148
|
+
? originalTitle.substring(lastIndexOfParenthesis + 2, originalTitle.length - 1).split(', ')
|
|
149
|
+
: [],
|
|
150
|
+
labels: [...el.querySelectorAll(".label_and_cat a[href^='https://www.discogs.com/']")]
|
|
151
|
+
.map(x => ({
|
|
152
|
+
id: safeParseInt(x.getAttribute('href')?.split('label/').pop()?.split('-').shift()),
|
|
153
|
+
name: x.textContent?.trim() ?? '',
|
|
154
|
+
url: `https://www.discogs.com${x.getAttribute('href') ?? ''}`,
|
|
155
|
+
}))
|
|
156
|
+
.filter((value, index, self) => self.findIndex(v => v.name === value.name) === index),
|
|
157
|
+
url: `https://www.discogs.com${itemHref}`,
|
|
158
|
+
listedAt: null,
|
|
159
|
+
catnos: getTextContent('.label_and_cat .item_catno', el) // cspell:disable-line
|
|
160
|
+
.split(', ')
|
|
161
|
+
.filter(x => x !== 'none'),
|
|
162
|
+
imageUrl: el.querySelector('.marketplace_image')?.getAttribute('data-src') ?? null,
|
|
163
|
+
description: getTextContent('.item_condition + *', el) || null,
|
|
164
|
+
isAcceptingOffer: getTextContent('.item_add_to_cart p a strong', el).split('/').length > 1,
|
|
165
|
+
isAvailable: !el.classList.contains('unavailable'),
|
|
166
|
+
condition: {
|
|
167
|
+
media: {
|
|
168
|
+
full: mediaCondition,
|
|
169
|
+
short: /\(([^)]+)\)/.exec(mediaCondition)?.[1] ?? '',
|
|
170
|
+
},
|
|
171
|
+
sleeve: {
|
|
172
|
+
full: sleeveCondition || null,
|
|
173
|
+
short: /\(([^)]+)\)/.exec(sleeveCondition)?.[1] ?? null,
|
|
174
|
+
},
|
|
175
|
+
},
|
|
176
|
+
seller: {
|
|
177
|
+
name: getTextContent('.seller_info a', el),
|
|
178
|
+
url: `https://www.discogs.com${el.querySelector('.seller_info a')?.getAttribute('href') ?? ''}`,
|
|
179
|
+
score: getTextContent('.seller_info li:nth-child(2) strong', el) || null,
|
|
180
|
+
notes: safeParseInt(getTextContent('.seller_info li:nth-child(2) .section_link', el)) || null,
|
|
181
|
+
},
|
|
182
|
+
price: {
|
|
183
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
184
|
+
base: convertCurrency(getTextContent('.price', el).replace(/,/, '.')),
|
|
185
|
+
shipping: convertCurrency(getTextContent('.item_shipping', el)
|
|
186
|
+
.replace(/(\s+|\+)/g, ' ')
|
|
187
|
+
.replace(',', '.')),
|
|
188
|
+
},
|
|
189
|
+
country: {
|
|
190
|
+
name: countryName,
|
|
191
|
+
code: country_data_1.default[countryName],
|
|
192
|
+
},
|
|
193
|
+
community: {
|
|
194
|
+
have: safeParseInt(getTextContent('.community_summary .community_result:nth-child(1) .community_number', el)),
|
|
195
|
+
want: safeParseInt(getTextContent('.community_summary .community_result:nth-child(2) .community_number', el)),
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
});
|
|
199
|
+
return { total, items, urlGenerated };
|
|
200
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type SearchResult from '../interfaces/search-result.interface.js';
|
|
2
|
+
import type { SearchParamsModern } from '../interfaces/search-params.interface.js';
|
|
3
|
+
/**
|
|
4
|
+
* Parses the page and returns the items found.
|
|
5
|
+
* @param SearchParams Search parameters
|
|
6
|
+
* @returns Items found and total
|
|
7
|
+
*/
|
|
8
|
+
export default function scrape({ currencies, genres, formats, formatDescriptions, conditions, years, isMakeAnOfferOnly, from, sort, limit, page, releaseIds, sellerIds, artistIds, priceRange, sellerRatingMin, includeGenericSleeves, includeSleevelessMedia, showUnavailable, sellerRatingCountMin, }: SearchParamsModern): Promise<Pick<SearchResult, 'items' | 'urlGenerated'> & {
|
|
9
|
+
/** Total items found */
|
|
10
|
+
total: SearchResult['result']['total'];
|
|
11
|
+
}>;
|