From 32b4c1a51da1650ca4ff44019fa9e96d22912f0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=AA=E6=9C=88?= <45122329+cokemine@users.noreply.github.com> Date: Wed, 2 Apr 2025 19:47:24 +0900 Subject: [PATCH] feat: add melonbooks (#18753) * feat: add melonbooks * refactor: extend description content * fix: fix cache path * fix: update * Update lib/routes/melonbooks/search.ts Co-authored-by: Tony * refactor: add whole item page into description --------- --- lib/routes/melonbooks/namespace.ts | 9 ++++++ lib/routes/melonbooks/parser.ts | 39 ++++++++++++++++++++++++++ lib/routes/melonbooks/search.ts | 44 ++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 lib/routes/melonbooks/namespace.ts create mode 100644 lib/routes/melonbooks/parser.ts create mode 100644 lib/routes/melonbooks/search.ts diff --git a/lib/routes/melonbooks/namespace.ts b/lib/routes/melonbooks/namespace.ts new file mode 100644 index 000000000..803396e94 --- /dev/null +++ b/lib/routes/melonbooks/namespace.ts @@ -0,0 +1,9 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: 'メロンブックス', + url: 'www.melonbooks.co.jp', + description: 'サイン本の同人誌、同人ゲーム、同人音楽、同人グッズの通販は国内最大級、業界最速の萌えいっぱいの総合書店メロンブックスで。同人作品、同人委託の特典付商品も多数あり。直営店舗数も同人業界で最大級。', + lang: 'ja', + categories: ['anime'], +}; diff --git a/lib/routes/melonbooks/parser.ts b/lib/routes/melonbooks/parser.ts new file mode 100644 index 000000000..9cad45a2f --- /dev/null +++ b/lib/routes/melonbooks/parser.ts @@ -0,0 +1,39 @@ +import { type DataItem } from '@/types'; +import { CheerioAPI, load } from 'cheerio'; +import cache from '@/utils/cache'; +import ofetch from '@/utils/ofetch'; + +export function parseItems($: CheerioAPI, baseUrl: string, fetchRestrictedContent: boolean): Promise { + const list = $('div.item-list ul li') + .toArray() + .map((el) => { + const $el = $(el); + const a = $el.find('a').first(); + const title = a.attr('title')!; + const link = fetchRestrictedContent ? `${a.attr('href')}&adult_view=1` : a.attr('href'); + // const author = $el.find('p.search-item-author-author a').text(); + const category = [$el.find('p.item-state').text()]; + const image = $el.find('div.item-image img').attr('data-src'); + return { + title, + link, + category, + image, + banner: image, + }; + }); + + return Promise.all( + list.map((item) => + cache.tryGet(`${baseUrl}${item.link}`, async () => { + const res = await ofetch(`${baseUrl}${item.link}`); + const $ = load(res); + const description = $.html('div.item-page'); + return { + ...item, + description, + }; + }) + ) + ) as unknown as Promise; +} diff --git a/lib/routes/melonbooks/search.ts b/lib/routes/melonbooks/search.ts new file mode 100644 index 000000000..993683588 --- /dev/null +++ b/lib/routes/melonbooks/search.ts @@ -0,0 +1,44 @@ +import { Data, Route } from '@/types'; +import ofetch from '@/utils/ofetch'; +import { load } from 'cheerio'; +import { parseItems } from './parser'; +import { Context } from 'hono'; +import querystring from 'querystring'; + +export const handler = async (ctx: Context): Promise => { + const baseUrl = 'https://www.melonbooks.co.jp'; + const query = ctx.req.param('query') ?? ''; + const url = `${baseUrl}/search/search.php?${query}`; + const fetchRestrictedContent = querystring.parse(query).adult_view === '1'; + + const res = await ofetch(url); + const $ = load(res); + const items = await parseItems($, baseUrl, fetchRestrictedContent); + + return { + title: '搜索结果', + link: url, + item: items, + }; +}; + +export const route: Route = { + path: '/search/:query?', + categories: ['anime'], + example: '/melonbooks/search/name=けいおん', + parameters: { category: '链接参数,对应网址问号后的内容,不携带问号' }, + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + name: '搜索结果', + maintainers: ['cokemine'], + description: `::: tip +如果你期望获取限制级内容,可以添加\`&adult_view=1\`参数 +:::`, + handler, +};