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
import {
    IPage,
    ISearchItem,
    ISearchParams,
    ISearchProvider,
    IYahooFleamarketProductItem,
    Order,
    Sort,
    StationId
} from "../type";
import {beginToPage, queryNextData, wrappedFetch} from "../lib";
 
export class Provider10YahooFleamarket implements ISearchProvider {
    readonly stationId = StationId.YahooFleamarket;
 
    async search(params: ISearchParams): Promise<IPage> {
        const {keyword, begin = 1, maxNum = 50, category, minPrice, maxPrice, shipping, sort, order} = params;
        const pageNum = beginToPage(begin, maxNum);
        const warn: string[] = [];
        const url = new URL(`https://paypayfleamarket.yahoo.co.jp/search/${keyword}`);
        url.searchParams.set("page", String(pageNum));
        url.searchParams.set("n", String(maxNum));
        if (category) {
            url.searchParams.set("categoryIds", category);
        }
        if (minPrice) {
            url.searchParams.set("minPrice", String(minPrice));
        }
        if (maxPrice) {
            url.searchParams.set("maxPrice", String(maxPrice));
        }
        if (shipping === 'free') {
            warn.push('unsupported free shipping');
        }
        if (sort) {
            if (sort === Sort.Latest) {
                url.searchParams.set("sort", 'openTime');
                if (order === Order.Asc) {
                    url.searchParams.set("order", 'asc');
                } else {
                    url.searchParams.set("order", 'desc');
                }
            } else if (sort ===Sort.Price) {
                url.searchParams.set("sort", 'price');
                if (order === Order.Desc) {
                    url.searchParams.set("order", 'desc');
                } else {
                    url.searchParams.set("order", '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 nextProps = queryNextData(docHtml);
    const result = nextProps?.initialState?.searchState?.search?.result;
    const dataList = result?.items.map(parseItem) || [];
 
    return {
        total: Math.max(result?.totalResultsAvailable || 0, dataList.length),
        dataList
    };
}
 
 
function parseItem(item: IYahooFleamarketProductItem): ISearchItem {
    return {
        itemId: item.id,
        url: `https://paypayfleamarket.yahoo.co.jp/item/${item.id}`,
        image: {
            imageUrl: item.thumbnailImageUrl,
        },
        name: item.title,
        time: item.endTime,
        price: item.price,
    };
}