From 295a4f845e2830c77344935afa2cbd8455b4c17c Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Mon, 1 Dec 2025 21:54:05 +0800 Subject: [PATCH] feat(route): add Produce Report (#20596) --- lib/routes/producereport/index.ts | 184 ++++++++++++++++++ lib/routes/producereport/namespace.ts | 9 + .../producereport/templates/description.art | 21 ++ 3 files changed, 214 insertions(+) create mode 100644 lib/routes/producereport/index.ts create mode 100644 lib/routes/producereport/namespace.ts create mode 100644 lib/routes/producereport/templates/description.art diff --git a/lib/routes/producereport/index.ts b/lib/routes/producereport/index.ts new file mode 100644 index 000000000..ff6746d66 --- /dev/null +++ b/lib/routes/producereport/index.ts @@ -0,0 +1,184 @@ +import path from 'node:path'; + +import type { Cheerio, CheerioAPI } from 'cheerio'; +import { load } from 'cheerio'; +import type { Element } from 'domhandler'; +import type { Context } from 'hono'; + +import type { Data, DataItem, Route } from '@/types'; +import { ViewType } from '@/types'; +import cache from '@/utils/cache'; +import ofetch from '@/utils/ofetch'; +import { parseDate } from '@/utils/parse-date'; +import { art } from '@/utils/render'; + +export const handler = async (ctx: Context): Promise => { + const { category = 'produce/fresh-fruits/apples' } = ctx.req.param(); + const limit: number = Number.parseInt(ctx.req.query('limit') ?? '10', 10); + + const baseUrl: string = 'https://www.producereport.com'; + const targetUrl: string = new URL(category, baseUrl).href; + + const response = await ofetch(targetUrl); + const $: CheerioAPI = load(response); + const language = $('html').attr('lang') ?? 'en'; + + let items: DataItem[] = []; + + items = $('table.views-table tbody tr') + .slice(0, limit) + .toArray() + .map((el): Element => { + const $el: Cheerio = $(el); + const $aEl: Cheerio = $el.find('a').first(); + + const title: string = $aEl.text(); + const image: string | undefined = $el + .find('td.views-field-field-image a img') + .attr('src') + ?.replace(/styles\/thumbnail\/public/, '') + ?.split(/\?/)?.[0]; + + const description: string | undefined = art(path.join(__dirname, 'templates/description.art'), { + images: image + ? [ + { + src: image, + alt: title, + }, + ] + : undefined, + intro: $aEl.parent().contents().last().text().trim(), + }); + const pubDateStr: string | undefined = $el.find('td.views-field-created').contents().first().text()?.trim(); + const linkUrl: string | undefined = $aEl.attr('href'); + const upDatedStr: string | undefined = pubDateStr; + + const processedItem: DataItem = { + title, + description, + pubDate: pubDateStr ? parseDate(pubDateStr) : undefined, + link: linkUrl ? new URL(linkUrl, baseUrl).href : undefined, + content: { + html: description, + text: description, + }, + image, + banner: image, + updated: upDatedStr ? parseDate(upDatedStr) : undefined, + language, + }; + + return processedItem; + }); + + items = await Promise.all( + items.map((item) => { + if (!item.link) { + return item; + } + + return cache.tryGet(item.link, async (): Promise => { + const detailResponse = await ofetch(item.link); + const $$: CheerioAPI = load(detailResponse); + + const title: string = $$('meta[property="og:title"]').attr('content') ?? item.title; + const image: string | undefined = $$('meta[property="og:image"]').attr('content'); + + const description: string | undefined = art(path.join(__dirname, 'templates/description.art'), { + images: image + ? [ + { + src: image, + alt: title, + }, + ] + : undefined, + description: $$('div[property="content:encoded"]').html(), + }); + const pubDateStr: string | undefined = $$('div.pane-node-created').text()?.trim(); + const categoryEls: Element[] = $$('div.pane-node-field-topics a').toArray(); + const categories: string[] = [...new Set(categoryEls.map((el) => $$(el).text()).filter(Boolean))]; + const authorEls: Element[] = $$('div.pane-node-author a.username').toArray(); + const authors: DataItem['author'] = authorEls.map((authorEl) => { + const $$authorEl: Cheerio = $$(authorEl); + + return { + name: $$authorEl.text(), + url: $$authorEl.attr('href') ? new URL($$authorEl.attr('href') as string, baseUrl).href : undefined, + avatar: undefined, + }; + }); + const upDatedStr: string | undefined = pubDateStr; + + const processedItem: DataItem = { + title, + description, + pubDate: pubDateStr ? parseDate(pubDateStr) : item.pubDate, + category: categories, + author: authors, + content: { + html: description, + text: description, + }, + image, + banner: image, + updated: upDatedStr ? parseDate(upDatedStr) : item.updated, + language, + }; + + return { + ...item, + ...processedItem, + }; + }); + }) + ); + + return { + title: $('title').text(), + description: $('meta[property="og:title"]').attr('content'), + link: targetUrl, + item: items, + allowEmpty: true, + image: $('meta[property="og:image"]').attr('content'), + author: $('meta[property="og:site_name"]').attr('content'), + language, + id: $('meta[property="og:url"]').attr('content'), + }; +}; + +export const route: Route = { + path: '/:category{.+}?', + name: 'Category', + url: 'www.producereport.com', + maintainers: ['nczitzk'], + handler, + example: '/producereport/produce/fresh-fruits/apples', + parameters: { + category: { + description: 'Category, `Fresh Fruits - Apple` by default', + }, + }, + description: `:::tip +To subscribe to [Apples](https://www.producereport.com/produce/fresh-fruits/apples), where the source URL is \`https://www.producereport.com/produce/fresh-fruits/apples\`, extract the certain parts from this URL to be used as parameters, resulting in the route as [\`/producereport/produce/fresh-fruits/apples\`](https://rsshub.app/producereport/produce/fresh-fruits/apples). +::: +`, + categories: ['new-media'], + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportRadar: true, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['www.producereport.com/:category'], + target: '/:category', + }, + ], + view: ViewType.Articles, +}; diff --git a/lib/routes/producereport/namespace.ts b/lib/routes/producereport/namespace.ts new file mode 100644 index 000000000..fa9a1af66 --- /dev/null +++ b/lib/routes/producereport/namespace.ts @@ -0,0 +1,9 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: 'Produce Report', + url: 'producereport.com', + categories: ['new-media'], + description: '', + lang: 'en', +}; diff --git a/lib/routes/producereport/templates/description.art b/lib/routes/producereport/templates/description.art new file mode 100644 index 000000000..249654e7e --- /dev/null +++ b/lib/routes/producereport/templates/description.art @@ -0,0 +1,21 @@ +{{ if images }} + {{ each images image }} + {{ if image?.src }} +
+ {{ image.alt }} +
+ {{ /if }} + {{ /each }} +{{ /if }} + +{{ if intro }} +
{{ intro }}
+{{ /if }} + +{{ if description }} + {{@ description }} +{{ /if }} \ No newline at end of file