import {IPage, ISearchItem, ISearchParams, ISearchProvider, Order, Sort, StationId} from "../type";
|
import {beginToPage, wrappedFetch} from "../lib";
|
import {HTMLElement, parse} from "node-html-parser";
|
|
export class Provider5Melonbooks implements ISearchProvider {
|
readonly stationId = StationId.Melonbooks;
|
|
async search(params: ISearchParams): Promise<IPage> {
|
const {keyword, begin = 1, maxNum = 100, minPrice, maxPrice, sort, order, category, shipping} = params;
|
|
const pageNum = beginToPage(begin, maxNum);
|
|
const url = new URL("https://www.melonbooks.co.jp/search/search.php?mode=search");
|
url.searchParams.set("mode", 'search');
|
url.searchParams.set("name", keyword);
|
url.searchParams.set("pageno", String(pageNum));
|
url.searchParams.set("disp_number", String(maxNum));
|
const warn: string[] = [];
|
if (category) {
|
warn.push(`unsupported category=${category}`)
|
}
|
if (minPrice) {
|
url.searchParams.set("price_low", String(minPrice));
|
}
|
if (maxPrice) {
|
url.searchParams.set("price_high", String(maxPrice));
|
}
|
if (shipping === 'free') {
|
warn.push(`unsupported shipping`);
|
}
|
if (sort) {
|
if (sort === Sort.Latest) {
|
if (order === Order.Asc) {
|
url.searchParams.set("orderby", 'publish_asc');
|
} else {
|
url.searchParams.set("orderby", 'publish_desc');
|
}
|
} else if (sort ===Sort.Price) {
|
if (order === Order.Desc) {
|
url.searchParams.set("orderby", 'price_desc');
|
} else {
|
url.searchParams.set("orderby", '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: any = await resp.text();
|
|
const result = parseProducts(html);
|
|
return {
|
srcUrl: url.toString(),
|
warning: warn.join(', '),
|
begin,
|
maxNum,
|
pageNum,
|
...params,
|
...result,
|
};
|
}
|
}
|
|
function parseProducts(html: string): Omit<IPage, keyof ISearchParams> {
|
const map = new Map<string, ISearchItem>();
|
const doc = parse(html);
|
doc.querySelectorAll('.search-page .item-list li[class^="product_"]')
|
.map(parseItem)
|
.forEach(item => item.itemId && map.set(item.itemId, item));
|
|
const totalText = doc.querySelector('.search-page .search-meta .search-val')?.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('.item-meta .item-price')?.textContent?.replace(/\D+/g, '');
|
return {
|
itemId: el.classNames?.replace(/\D+/g, ''),
|
url: el.querySelector('.item-image a')?.getAttribute('href'),
|
image: {
|
imageUrl: el.querySelector('.item-image .item-thumbnail img')?.getAttribute('src'),
|
alt: el.querySelector('.item-image .item-thumbnail img')?.getAttribute('alt'),
|
},
|
name: el.querySelector('.item-meta .product_title')?.textContent?.trim(),
|
price: priceText ? parseInt(priceText) : undefined,
|
};
|
}
|