feat: route of smzdm product (#16516)

* feat: route of smzdm product

* fix: type

* fix: type

* Update lib/routes/smzdm/product.ts

Co-authored-by: Tony <TonyRL@users.noreply.github.com>

* refactor: replace .each() with .map()

* feat: cache
This commit is contained in:
chesha1 2024-08-23 23:57:15 +08:00 committed by GitHub
parent edf1a8f282
commit 9e033f05f7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 86 additions and 0 deletions

View File

@ -0,0 +1,86 @@
import { Route, DataItem } from '@/types';
import { ofetch } from 'ofetch';
import { load } from 'cheerio';
import cache from '@/utils/cache';
import got from '@/utils/got';
export const route: Route = {
path: '/product/:id',
categories: ['shopping'],
example: '/smzdm/product/zm5vzpe',
parameters: { id: '商品 id网址上直接可以看到' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['wiki.smzdm.com/p/:id'],
target: '/product/:id',
},
],
name: '商品',
maintainers: ['chesha1'],
handler,
};
async function handler(ctx) {
const link = `https://wiki.smzdm.com/p/${ctx.req.param('id')}`;
const response = await ofetch(link);
const $ = load(response);
const title = $('title').text();
// get simple info from list
const items: DataItem[] = $('ul#feed-main-list li')
.map(function () {
const altText = $(this).find('img').attr('alt');
const link = $(this).find('h5.feed-block-title a').attr('href');
const price = $(this).find('.z-highlight').text();
const title = altText + ' ' + price;
const description = $(this).find('.feed-block-descripe').text().replaceAll(/\s+/g, '');
return {
title,
link,
description,
};
})
.toArray();
// get detail info from each item
const out = await Promise.all(
items.map((item) =>
cache.tryGet(item.link, async () => {
const { data: response } = await got(item.link);
const $ = load(response);
// filter outdated articles
if ($('span.old').length > 0) {
return null;
} else {
const pubDate = $('meta[name="weibo:webpage:create_at"]').attr('content');
item.pubDate = pubDate;
if (item.description === '阅读全文') {
item.description = $('p[itemprop="description"]').first().html() as string;
}
return item;
}
})
)
);
const filteredOut = out.filter((result) => result !== null);
return {
title,
link,
item: filteredOut,
};
}