discogs-marketplace-api-nodejs 1.10.1 → 1.12.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 +160 -133
- package/dist/data/{countries.data.d.ts → country.data.d.ts} +5 -2
- package/dist/data/{countries.data.js → country.data.js} +6 -6
- package/dist/data/{currencies.data.d.ts → currency.data.d.ts} +5 -2
- package/dist/data/{currencies.data.js → currency.data.js} +2 -2
- package/dist/get-search-params.d.ts +10 -0
- package/dist/get-search-params.js +122 -0
- package/dist/index.d.ts +13 -4
- package/dist/index.js +7 -2
- package/dist/interfaces/search-params.interface.d.ts +120 -0
- package/dist/interfaces/search-result.interface.d.ts +118 -0
- package/dist/scrape.d.ts +10 -0
- package/dist/scrape.js +126 -0
- package/dist/search.d.ts +11 -0
- package/dist/search.js +154 -0
- package/dist/types/condition.type.d.ts +1 -1
- package/dist/types/currency.type.d.ts +1 -1
- package/dist/types/format-description.type.d.ts +1 -1
- package/dist/types/format.type.d.ts +1 -1
- package/dist/types/from.type.d.ts +1 -1
- package/dist/types/genre.type.d.ts +1 -1
- package/dist/types/hours-range.type.d.ts +4 -0
- package/dist/types/lang.type.d.ts +1 -1
- package/dist/types/limit.type.d.ts +1 -1
- package/dist/types/search-type.type.d.ts +1 -1
- package/dist/types/sort.type.d.ts +4 -1
- package/dist/types/style.type.d.ts +1 -1
- package/package.json +19 -18
- package/dist/data/index.d.ts +0 -3
- package/dist/data/index.js +0 -10
- package/dist/interfaces/index.d.ts +0 -4
- package/dist/interfaces/input.interface.d.ts +0 -100
- package/dist/interfaces/output-error.interface.d.ts +0 -9
- package/dist/interfaces/output-success.interface.d.ts +0 -115
- package/dist/interfaces/output-success.interface.js +0 -2
- package/dist/marketplace.d.ts +0 -27
- package/dist/marketplace.js +0 -311
- package/dist/types/index.d.ts +0 -12
- package/dist/types/index.js +0 -2
- /package/dist/interfaces/{index.js → search-params.interface.js} +0 -0
- /package/dist/interfaces/{input.interface.js → search-result.interface.js} +0 -0
- /package/dist/{interfaces/output-error.interface.js → types/hours-range.type.js} +0 -0
package/dist/index.js
CHANGED
|
@@ -4,5 +4,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.DiscogsMarketplace = void 0;
|
|
7
|
-
const
|
|
8
|
-
|
|
7
|
+
const search_1 = __importDefault(require("./search.js"));
|
|
8
|
+
const get_search_params_1 = __importDefault(require("./get-search-params.js"));
|
|
9
|
+
/**
|
|
10
|
+
* Discogs Marketplace API, tools for interacting with the Discogs Marketplace.
|
|
11
|
+
* @see {@link https://github.com/KirianCaumes/Discogs-Marketplace-API-NodeJS GitHub Repository}
|
|
12
|
+
*/
|
|
13
|
+
exports.DiscogsMarketplace = { search: search_1.default, getSearchParamsFromUrl: get_search_params_1.default };
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import type { Condition } from '../types/condition.type.js';
|
|
2
|
+
import type { Currency } from '../types/currency.type.js';
|
|
3
|
+
import type { FormatDescription } from '../types/format-description.type.js';
|
|
4
|
+
import type { Format } from '../types/format.type.js';
|
|
5
|
+
import type { From } from '../types/from.type.js';
|
|
6
|
+
import type { Genre } from '../types/genre.type.js';
|
|
7
|
+
import type { HoursRange } from '../types/hours-range.type.js';
|
|
8
|
+
import type { Lang } from '../types/lang.type.js';
|
|
9
|
+
import type { Limit } from '../types/limit.type.js';
|
|
10
|
+
import type { Search } from '../types/search-type.type.js';
|
|
11
|
+
import type { Sort } from '../types/sort.type.js';
|
|
12
|
+
import type { Style } from '../types/style.type.js';
|
|
13
|
+
/**
|
|
14
|
+
* Parameters used to search listings on the Discogs Marketplace.
|
|
15
|
+
*/
|
|
16
|
+
export default interface SearchParams {
|
|
17
|
+
/**
|
|
18
|
+
* The type of element to search.
|
|
19
|
+
* | Value | Description |
|
|
20
|
+
* |-------------|--------------------------------|
|
|
21
|
+
* | `q` | Basic query search |
|
|
22
|
+
* | `master_id` | Search within a master release |
|
|
23
|
+
* | `release_id`| Search within a release |
|
|
24
|
+
* | `label_id` | Search within a label |
|
|
25
|
+
* | `artist_id` | Search by artist |
|
|
26
|
+
* | `user` | Search a user's wantlist |
|
|
27
|
+
* @default 'q'
|
|
28
|
+
*/
|
|
29
|
+
searchType: Search;
|
|
30
|
+
/**
|
|
31
|
+
* The search value corresponding to the selected `searchType`.
|
|
32
|
+
*/
|
|
33
|
+
searchValue?: string | number;
|
|
34
|
+
/**
|
|
35
|
+
* Currency code for price filtering.
|
|
36
|
+
* @example 'USD'
|
|
37
|
+
*/
|
|
38
|
+
currency?: Currency;
|
|
39
|
+
/**
|
|
40
|
+
* Genre filter.
|
|
41
|
+
* @example 'Rock'
|
|
42
|
+
*/
|
|
43
|
+
genre?: Genre;
|
|
44
|
+
/**
|
|
45
|
+
* List of styles.
|
|
46
|
+
* @example ['Death Metal', 'Heavy Metal']
|
|
47
|
+
*/
|
|
48
|
+
style?: Array<Style>;
|
|
49
|
+
/**
|
|
50
|
+
* List of formats.
|
|
51
|
+
* @example ['Vinyl', 'CD']
|
|
52
|
+
*/
|
|
53
|
+
format?: Array<Format>;
|
|
54
|
+
/**
|
|
55
|
+
* List of format descriptions.
|
|
56
|
+
* @example ['Limited Edition', 'Numbered']
|
|
57
|
+
*/
|
|
58
|
+
formatDescription?: Array<FormatDescription>;
|
|
59
|
+
/**
|
|
60
|
+
* List of media conditions.
|
|
61
|
+
* @example ['Mint (M)', 'Very Good Plus (VG+)']
|
|
62
|
+
*/
|
|
63
|
+
condition?: Array<Condition>;
|
|
64
|
+
/**
|
|
65
|
+
* Release year.
|
|
66
|
+
* Do not use together with `years`.
|
|
67
|
+
*/
|
|
68
|
+
year?: number;
|
|
69
|
+
/**
|
|
70
|
+
* Range of years.
|
|
71
|
+
* Do not use together with `year`.
|
|
72
|
+
*/
|
|
73
|
+
years?: {
|
|
74
|
+
/** Minimum year */
|
|
75
|
+
min: number;
|
|
76
|
+
/** Maximum year */
|
|
77
|
+
max: number;
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* If true, only listings that accept offers are returned.
|
|
81
|
+
* @default false
|
|
82
|
+
*/
|
|
83
|
+
isMakeAnOfferOnly?: boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Expedition country filter (2-letter country code).
|
|
86
|
+
* @example 'US'
|
|
87
|
+
*/
|
|
88
|
+
from?: From;
|
|
89
|
+
/**
|
|
90
|
+
* Seller's username filter.
|
|
91
|
+
*/
|
|
92
|
+
seller?: string;
|
|
93
|
+
/**
|
|
94
|
+
* Hours range for wantlist searches.
|
|
95
|
+
* @example '0-24'
|
|
96
|
+
*/
|
|
97
|
+
hoursRange?: HoursRange;
|
|
98
|
+
/**
|
|
99
|
+
* Sort order for the results.
|
|
100
|
+
* @default 'listed,desc'
|
|
101
|
+
*/
|
|
102
|
+
sort?: Sort;
|
|
103
|
+
/**
|
|
104
|
+
* Number of results to return per page.
|
|
105
|
+
* Allowed values: 25, 50, 100, 250.
|
|
106
|
+
* @default 25
|
|
107
|
+
*/
|
|
108
|
+
limit?: Limit;
|
|
109
|
+
/**
|
|
110
|
+
* Page number of results.
|
|
111
|
+
* Must be less than 401 to avoid Discogs 404 errors.
|
|
112
|
+
* @default 1
|
|
113
|
+
*/
|
|
114
|
+
page?: number;
|
|
115
|
+
/**
|
|
116
|
+
* Language code for Discogs interface.
|
|
117
|
+
* @default 'en'
|
|
118
|
+
*/
|
|
119
|
+
lang?: Lang;
|
|
120
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { CountryValues } from '../data/country.data.js';
|
|
2
|
+
import type { CurrencyValues } from '../data/currency.data.js';
|
|
3
|
+
/**
|
|
4
|
+
* Result returned from a Discogs Marketplace search.
|
|
5
|
+
*/
|
|
6
|
+
export default interface SearchResult {
|
|
7
|
+
/** List of items matching the search query. */
|
|
8
|
+
items: Array<{
|
|
9
|
+
/** Unique identifier for the listing */
|
|
10
|
+
id: number;
|
|
11
|
+
/** Title details */
|
|
12
|
+
title: {
|
|
13
|
+
/** Full original title */
|
|
14
|
+
original: string;
|
|
15
|
+
/** Artist name */
|
|
16
|
+
artist: string;
|
|
17
|
+
/** Item description */
|
|
18
|
+
item: string;
|
|
19
|
+
/** Array of format names */
|
|
20
|
+
formats: Array<string>;
|
|
21
|
+
};
|
|
22
|
+
/** URL to the listing on Discogs */
|
|
23
|
+
url: string;
|
|
24
|
+
/** Array of label names */
|
|
25
|
+
labels: Array<string>;
|
|
26
|
+
/** Array of catalog numbers */
|
|
27
|
+
catnos: Array<string>;
|
|
28
|
+
/** URL to the item's image */
|
|
29
|
+
imageUrl: string;
|
|
30
|
+
/** Text description of the item */
|
|
31
|
+
description: string;
|
|
32
|
+
/** Indicates if offers are accepted */
|
|
33
|
+
isAcceptingOffer: boolean;
|
|
34
|
+
/** Availability status of the item */
|
|
35
|
+
isAvailable: boolean;
|
|
36
|
+
/** Condition details */
|
|
37
|
+
condition: {
|
|
38
|
+
/** Media condition */
|
|
39
|
+
media: {
|
|
40
|
+
/** Full description */
|
|
41
|
+
full: string;
|
|
42
|
+
/** Short code */
|
|
43
|
+
short: string;
|
|
44
|
+
};
|
|
45
|
+
/** Sleeve condition */
|
|
46
|
+
sleeve: {
|
|
47
|
+
/** Full description */
|
|
48
|
+
full: string;
|
|
49
|
+
/** Short code */
|
|
50
|
+
short: string;
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
/** Seller information */
|
|
54
|
+
seller: {
|
|
55
|
+
/** Seller's username */
|
|
56
|
+
name: string;
|
|
57
|
+
/** Seller's profile URL */
|
|
58
|
+
url: string;
|
|
59
|
+
/** Seller rating/score */
|
|
60
|
+
score: string;
|
|
61
|
+
/** Number of notes/reviews */
|
|
62
|
+
notes: number;
|
|
63
|
+
};
|
|
64
|
+
/** Pricing details */
|
|
65
|
+
price: {
|
|
66
|
+
/**
|
|
67
|
+
* Base price as a string combining amount and currency.
|
|
68
|
+
* @example '12.34 USD'
|
|
69
|
+
*/
|
|
70
|
+
base: `${number} ${CurrencyValues}` | '';
|
|
71
|
+
/**
|
|
72
|
+
* Shipping cost as a string combining amount and currency.
|
|
73
|
+
* @example '5.67 USD'
|
|
74
|
+
*/
|
|
75
|
+
shipping: `${number} ${CurrencyValues}` | '';
|
|
76
|
+
};
|
|
77
|
+
/** Shipping origin country */
|
|
78
|
+
country: {
|
|
79
|
+
/** Country name */
|
|
80
|
+
name: string;
|
|
81
|
+
/**
|
|
82
|
+
* ISO country code
|
|
83
|
+
* @example 'US'
|
|
84
|
+
*/
|
|
85
|
+
code: CountryValues;
|
|
86
|
+
};
|
|
87
|
+
/** Community stats */
|
|
88
|
+
community: {
|
|
89
|
+
/** Number of users who have this item */
|
|
90
|
+
have: number;
|
|
91
|
+
/** Number of users who want this item */
|
|
92
|
+
want: number;
|
|
93
|
+
};
|
|
94
|
+
/** Release details */
|
|
95
|
+
release: {
|
|
96
|
+
/** Release ID */
|
|
97
|
+
id: number;
|
|
98
|
+
/** Release URL */
|
|
99
|
+
url: string;
|
|
100
|
+
};
|
|
101
|
+
}>;
|
|
102
|
+
/** Pagination information */
|
|
103
|
+
page: {
|
|
104
|
+
/** Current page number */
|
|
105
|
+
current: number;
|
|
106
|
+
/** Total number of pages */
|
|
107
|
+
total: number;
|
|
108
|
+
};
|
|
109
|
+
/** Overall search result metadata */
|
|
110
|
+
result: {
|
|
111
|
+
/** Total number of results found */
|
|
112
|
+
total: number;
|
|
113
|
+
/** Number of results per page */
|
|
114
|
+
perPage: number;
|
|
115
|
+
};
|
|
116
|
+
/** The generated URL used for the search */
|
|
117
|
+
urlGenerated: string;
|
|
118
|
+
}
|
package/dist/scrape.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type SearchResult from './interfaces/search-result.interface.js';
|
|
2
|
+
/**
|
|
3
|
+
* Parses the page and returns the items found.
|
|
4
|
+
* This function is called by the browser context when the page is loaded.
|
|
5
|
+
* @returns Items found and total
|
|
6
|
+
*/
|
|
7
|
+
export default function scrape(): Pick<SearchResult, 'items'> & {
|
|
8
|
+
/** Total items found */
|
|
9
|
+
total: SearchResult['result']['total'];
|
|
10
|
+
};
|
package/dist/scrape.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = scrape;
|
|
4
|
+
// These variables are injected by the browser context when the page is loaded.
|
|
5
|
+
const { Country, Currency } = globalThis;
|
|
6
|
+
/**
|
|
7
|
+
* Parses the page and returns the items found.
|
|
8
|
+
* This function is called by the browser context when the page is loaded.
|
|
9
|
+
* @returns Items found and total
|
|
10
|
+
*/
|
|
11
|
+
function scrape() {
|
|
12
|
+
/**
|
|
13
|
+
* Converts a currency string to a standardized format.
|
|
14
|
+
* @param value String to clean
|
|
15
|
+
* @returns Cleaned-up string
|
|
16
|
+
*/
|
|
17
|
+
const convertCurrency = (value) => {
|
|
18
|
+
if (!value) {
|
|
19
|
+
return '';
|
|
20
|
+
}
|
|
21
|
+
const currencyFound = Object.keys(Currency).find(key => value.includes(key));
|
|
22
|
+
if (!currencyFound) {
|
|
23
|
+
return '';
|
|
24
|
+
}
|
|
25
|
+
const currencyClean = Currency[currencyFound];
|
|
26
|
+
const amount = value
|
|
27
|
+
.replace(currencyClean !== 'JPY' ? /[.](?=.*[.])/g : /\./g, '') // Remove all dots but the last, except for JPY
|
|
28
|
+
.replace(currencyFound, '') // Remove original currency
|
|
29
|
+
.replace(/\s\s+/g, ''); // Remove spaces
|
|
30
|
+
const amountParsed = Number.parseFloat(amount);
|
|
31
|
+
if (Number.isNaN(amountParsed)) {
|
|
32
|
+
return '';
|
|
33
|
+
}
|
|
34
|
+
return `${amountParsed} ${currencyClean}`;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Safely parses a number from a string.
|
|
38
|
+
* @param value String to parse
|
|
39
|
+
* @returns Parsed number or 0 if invalid
|
|
40
|
+
*/
|
|
41
|
+
const safeParseInt = (value) => {
|
|
42
|
+
const parsed = Number.parseInt(value ?? '', 10);
|
|
43
|
+
return Number.isNaN(parsed) ? 0 : parsed;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Extracts and cleans text content from an element.
|
|
47
|
+
* @param selector CSS selector
|
|
48
|
+
* @param context Parent element to query from
|
|
49
|
+
* @returns Cleaned text content or an empty string
|
|
50
|
+
*/
|
|
51
|
+
const getTextContent = (selector, context = document) => context.querySelector(selector)?.textContent?.trim() ?? '';
|
|
52
|
+
const total = safeParseInt(getTextContent('.pagination_total')
|
|
53
|
+
.split(' ')
|
|
54
|
+
.filter(x => x)
|
|
55
|
+
.pop()
|
|
56
|
+
?.replace(/(,|\.|\s)/g, ''));
|
|
57
|
+
const items = [...document.querySelectorAll('table.table_block tbody tr')].map(el => {
|
|
58
|
+
const itemHref = el.querySelector('a.item_description_title')?.getAttribute('href') ?? '';
|
|
59
|
+
const mediaCondition = getTextContent('.item_condition span:nth-child(3)', el).split('\n')[0] ?? '';
|
|
60
|
+
const sleeveCondition = getTextContent('.item_condition span.item_sleeve_condition', el);
|
|
61
|
+
const countryName = getTextContent('.seller_info li:nth-child(3)', el).split(':').pop() ?? '';
|
|
62
|
+
const originalTitle = getTextContent('a.item_description_title', el);
|
|
63
|
+
const firstIndexOfDash = originalTitle.indexOf(' - ');
|
|
64
|
+
const lastIndexOfParenthesis = originalTitle.lastIndexOf(' (');
|
|
65
|
+
const releaseHref = el.querySelector('a.item_release_link')?.getAttribute('href') ?? '';
|
|
66
|
+
return {
|
|
67
|
+
id: safeParseInt(itemHref.split('/').pop()),
|
|
68
|
+
title: {
|
|
69
|
+
original: originalTitle,
|
|
70
|
+
artist: firstIndexOfDash > -1 ? originalTitle.substring(0, firstIndexOfDash) : '',
|
|
71
|
+
item: firstIndexOfDash > -1 && lastIndexOfParenthesis > -1
|
|
72
|
+
? originalTitle.substring(firstIndexOfDash + 3, lastIndexOfParenthesis)
|
|
73
|
+
: '',
|
|
74
|
+
formats: lastIndexOfParenthesis > -1
|
|
75
|
+
? originalTitle.substring(lastIndexOfParenthesis + 2, originalTitle.length - 1).split(', ')
|
|
76
|
+
: [],
|
|
77
|
+
},
|
|
78
|
+
url: `https://www.discogs.com${itemHref}`,
|
|
79
|
+
labels: [...el.querySelectorAll(".label_and_cat a[href^='https://www.discogs.com/']")]
|
|
80
|
+
.map(x => x.textContent?.trim() ?? '')
|
|
81
|
+
.filter((value, index, self) => self.indexOf(value) === index),
|
|
82
|
+
catnos: getTextContent('.label_and_cat .item_catno', el) // cspell:disable-line
|
|
83
|
+
.split(', ')
|
|
84
|
+
.filter(x => x !== 'none'),
|
|
85
|
+
imageUrl: el.querySelector('.marketplace_image')?.getAttribute('data-src') ?? '',
|
|
86
|
+
description: getTextContent('.item_condition + *', el),
|
|
87
|
+
isAcceptingOffer: getTextContent('.item_add_to_cart p a strong', el).split('/').length > 1,
|
|
88
|
+
isAvailable: !el.classList.contains('unavailable'),
|
|
89
|
+
condition: {
|
|
90
|
+
media: {
|
|
91
|
+
full: mediaCondition,
|
|
92
|
+
short: /\(([^)]+)\)/.exec(mediaCondition)?.[1] ?? '',
|
|
93
|
+
},
|
|
94
|
+
sleeve: {
|
|
95
|
+
full: sleeveCondition,
|
|
96
|
+
short: /\(([^)]+)\)/.exec(sleeveCondition)?.[1] ?? '',
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
seller: {
|
|
100
|
+
name: getTextContent('.seller_info a', el),
|
|
101
|
+
url: `https://www.discogs.com${el.querySelector('.seller_info a')?.getAttribute('href') ?? ''}`,
|
|
102
|
+
score: getTextContent('.seller_info li:nth-child(2) strong', el),
|
|
103
|
+
notes: safeParseInt(getTextContent('.seller_info li:nth-child(2) .section_link', el)),
|
|
104
|
+
},
|
|
105
|
+
price: {
|
|
106
|
+
base: convertCurrency(getTextContent('.price', el).replace(/,/, '.')),
|
|
107
|
+
shipping: convertCurrency(getTextContent('.item_shipping', el)
|
|
108
|
+
.replace(/(\s+|\+)/g, ' ')
|
|
109
|
+
.replace(',', '.')),
|
|
110
|
+
},
|
|
111
|
+
country: {
|
|
112
|
+
name: countryName,
|
|
113
|
+
code: Country[countryName],
|
|
114
|
+
},
|
|
115
|
+
community: {
|
|
116
|
+
have: safeParseInt(getTextContent('.community_summary .community_result:nth-child(1) .community_number', el)),
|
|
117
|
+
want: safeParseInt(getTextContent('.community_summary .community_result:nth-child(2) .community_number', el)),
|
|
118
|
+
},
|
|
119
|
+
release: {
|
|
120
|
+
id: safeParseInt(releaseHref.split('release/').pop()?.split('-').shift()),
|
|
121
|
+
url: `https://www.discogs.com${releaseHref}`,
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
});
|
|
125
|
+
return { total, items };
|
|
126
|
+
}
|
package/dist/search.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type Page } from 'playwright-chromium';
|
|
2
|
+
import type SearchParams from './interfaces/search-params.interface.js';
|
|
3
|
+
import type SearchResult from './interfaces/search-result.interface.js';
|
|
4
|
+
/**
|
|
5
|
+
* Performs a search on the Discogs marketplace using the provided parameters.
|
|
6
|
+
* @param pageInstance
|
|
7
|
+
* Optional Playwright page instance to reuse for multiple requests. This can be useful for reducing overhead when making numerous requests.
|
|
8
|
+
* If not provided, a new browser instance will be created and managed internally.
|
|
9
|
+
* @returns A promise that resolves to the search results, including items found, pagination details, and the generated URL.
|
|
10
|
+
*/
|
|
11
|
+
export default function search({ searchType, searchValue, currency, genre, style, format, formatDescription, condition, year, years, isMakeAnOfferOnly, from, seller, hoursRange, sort, limit, page, lang, }: SearchParams, pageInstance?: Page): Promise<SearchResult>;
|
package/dist/search.js
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
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 = search;
|
|
7
|
+
const user_agents_1 = __importDefault(require("user-agents"));
|
|
8
|
+
const playwright_chromium_1 = require("playwright-chromium");
|
|
9
|
+
const currency_data_1 = __importDefault(require("./data/currency.data.js"));
|
|
10
|
+
const country_data_1 = __importDefault(require("./data/country.data.js"));
|
|
11
|
+
const scrape_1 = __importDefault(require("./scrape.js"));
|
|
12
|
+
/**
|
|
13
|
+
* Performs a search on the Discogs marketplace using the provided parameters.
|
|
14
|
+
* @param pageInstance
|
|
15
|
+
* Optional Playwright page instance to reuse for multiple requests. This can be useful for reducing overhead when making numerous requests.
|
|
16
|
+
* If not provided, a new browser instance will be created and managed internally.
|
|
17
|
+
* @returns A promise that resolves to the search results, including items found, pagination details, and the generated URL.
|
|
18
|
+
*/
|
|
19
|
+
async function search({ searchType = 'q', searchValue, currency, genre, style, format, formatDescription, condition, year, years, isMakeAnOfferOnly = false, from, seller, hoursRange, sort = 'listed,desc', limit = 25, page = 1, lang = 'en', }, pageInstance) {
|
|
20
|
+
/** Url to be called by the browser */
|
|
21
|
+
const url = [
|
|
22
|
+
generateUrl({ searchType, seller, lang }),
|
|
23
|
+
serializeParams({
|
|
24
|
+
[searchType]: searchValue,
|
|
25
|
+
currency,
|
|
26
|
+
genre,
|
|
27
|
+
style: style?.length ? style : null,
|
|
28
|
+
format: format?.length ? format : null,
|
|
29
|
+
format_desc: formatDescription?.length ? formatDescription : null,
|
|
30
|
+
condition: condition?.length ? condition : null,
|
|
31
|
+
year: year && !years ? year : null,
|
|
32
|
+
year1: years?.min && !year ? years.min : null,
|
|
33
|
+
year2: years?.max && !year ? years.max : null,
|
|
34
|
+
offers: isMakeAnOfferOnly ? 1 : null,
|
|
35
|
+
ships_from: from,
|
|
36
|
+
hours_range: hoursRange,
|
|
37
|
+
limit,
|
|
38
|
+
page,
|
|
39
|
+
sort,
|
|
40
|
+
}),
|
|
41
|
+
].join('?');
|
|
42
|
+
// Get browser page by user input or generate a new one
|
|
43
|
+
const { browserPage, browser, browserContext } = pageInstance
|
|
44
|
+
? { browserPage: pageInstance }
|
|
45
|
+
: await (async () => {
|
|
46
|
+
/** Init browser */
|
|
47
|
+
const browser = await playwright_chromium_1.chromium.launch({
|
|
48
|
+
args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-gl-drawing-for-tests'],
|
|
49
|
+
});
|
|
50
|
+
/** Init context */
|
|
51
|
+
const browserContext = await browser.newContext({
|
|
52
|
+
userAgent: new user_agents_1.default({ platform: 'Win32' }).toString(),
|
|
53
|
+
extraHTTPHeaders: {
|
|
54
|
+
'X-PJAX': 'true',
|
|
55
|
+
},
|
|
56
|
+
javaScriptEnabled: false,
|
|
57
|
+
});
|
|
58
|
+
/** Init page */
|
|
59
|
+
const browserPage = await browserContext.newPage();
|
|
60
|
+
return {
|
|
61
|
+
browser,
|
|
62
|
+
browserContext,
|
|
63
|
+
browserPage,
|
|
64
|
+
};
|
|
65
|
+
})();
|
|
66
|
+
// Block all resources except document
|
|
67
|
+
await browserPage.route('**/*', route => [
|
|
68
|
+
'stylesheet',
|
|
69
|
+
'image',
|
|
70
|
+
'media',
|
|
71
|
+
'font',
|
|
72
|
+
'script',
|
|
73
|
+
'texttrack',
|
|
74
|
+
'xhr',
|
|
75
|
+
'fetch',
|
|
76
|
+
'eventsource',
|
|
77
|
+
'websocket',
|
|
78
|
+
'manifest',
|
|
79
|
+
'other',
|
|
80
|
+
].includes(route.request().resourceType())
|
|
81
|
+
? route.abort()
|
|
82
|
+
: route.continue());
|
|
83
|
+
// Inject some globals to the page, so we can use them in the scraping function
|
|
84
|
+
await browserPage.addInitScript(g => Object.assign(globalThis, g), { Country: country_data_1.default, Currency: currency_data_1.default });
|
|
85
|
+
/** Init page */
|
|
86
|
+
const response = await browserPage.goto(url, { waitUntil: 'domcontentloaded' });
|
|
87
|
+
/** Status code from the page */
|
|
88
|
+
const status = response?.status() ?? 500;
|
|
89
|
+
// See current issue: https://github.com/evanw/esbuild/issues/2605
|
|
90
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access
|
|
91
|
+
await browserPage.evaluate(() => (window.__name ??= (func) => func));
|
|
92
|
+
// If error status, reject
|
|
93
|
+
if (status >= 400) {
|
|
94
|
+
const errorMessage =
|
|
95
|
+
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
|
96
|
+
(await browserPage.evaluate(() => document.querySelector('h1 + p')?.innerHTML.trim())) || `An error ${status} occurred.`;
|
|
97
|
+
// Close browser
|
|
98
|
+
await browserContext?.close();
|
|
99
|
+
await browser?.close();
|
|
100
|
+
throw new Error(errorMessage);
|
|
101
|
+
}
|
|
102
|
+
/** Url generated by the browser */
|
|
103
|
+
const urlGenerated = browserPage.url();
|
|
104
|
+
// Scrape data from the page
|
|
105
|
+
const { items, total } = await browserPage.evaluate(scrape_1.default);
|
|
106
|
+
// Close browser
|
|
107
|
+
await browserContext?.close();
|
|
108
|
+
await browser?.close();
|
|
109
|
+
return {
|
|
110
|
+
items,
|
|
111
|
+
result: {
|
|
112
|
+
total,
|
|
113
|
+
perPage: limit,
|
|
114
|
+
},
|
|
115
|
+
page: {
|
|
116
|
+
current: page,
|
|
117
|
+
total: Math.ceil(total / limit),
|
|
118
|
+
},
|
|
119
|
+
urlGenerated,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Generate URL to be parsed
|
|
124
|
+
* @returns Url
|
|
125
|
+
*/
|
|
126
|
+
function generateUrl({ searchType, seller, lang, }) {
|
|
127
|
+
const baseUrl = `https://www.discogs.com/${lang}`;
|
|
128
|
+
if (seller) {
|
|
129
|
+
const path = searchType === 'user' ? 'mywants' : 'profile';
|
|
130
|
+
return `${baseUrl}/seller/${seller}/${path}`;
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
const path = searchType === 'user' ? 'mywants' : 'list';
|
|
134
|
+
return `${baseUrl}/sell/${path}`;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Serialize params URL
|
|
139
|
+
* @param params Object of GET parameters
|
|
140
|
+
* @returns Url
|
|
141
|
+
*/
|
|
142
|
+
function serializeParams(params) {
|
|
143
|
+
return Object.entries(params)
|
|
144
|
+
.flatMap(([key, value]) => {
|
|
145
|
+
if (value === undefined || value === null) {
|
|
146
|
+
return [];
|
|
147
|
+
}
|
|
148
|
+
if (Array.isArray(value)) {
|
|
149
|
+
return value.map(v => `${key}=${encodeURIComponent(v)}`);
|
|
150
|
+
}
|
|
151
|
+
return [`${key}=${encodeURIComponent(value)}`];
|
|
152
|
+
})
|
|
153
|
+
.join('&');
|
|
154
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Conditions
|
|
3
3
|
*/
|
|
4
|
-
export type
|
|
4
|
+
export type Condition = '0' | 'Fair (F)' | 'Good (G)' | 'Good Plus (G+)' | 'Mint (M)' | 'Near Mint (NM or M-)' | 'Poor (P)' | 'Very Good (VG)' | 'Very Good Plus (VG+)' | ({} & string);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Currencies
|
|
3
3
|
*/
|
|
4
|
-
export type
|
|
4
|
+
export type Currency = 'AUD' | 'BRL' | 'CAD' | 'CHF' | 'DKK' | 'EUR' | 'GBP' | 'JPY' | 'MXN' | 'NZD' | 'SEK' | 'USD' | 'ZAR' | ({} & string);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* FormatDescriptions
|
|
3
3
|
*/
|
|
4
|
-
export type
|
|
4
|
+
export type FormatDescription = '1 ⅞ ips' | '1"' | '10"' | '10.5" NAB Reel' | '11"' | '12"' | '15 ips' | '15/16 ips' | '16 ⅔ RPM' | '16"' | '16mm' | '2 Minute' | '2"' | '2-Track Mono' | '2-Track Stereo' | '21cm' | '25cm' | '27cm' | '29cm' | '3 Minute' | '3 ¾ ips' | '3"' | '3" Cine Reel' | '3.5"' | '30 ips' | '33 ⅓ RPM' | '35cm' | '35mm' | '3½"' | '4 Minute' | '4"' | '4-Track Mono' | '4-Track Stereo' | '45 RPM' | '4K' | '5"' | '5" Cine Reel' | '5.25"' | '50cm' | '5½"' | '6"' | '6" Cine Reel' | '6½"' | '7 ½ ips' | '7"' | '7" Cine Reel' | '78 RPM' | '8 ⅓ RPM' | '8"' | '80 RPM' | '8K' | '9"' | '90 RPM' | 'AAC' | 'AIFC' | 'AIFF' | 'ALAC' | 'AMR' | 'APE' | 'AVCD' | 'AVI' | 'Advance' | 'Album' | 'Ambisonic' | 'Bioplastic' | 'Blu-ray Audio' | 'Business Card' | 'CD+G' | 'CD-ROM' | 'CD-Record' | 'CDi' | 'Card Backed' | 'Club Edition' | 'Compilation' | 'Concert' | 'Copy Protected' | 'DFF' | 'DSF' | 'DVD-Audio' | 'DVD-Data' | 'DVD-Video' | 'DVDplus' | 'Deluxe Edition' | 'Disc Image' | 'Double Sided' | 'DualDisc' | 'EP' | 'Enhanced' | 'Etched' | 'FLAC' | 'FLV' | 'HDCD' | 'Hybrid' | 'Jukebox' | 'LP' | 'Limited Edition' | 'MOV' | 'MP2' | 'MP3' | 'MP3 Surround' | 'MPEG Video' | 'MPEG-4 Video' | 'Maxi-Single' | 'Mini' | 'Mini-Album' | 'Minimax' | 'Mispress' | 'Misprint' | 'Mixed' | 'Mixtape' | 'Mono' | 'Multichannel' | 'NTSC' | 'Numbered' | 'Opus' | 'PAL' | 'Partially Mixed' | 'Partially Unofficial' | 'Picture Disc' | 'Promo' | 'Quadraphonic' | 'RA' | 'RM' | 'Reissue' | 'Remastered' | 'Repress' | 'SECAM' | 'SHN' | 'SVCD' | 'SWF' | 'Salon' | 'Sampler' | 'Shape' | 'Single' | 'Single Sided' | 'Special Edition' | 'Stereo' | 'Styrene' | 'TTA' | 'Test Pressing' | 'Transcription' | 'Unofficial Release' | 'VCD' | 'VinylDisc' | 'WAV' | 'WMA' | 'WMV' | 'WavPack' | 'White Label' | 'XRCD' | 'ogg-vorbis' | '¼"' | '½"' | '⅛"' | ({} & string);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Formats
|
|
3
3
|
*/
|
|
4
|
-
export type
|
|
4
|
+
export type Format = '4-Track Cartridge' | '8-Track Cartridge' | 'Acetate' | 'All Media' | 'Betacam' | 'Betacam SP' | 'Betamax' | 'Blu-ray' | 'Blu-ray-R' | 'Box Set' | 'CD' | 'CDV' | 'CDr' | 'Cartrivision' | 'Cassette' | 'Cylinder' | 'DAT' | 'DC-International' | 'DCC' | 'DVD' | 'DVDr' | 'Edison Disc' | 'Elcaset' | 'File' | 'Film Reel' | 'Flexi-disc' | 'Floppy Disk' | 'HD DVD' | 'HD DVD-R' | 'HitClips' | 'Hybrid' | 'Laserdisc' | 'Lathe Cut' | 'MVD' | 'Memory Stick' | 'Microcassette' | 'Mighty Tiny' | 'MiniDV' | 'Minidisc' | 'NT Cassette' | 'Pathé Disc' | 'PlayTape' | 'Pocket Rocker' | 'RCA Tape Cartridge' | 'Reel-To-Reel' | 'Revere Magnetic Stereo Tape Ca' | 'SACD' | 'Sabamobil' | 'SelectaVision' | 'Shellac' | 'Sopic' | 'Super VHS' | 'TeD' | 'Tefifon' | 'U-matic' | 'UMD' | 'Ultra HD Blu-ray' | 'VHD' | 'VHS' | 'Video 2000' | 'Video8' | 'Vinyl' | 'Wire Recording' | ({} & string);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* From
|
|
3
3
|
*/
|
|
4
|
-
export type
|
|
4
|
+
export type From = 'Afghanistan' | 'Albania' | 'Algeria' | 'American Samoa' | 'Andorra' | 'Angola' | 'Anguilla' | 'Antarctica' | 'Antigua and Barbuda' | 'Argentina' | 'Armenia' | 'Aruba' | 'Australia' | 'Austria' | 'Azerbaidjan' | 'Bahamas' | 'Bahrain' | 'Bangladesh' | 'Barbados' | 'Belarus' | 'Belgium' | 'Belize' | 'Benin' | 'Bermuda' | 'Bhutan' | 'Bolivia' | 'Bosnia-Herzegovina' | 'Botswana' | 'Bouvet Island' | 'Brazil' | 'British Indian Ocean Territory' | 'Brunei Darussalam' | 'Bulgaria' | 'Burkina Faso' | 'Burundi' | 'Cambodia' | 'Cameroon' | 'Canada' | 'Cape Verde' | 'Cayman Islands' | 'Central African Republic' | 'Chad' | 'Chile' | 'China' | 'Christmas Island' | 'Cocos (Keeling) Islands' | 'Colombia' | 'Comoros' | 'Congo' | 'Cook Islands' | 'Costa Rica' | 'Croatia' | 'Cuba' | 'Cyprus' | 'Czech Republic' | 'Denmark' | 'Djibouti' | 'Dominica' | 'Dominican Republic' | 'East Timor' | 'Ecuador' | 'Egypt' | 'El Salvador' | 'Equatorial Guinea' | 'Eritrea' | 'Estonia' | 'Ethiopia' | 'Falkland Islands' | 'Faroe Islands' | 'Fiji' | 'Finland' | 'Former Czechoslovakia' | 'Former USSR' | 'France' | 'France (European Territory)' | 'French Guyana' | 'French Southern Territories' | 'Gabon' | 'Gambia' | 'Georgia' | 'Germany' | 'Ghana' | 'Gibraltar' | 'Greece' | 'Greenland' | 'Grenada' | 'Guadeloupe (French)' | 'Guam (USA)' | 'Guatemala' | 'Guinea' | 'Guinea Bissau' | 'Guyana' | 'Haiti' | 'Heard and McDonald Islands' | 'Honduras' | 'Hong Kong' | 'Hungary' | 'Iceland' | 'India' | 'Indonesia' | 'Iran' | 'Iraq' | 'Ireland' | 'Israel' | 'Italy' | "Ivory Coast (Cote D'Ivoire)" | 'Jamaica' | 'Japan' | 'Jordan' | 'Kazakhstan' | 'Kenya' | 'Kiribati' | 'Kuwait' | 'Kyrgyzstan' | 'Laos' | 'Latvia' | 'Lebanon' | 'Lesotho' | 'Liberia' | 'Libya' | 'Liechtenstein' | 'Lithuania' | 'Luxembourg' | 'Macau' | 'Macedonia' | 'Madagascar' | 'Malawi' | 'Malaysia' | 'Maldives' | 'Mali' | 'Malta' | 'Marshall Islands' | 'Martinique (French)' | 'Mauritania' | 'Mauritius' | 'Mayotte' | 'Mexico' | 'Micronesia' | 'Moldova, Republic of' | 'Monaco' | 'Mongolia' | 'Montenegro' | 'Montserrat' | 'Morocco' | 'Mozambique' | 'Myanmar' | 'Namibia' | 'Nauru' | 'Nepal' | 'Netherlands' | 'Netherlands Antilles' | 'Neutral Zone' | 'New Caledonia (French)' | 'New Zealand' | 'Nicaragua' | 'Niger' | 'Nigeria' | 'Niue' | 'Norfolk Island' | 'Northern Ireland' | 'Northern Mariana Islands' | 'Norway' | 'Oman' | 'Pakistan' | 'Palau' | 'Panama' | 'Papua New Guinea' | 'Paraguay' | 'Peru' | 'Philippines' | 'Pitcairn Island' | 'Poland' | 'Polynesia (French)' | 'Portugal' | 'Puerto Rico' | 'Qatar' | 'Reunion (French)' | 'Romania' | 'Russian Federation' | 'Rwanda' | 'S. Georgia & S. Sandwich Isls.' | 'Saint Helena' | 'Saint Kitts & Nevis Anguilla' | 'Saint Lucia' | 'Saint Pierre and Miquelon' | 'Saint Tome (Sao Tome) and Princi' | 'Saint Vincent & Grenadines' | 'Samoa' | 'San Marino' | 'Saudi Arabia' | 'Select your country' | 'Senegal' | 'Serbia' | 'Seychelles' | 'Sierra Leone' | 'Singapore' | 'Slovak Republic' | 'Slovenia' | 'Solomon Islands' | 'Somalia' | 'South Africa' | 'South Korea' | 'Spain' | 'Spain (Canary Islands)' | 'Sri Lanka' | 'Sudan' | 'Suriname' | 'Svalbard and Jan Mayen Islands' | 'Swaziland' | 'Sweden' | 'Switzerland' | 'Syria' | 'Tadjikistan' | 'Taiwan' | 'Tanzania' | 'Thailand' | 'Togo' | 'Tokelau' | 'Tonga' | 'Trinidad and Tobago' | 'Tunisia' | 'Turkey' | 'Turkmenistan' | 'Turks and Caicos Islands' | 'Tuvalu' | 'USA Minor Outlying Islands' | 'Uganda' | 'Ukraine' | 'United Arab Emirates' | 'United Kingdom' | 'United Kingdom (Channel Islands)' | 'United States' | 'Uruguay' | 'Uzbekistan' | 'Vanuatu' | 'Vatican City State' | 'Venezuela' | 'Vietnam' | 'Virgin Islands (British)' | 'Virgin Islands (USA)' | 'Wallis and Futuna Islands' | 'Western Sahara' | 'Yemen' | 'Yugoslavia' | 'Zaire' | 'Zambia' | 'Zimbabwe' | ({} & string);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Genres
|
|
3
3
|
*/
|
|
4
|
-
export type
|
|
4
|
+
export type Genre = 'Blues' | 'Brass & Military' | "Children's" | 'Classical' | 'Electronic' | 'Folk, World, & Country' | 'Funk / Soul' | 'Hip Hop' | 'Jazz' | 'Latin' | 'Non-Music' | 'Pop' | 'Reggae' | 'Rock' | 'Stage & Screen' | ({} & string);
|