lefengyang
2026-08-20 d01a28cb030b2cb0ff1d42273c877a0f728519a6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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,
    };
}