discogs-marketplace-api-nodejs 1.18.2 → 1.18.3
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.
|
@@ -7,4 +7,4 @@ import type { BrowserContext } from 'patchright';
|
|
|
7
7
|
* @param browserContext Patchright browser context
|
|
8
8
|
* @returns Items found, total, next cursor and url of the same search on Discogs
|
|
9
9
|
*/
|
|
10
|
-
export default function scrapeMarketplace(
|
|
10
|
+
export default function scrapeMarketplace(searchParams: SearchParams, browserContext: BrowserContext): Promise<SearchResult>;
|
|
@@ -91,12 +91,13 @@ async function queryGraphql(page, operationName, variables) {
|
|
|
91
91
|
response = await run();
|
|
92
92
|
}
|
|
93
93
|
const { status, cfMitigated, cfRay, json } = response;
|
|
94
|
+
// eslint-disable-next-line typescript/no-unsafe-type-assertion -- The GraphQL answer has the shape of the query it answers, which the caller names
|
|
94
95
|
const result = json;
|
|
95
96
|
if (cfMitigated) {
|
|
96
97
|
throw new Error(`An error ${status} occurred: blocked by Cloudflare (${cfMitigated}, cf-ray ${cfRay ?? '?'}).`);
|
|
97
98
|
}
|
|
98
99
|
if (!result?.data) {
|
|
99
|
-
// eslint-disable-next-line
|
|
100
|
+
// eslint-disable-next-line typescript/prefer-nullish-coalescing -- An empty list of errors joins into an empty message, which falls back too
|
|
100
101
|
throw new Error(result?.errors?.map(x => x.message).join(', ') || `An error ${status} occurred.`);
|
|
101
102
|
}
|
|
102
103
|
return result;
|
|
@@ -109,6 +110,7 @@ async function queryGraphql(page, operationName, variables) {
|
|
|
109
110
|
* @returns Price
|
|
110
111
|
*/
|
|
111
112
|
function formatPrice(price) {
|
|
113
|
+
// eslint-disable-next-line typescript/no-unsafe-type-assertion -- toFixed keeps the trailing zeros, which the `${number}` of the return type cannot express
|
|
112
114
|
return `${price?.currency === 'JPY' ? (price.amount ?? 0) : (price?.amount ?? 0).toFixed(2)} ${price?.currency ?? ''}`;
|
|
113
115
|
}
|
|
114
116
|
/**
|
|
@@ -121,7 +123,7 @@ function toCondition(value) {
|
|
|
121
123
|
return { full, short: /\(([^)]+)\)/.exec(full ?? '')?.[1] ?? null };
|
|
122
124
|
}
|
|
123
125
|
/** HTML entities the notes of the sellers can hold, besides the numeric ones */
|
|
124
|
-
const HTML_ENTITIES = { quot: '"', apos: "'", lt: '<', gt: '>', nbsp: '\
|
|
126
|
+
const HTML_ENTITIES = { quot: '"', apos: "'", lt: '<', gt: '>', nbsp: '\u00A0' }; // cspell: disable-line
|
|
125
127
|
/**
|
|
126
128
|
* Decode the HTML entities of a text, as the notes of the sellers come HTML escaped
|
|
127
129
|
* @param text Text
|
|
@@ -129,10 +131,10 @@ const HTML_ENTITIES = { quot: '"', apos: "'", lt: '<', gt: '>', nbsp: '\u00a0' }
|
|
|
129
131
|
*/
|
|
130
132
|
function decodeHtmlEntities(text) {
|
|
131
133
|
return text
|
|
132
|
-
.
|
|
133
|
-
.
|
|
134
|
-
.
|
|
135
|
-
.
|
|
134
|
+
.replaceAll(/&#(\d+);/g, (_, code) => String.fromCodePoint(Number(code)))
|
|
135
|
+
.replaceAll(/&#x([\da-f]+);/gi, (_, code) => String.fromCodePoint(parseInt(code, 16)))
|
|
136
|
+
.replaceAll(new RegExp(`&(${Object.keys(HTML_ENTITIES).join('|')});`, 'g'), (_, name) => HTML_ENTITIES[name] ?? '')
|
|
137
|
+
.replaceAll('&', '&');
|
|
136
138
|
}
|
|
137
139
|
/**
|
|
138
140
|
* Searches the listings through the `MarketplaceSearch` GraphQL query of the Discogs shop.
|
|
@@ -140,14 +142,17 @@ function decodeHtmlEntities(text) {
|
|
|
140
142
|
* @param browserContext Patchright browser context
|
|
141
143
|
* @returns Items found, total, next cursor and url of the same search on Discogs
|
|
142
144
|
*/
|
|
143
|
-
async function scrapeMarketplace(
|
|
145
|
+
async function scrapeMarketplace(searchParams, browserContext) {
|
|
146
|
+
const { query, artistIds, labelIds, masterIds, releaseIds, sellerIds, genres, styles, formats, formatDescriptions, mediaConditions, sleeveConditions, from, currencies, priceRange, sellerRatingMin, sellerRatingCountMin, years, isMakeAnOfferOnly = false, hasItemPhotos = false, hideGenericSleeves = false, hideSleevelessMedia = false, showUnavailable = true, withCommunityStats = true, sort = 'listed,desc', limit = 25, after, } = searchParams;
|
|
144
147
|
const [sortField = 'listed', sortOrder = 'desc'] = sort.split(',');
|
|
145
148
|
const countries = from?.flatMap(code => {
|
|
146
|
-
const names = Object.
|
|
147
|
-
|
|
149
|
+
const names = Object.entries(country_data_1.default)
|
|
150
|
+
.filter(([, value]) => value === code)
|
|
151
|
+
.map(([key]) => key);
|
|
152
|
+
return names.length > 0 ? names : [code];
|
|
148
153
|
});
|
|
149
154
|
const styleIds = styles?.flatMap(name => {
|
|
150
|
-
const ids = style_data_1.default
|
|
155
|
+
const ids = new Map(Object.entries(style_data_1.default)).get(name);
|
|
151
156
|
return ids ? [ids] : [];
|
|
152
157
|
}) ?? [];
|
|
153
158
|
/** Url of the same search on the Discogs shop */
|
|
@@ -198,8 +203,8 @@ async function scrapeMarketplace({ query, artistIds, labelIds, masterIds, releas
|
|
|
198
203
|
seller: sellerIds,
|
|
199
204
|
genre: genres,
|
|
200
205
|
genreAnd: genres?.length ? true : undefined,
|
|
201
|
-
styleId: styleIds.length ? [...new Set(styleIds.flat())] : undefined,
|
|
202
|
-
styleIdAnd: styleIds.length ? styleIds.every(x => x.length === 1) : undefined,
|
|
206
|
+
styleId: styleIds.length > 0 ? [...new Set(styleIds.flat())] : undefined,
|
|
207
|
+
styleIdAnd: styleIds.length > 0 ? styleIds.every(x => x.length === 1) : undefined,
|
|
203
208
|
formatName: formats,
|
|
204
209
|
formatNameAnd: formats?.length ? true : undefined,
|
|
205
210
|
formatDescription: formatDescriptions,
|
|
@@ -219,10 +224,11 @@ async function scrapeMarketplace({ query, artistIds, labelIds, masterIds, releas
|
|
|
219
224
|
hideGenericSleeves: hideGenericSleeves || undefined,
|
|
220
225
|
hideSleevelessMedia: hideSleevelessMedia || undefined,
|
|
221
226
|
showUnavailable: showUnavailable || undefined,
|
|
222
|
-
}).filter(([, x]) => x !== undefined && !(Array.isArray(x) &&
|
|
227
|
+
}).filter(([, x]) => x !== undefined && !(Array.isArray(x) && x.length === 0)));
|
|
223
228
|
const result = (await queryGraphql(browserPage, 'MarketplaceSearch', {
|
|
224
|
-
|
|
225
|
-
|
|
229
|
+
// eslint-disable-next-line typescript/prefer-nullish-coalescing -- An empty query searches everything, as no query
|
|
230
|
+
query: query || undefined,
|
|
231
|
+
filter: Object.keys(filter).length > 0 ? filter : undefined,
|
|
226
232
|
sort: {
|
|
227
233
|
dimension: SORT_DIMENSIONS[sortField] ?? SORT_DIMENSIONS.listed,
|
|
228
234
|
direction: sortOrder === 'asc' ? 'ASCENDING' : 'DESCENDING',
|
|
@@ -233,10 +239,10 @@ async function scrapeMarketplace({ query, artistIds, labelIds, masterIds, releas
|
|
|
233
239
|
const nodes = result?.edges?.map(x => x.node ?? {}) ?? [];
|
|
234
240
|
// The community stats and the rating of the releases are not part of the listings
|
|
235
241
|
const releaseIdsFound = withCommunityStats ? [...new Set(nodes.map(x => x.release?.discogsId ?? 0).filter(x => !!x))] : [];
|
|
236
|
-
const { data: details } = releaseIdsFound.length
|
|
242
|
+
const { data: details } = releaseIdsFound.length > 0
|
|
237
243
|
? await queryGraphql(browserPage, 'Releases', { discogsIds: releaseIdsFound, first: 1 })
|
|
238
244
|
: { data: undefined };
|
|
239
|
-
const releaseDetails = new Map(details?.releases?.map(x => [x?.discogsId ?? 0, x])
|
|
245
|
+
const releaseDetails = new Map(details?.releases?.map(x => [x?.discogsId ?? 0, x]));
|
|
240
246
|
const items = nodes.map(node => {
|
|
241
247
|
const release = node.release ?? {};
|
|
242
248
|
const artists = release.primaryArtists ?? [];
|
|
@@ -259,7 +265,8 @@ async function scrapeMarketplace({ query, artistIds, labelIds, masterIds, releas
|
|
|
259
265
|
url: `https://www.discogs.com/release/${release.discogsId ?? 0}`,
|
|
260
266
|
year: year || null,
|
|
261
267
|
country: release.country ?? null,
|
|
262
|
-
|
|
268
|
+
// eslint-disable-next-line typescript/prefer-nullish-coalescing -- A release without votes has a 0 average, shown as no rating
|
|
269
|
+
rating: releaseDetail?.ratings?.averageRating || null,
|
|
263
270
|
},
|
|
264
271
|
formats: [
|
|
265
272
|
...new Set([
|
|
@@ -277,7 +284,7 @@ async function scrapeMarketplace({ query, artistIds, labelIds, masterIds, releas
|
|
|
277
284
|
listedAt: node.listedDate ? new Date(node.listedDate) : null,
|
|
278
285
|
catnos: labels.map(x => x.catalogNumber ?? '').filter(x => !!x),
|
|
279
286
|
imageUrl: release.images?.edges?.[0]?.node?.thumbnail?.webpUrl ?? null,
|
|
280
|
-
photos:
|
|
287
|
+
photos: (node.itemPhotos ?? []).toSorted((a, b) => (a.position ?? 0) - (b.position ?? 0)).map(x => x.url ?? ''),
|
|
281
288
|
description: node.publicNotes ? decodeHtmlEntities(node.publicNotes) : null,
|
|
282
289
|
isAcceptingOffer: node.offerOk ?? false,
|
|
283
290
|
isAvailable: node.availability?.isAvailable ?? true,
|
|
@@ -310,7 +317,7 @@ async function scrapeMarketplace({ query, artistIds, labelIds, masterIds, releas
|
|
|
310
317
|
return {
|
|
311
318
|
items,
|
|
312
319
|
total: result?.totalCount ?? 0,
|
|
313
|
-
// eslint-disable-next-line
|
|
320
|
+
// eslint-disable-next-line typescript/prefer-nullish-coalescing -- No next page or an empty cursor both mean the last page
|
|
314
321
|
nextCursor: (result?.pageInfo?.hasNextPage && result.pageInfo.endCursor) || null,
|
|
315
322
|
urlGenerated,
|
|
316
323
|
};
|
package/dist/wantlist.d.ts
CHANGED
|
@@ -8,8 +8,10 @@ export type WantlistOptions = {
|
|
|
8
8
|
* @see {@link https://www.discogs.com/settings/developers}
|
|
9
9
|
*/
|
|
10
10
|
token?: string;
|
|
11
|
+
/** Not with `token`: the public wantlist page is read without one */
|
|
11
12
|
onlyForSale?: never;
|
|
12
13
|
} | {
|
|
14
|
+
/** Not with `onlyForSale`: the token only serves the API */
|
|
13
15
|
token?: never;
|
|
14
16
|
/**
|
|
15
17
|
* If true, only the releases currently for sale are returned, read from the public wantlist page with a browser.
|
|
@@ -22,6 +24,8 @@ export type WantlistOptions = {
|
|
|
22
24
|
* By default, it reads the public Discogs API (limited to 25 requests per minute without token, 60 with).
|
|
23
25
|
* @param username - Username whose wantlist is read.
|
|
24
26
|
* @param options - Options.
|
|
27
|
+
* @param options.token - Discogs personal access token, for the API.
|
|
28
|
+
* @param options.onlyForSale - Whether to keep only the releases for sale, read from the public wantlist page.
|
|
25
29
|
* @param browserContext - Optional Patchright browser context, only used with `onlyForSale`. If provided, you must manage its lifecycle.
|
|
26
30
|
* @returns A promise that resolves to the release IDs.
|
|
27
31
|
*/
|
package/dist/wantlist.js
CHANGED
|
@@ -38,6 +38,7 @@ function fetchWantlist(username, token) {
|
|
|
38
38
|
...(token ? { Authorization: `Discogs token=${token}` } : {}),
|
|
39
39
|
},
|
|
40
40
|
});
|
|
41
|
+
// eslint-disable-next-line typescript/no-unsafe-type-assertion -- The Discogs API answer is trusted as documented, a library keeping no schema dependency
|
|
41
42
|
const json = (await response.json().catch(() => null));
|
|
42
43
|
if (!response.ok) {
|
|
43
44
|
throw new Error(json?.message ?? `An error ${response.status} occurred.`);
|
|
@@ -79,7 +80,7 @@ function scrapeWantlist(username, browserContext) {
|
|
|
79
80
|
: `An error ${status} occurred.`);
|
|
80
81
|
}
|
|
81
82
|
const { total, ids } = await browserPage.evaluate(() => ({
|
|
82
|
-
total: +(document.querySelector('.pagination_total')?.textContent.split(' of ').pop()?.
|
|
83
|
+
total: +(document.querySelector('.pagination_total')?.textContent.split(' of ').pop()?.replaceAll(',', '') ?? '0'),
|
|
83
84
|
// The releases for sale are the ones with a link to their listings
|
|
84
85
|
ids: [...document.querySelectorAll('.marketplace_for_sale_count a')].map(x => +(new URL(x.href).pathname.split('/').pop() ?? '0')),
|
|
85
86
|
}));
|
|
@@ -95,6 +96,8 @@ function scrapeWantlist(username, browserContext) {
|
|
|
95
96
|
* By default, it reads the public Discogs API (limited to 25 requests per minute without token, 60 with).
|
|
96
97
|
* @param username - Username whose wantlist is read.
|
|
97
98
|
* @param options - Options.
|
|
99
|
+
* @param options.token - Discogs personal access token, for the API.
|
|
100
|
+
* @param options.onlyForSale - Whether to keep only the releases for sale, read from the public wantlist page.
|
|
98
101
|
* @param browserContext - Optional Patchright browser context, only used with `onlyForSale`. If provided, you must manage its lifecycle.
|
|
99
102
|
* @returns A promise that resolves to the release IDs.
|
|
100
103
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "discogs-marketplace-api-nodejs",
|
|
3
|
-
"version": "1.18.
|
|
3
|
+
"version": "1.18.3",
|
|
4
4
|
"description": "Another (better ?) NodeJs library to fetch data from Discogs marketplace",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"discogs",
|
|
@@ -21,10 +21,11 @@
|
|
|
21
21
|
"build": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json",
|
|
22
22
|
"start:dev": "node --import tsx --watch ./src/index.ts",
|
|
23
23
|
"test": "node --import tsx --test ./test/*.test.ts",
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
24
|
+
"check-types": "tsc --noEmit",
|
|
25
|
+
"oxlint": "oxlint",
|
|
26
|
+
"oxlint:fix": "oxlint --fix",
|
|
27
|
+
"oxfmt": "oxfmt --check",
|
|
28
|
+
"oxfmt:fix": "oxfmt",
|
|
28
29
|
"cspell": "npx cspell ./ --no-progress",
|
|
29
30
|
"knip": "knip"
|
|
30
31
|
},
|
|
@@ -43,25 +44,20 @@
|
|
|
43
44
|
"patchright": "^1.64.2"
|
|
44
45
|
},
|
|
45
46
|
"devDependencies": {
|
|
46
|
-
"@eslint/js": "^9.39.5",
|
|
47
47
|
"@types/node": "^26.6.6",
|
|
48
|
-
"@typescript-eslint/eslint-plugin": "^8.72.1",
|
|
49
|
-
"@typescript-eslint/parser": "^8.72.1",
|
|
50
48
|
"cspell": "^10.4.0",
|
|
51
|
-
"eslint": "^
|
|
52
|
-
"eslint-
|
|
53
|
-
"eslint-import-resolver-typescript": "^4.4.5",
|
|
54
|
-
"eslint-plugin-import": "^2.32.0",
|
|
55
|
-
"eslint-plugin-jsdoc": "^65.4.2",
|
|
56
|
-
"globals": "^17.13.0",
|
|
49
|
+
"eslint-plugin-jsdoc": "^65.5.4",
|
|
50
|
+
"eslint-plugin-regexp": "^3.3.3",
|
|
57
51
|
"husky": "^9.1.7",
|
|
58
52
|
"knip": "^6.43.0",
|
|
59
53
|
"lint-staged": "^17.6.0",
|
|
60
|
-
"
|
|
54
|
+
"oxfmt": "^0.79.0",
|
|
55
|
+
"oxlint": "^1.95.0",
|
|
56
|
+
"oxlint-plugin-eslint": "^1.95.0",
|
|
57
|
+
"oxlint-tsgolint": "^7.0.2008",
|
|
61
58
|
"rimraf": "^6.1.3",
|
|
62
59
|
"tsc-alias": "^1.9.9",
|
|
63
60
|
"tsx": "^4.23.15",
|
|
64
|
-
"typescript": "^6.0.3"
|
|
65
|
-
"typescript-eslint": "^8.72.1"
|
|
61
|
+
"typescript": "^6.0.3"
|
|
66
62
|
}
|
|
67
63
|
}
|