|
| 1 | +import { Route } from '@/types'; |
| 2 | +import cache from '@/utils/cache'; |
| 3 | +import { fetchSearchItems, fetchItemDetail, formatItemDetail, MercariSort, MercariOrder, MercariStatus } from './util'; |
| 4 | + |
| 5 | +export const route: Route = { |
| 6 | + path: '/search/:query', |
| 7 | + categories: ['shopping'], |
| 8 | + example: '/mercari/search/keyword=シャツ&7bd3eacc-ae45-4d73-bc57-a611c9432014=340258ac-e220-4722-8c35-7f73b7382831', |
| 9 | + parameters: { |
| 10 | + query: 'Search parameters in URL query string format.', |
| 11 | + }, |
| 12 | + features: { |
| 13 | + requireConfig: false, |
| 14 | + requirePuppeteer: false, |
| 15 | + antiCrawler: false, |
| 16 | + supportBT: false, |
| 17 | + supportPodcast: false, |
| 18 | + supportScihub: false, |
| 19 | + }, |
| 20 | + name: 'Search', |
| 21 | + maintainers: ['yana9i, Tsuyumi25'], |
| 22 | + url: 'jp.mercari.com', |
| 23 | + handler, |
| 24 | +}; |
| 25 | + |
| 26 | +function parseSearchQuery(queryString: string) { |
| 27 | + const params = new URLSearchParams(queryString); |
| 28 | + |
| 29 | + const keyword = params.get('keyword') || ''; |
| 30 | + const sort = MercariSort[params.get('sort') as keyof typeof MercariSort] || MercariSort.default; |
| 31 | + const order = MercariOrder[params.get('order') as keyof typeof MercariOrder] || MercariOrder.desc; |
| 32 | + |
| 33 | + const statusMap: Record<string, keyof typeof MercariStatus> = { |
| 34 | + on_sale: 'onsale', |
| 35 | + 'sold_out|trading': 'soldout', |
| 36 | + }; |
| 37 | + const statusArray = |
| 38 | + params |
| 39 | + .get('status') |
| 40 | + ?.split(',') |
| 41 | + .map((s) => MercariStatus[statusMap[s]]) |
| 42 | + .filter(Boolean) || []; |
| 43 | + |
| 44 | + const attributeIds = [ |
| 45 | + '7bd3eacc-ae45-4d73-bc57-a611c9432014', // 色 |
| 46 | + '47295d80-5839-4237-bbfc-deb44b4e7999', // 割引オプション |
| 47 | + 'f42ae390-04ff-46ea-808b-f5d97cb45db4', // サイズ |
| 48 | + 'd664efe3-ae5a-4824-b729-e789bf93aba9', // 出品形式 |
| 49 | + ]; |
| 50 | + |
| 51 | + const attributes: Array<{ id: string; values: string[] }> = []; |
| 52 | + for (const id of attributeIds) { |
| 53 | + const values = params.get(id); |
| 54 | + if (values) { |
| 55 | + attributes.push({ id, values: values.split(',') }); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + const options = { |
| 60 | + categoryId: params.get('category_id')?.split(',').map(Number), |
| 61 | + brandId: params.get('brand_id')?.split(',').map(Number), |
| 62 | + priceMin: params.get('price_min') ? Number(params.get('price_min')) : undefined, |
| 63 | + priceMax: params.get('price_max') ? Number(params.get('price_max')) : undefined, |
| 64 | + itemConditionId: params.get('item_condition_id')?.split(',').map(Number), |
| 65 | + excludeKeyword: params.get('exclude_keyword') || undefined, |
| 66 | + itemTypes: params |
| 67 | + .get('item_types') |
| 68 | + ?.split(',') |
| 69 | + .map((type) => (type === 'mercari' ? 'ITEM_TYPE_MERCARI' : type === 'beyond' ? 'ITEM_TYPE_BEYOND' : type)), |
| 70 | + attributes, |
| 71 | + }; |
| 72 | + |
| 73 | + return { keyword, sort, order, status: statusArray, options }; |
| 74 | +} |
| 75 | + |
| 76 | +async function handler(ctx) { |
| 77 | + const queryString = ctx.req.param('query'); |
| 78 | + |
| 79 | + const { keyword, sort, order, status, options } = parseSearchQuery(queryString); |
| 80 | + const searchItems = (await fetchSearchItems(sort, order, status, keyword, options)).items; |
| 81 | + |
| 82 | + const items = await Promise.all(searchItems.map((item) => cache.tryGet(`mercari:${item.id}`, async () => await fetchItemDetail(item.id, item.itemType).then((detail) => formatItemDetail(detail))))); |
| 83 | + |
| 84 | + return { |
| 85 | + title: `${keyword} の検索結果`, |
| 86 | + link: `https://jp.mercari.com/search?${queryString}`, |
| 87 | + description: `Mercari advanced search results for: ${queryString}`, |
| 88 | + item: items, |
| 89 | + }; |
| 90 | +} |
0 commit comments