import {IPage, ISearchItem, ISearchParams, ISearchProvider, Order, Sort, StationId} from "../type";
|
import {HTMLElement, parse} from "node-html-parser";
|
import {beginToPage, wrappedFetch} from "../lib";
|
|
export class Provider7Booth implements ISearchProvider {
|
readonly stationId = StationId.Booth;
|
|
async search(params: ISearchParams): Promise<IPage> {
|
const maxNum = 60;
|
|
const {keyword, begin = 1, maxNum: mn, category, minPrice, maxPrice, shipping, sort, order} = params;
|
|
const pageNum = beginToPage(begin, maxNum);
|
|
const url = new URL(category ? `https://booth.pm/zh-cn/browse/${category}` : `https://booth.pm/zh-cn/search/${keyword}`);
|
if (category) {
|
url.searchParams.set("q", keyword);
|
}
|
url.searchParams.set("page", pageNum);
|
const warn: string[] = [];
|
if (mn !== undefined) {
|
warn.push('unsupported maxNum');
|
}
|
if (minPrice) {
|
url.searchParams.set("min_price", String(minPrice));
|
}
|
if (maxPrice) {
|
url.searchParams.set("max_price", String(maxPrice));
|
}
|
if (shipping === 'free') {
|
warn.push('unsupported shipping');
|
}
|
if (sort) {
|
if (sort === Sort.Latest) {
|
url.searchParams.set("sort", 'new');
|
} else if (sort ===Sort.Price) {
|
if (order === Order.Desc) {
|
url.searchParams.set("sort", 'price_desc');
|
} else {
|
url.searchParams.set("sort", 'price_asc');
|
}
|
} else if(sort!==Sort.Default) {
|
warn.push(`unsupported sort=${sort}`);
|
}
|
}
|
|
|
const resp = await wrappedFetch(url);
|
|
if (!resp.ok) {
|
if (resp.status === 404) {
|
return {...params, dataList: [], error: '404 Not Found'};
|
}
|
return {...params, dataList: [], error: `HTTP error! status: ${resp.status}`};
|
}
|
|
// noinspection TypeScriptUnresolvedReference
|
const html = await resp.text();
|
const result = parseProducts(html);
|
|
return {
|
srcUrl: url.toString(),
|
warning: warn.join(', '),
|
begin,
|
maxNum,
|
pageNum,
|
...params,
|
...result,
|
};
|
}
|
}
|
|
|
function parseProducts(docHtml: string): Omit<IPage, keyof ISearchParams> {
|
const map = new Map<string, ISearchItem>();
|
const doc = parse(docHtml);
|
doc.querySelectorAll('.market-items li.item-card')
|
.map(parseItem)
|
.forEach(item => item.itemId && map.set(item.itemId, item));
|
|
const totalText = doc.querySelector('.items-center>b')?.textContent;
|
const total = totalText ? parseInt(totalText.replace(/\D+/g, '')) : 0;
|
|
return {
|
total: Math.max(total || 0, map.size),
|
dataList: Array.from(map.values())
|
};
|
}
|
|
|
function parseItem(el: HTMLElement): ISearchItem {
|
const priceText = el.querySelector('.items-center .price')?.textContent?.replace(/\D+/g, '');
|
const link = el.querySelector('.item-card__thumbnail-images a')
|
const url = link?.getAttribute('href');
|
|
return {
|
itemId: el.getAttribute('data-product-id') || url?.replace(/\D+/g, ''),
|
url,
|
image: {
|
imageUrl: link?.getAttribute('data-original')
|
},
|
name: el.querySelector('.item-card__title')?.textContent?.trim(),
|
price: priceText ? parseInt(priceText) : undefined,
|
};
|
}
|