From c106c5f214f2854ac58769156ead47e169f6210d Mon Sep 17 00:00:00 2001 From: Geraldxm <115340172+Geraldxm@users.noreply.github.com> Date: Fri, 29 Nov 2024 22:39:38 +0800 Subject: [PATCH] feat: Feature/foodtalks (#17718) * 24/11/28 fix: remove uncessary files * 24/11/28 fix: return promise * 24/11/28 refactor: return promises * 24/11/28 refactor: remove await in the function * 24/11/29 fix: pass Promises to items? --- lib/routes/foodtalks/index.ts | 78 +++++++++++++++++++++++++++++++ lib/routes/foodtalks/namespace.ts | 9 ++++ 2 files changed, 87 insertions(+) create mode 100644 lib/routes/foodtalks/index.ts create mode 100644 lib/routes/foodtalks/namespace.ts diff --git a/lib/routes/foodtalks/index.ts b/lib/routes/foodtalks/index.ts new file mode 100644 index 00000000000000..4633cc1ac12b24 --- /dev/null +++ b/lib/routes/foodtalks/index.ts @@ -0,0 +1,78 @@ +import { Route } from '@/types'; +import ofetch from '@/utils/ofetch'; +import cache from '@/utils/cache'; +import { namespace } from './namespace'; +import logger from '@/utils/logger'; + +export const route: Route = { + path: '/', + categories: namespace.categories, + example: '/foodtalks', + radar: [ + { + source: ['www.foodtalks.cn'], + }, + ], + name: 'FoodTalks global food information network', + maintainers: ['Geraldxm'], + handler, + url: 'www.foodtalks.cn', +}; + +function processItems(list: any[], fullTextApi: string) { + return Promise.all( + list.map((item) => + cache.tryGet(item.link, async () => { + try { + const response = await ofetch(fullTextApi.replace('{id}', item.id), { + headers: { + referrer: 'https://www.foodtalks.cn/', + method: 'GET', + }, + }); + item.description = response.data.content; + return item; + } catch (error) { + // Log the error for debugging, but don't rethrow to avoid halting the entire batch. + // Consider adding a fallback description or other handling here. + logger.error(`Error fetching full text for ${item.link}:`, error); + return item; // Return the original item, even without the description + } + }) + ) + ); +} + +async function handler() { + const url = 'https://api-we.foodtalks.cn/news/news/page?current=1&size=15&isLatest=1&language=ZH'; + const response = await ofetch(url, { + headers: { + referrer: 'https://www.foodtalks.cn/', + method: 'GET', + }, + }); + const records = response.data.records; + + const list = records.map((item) => ({ + title: item.title, + pubDate: new Date(item.publishTime), + link: `https://www.foodtalks.cn/news/${item.id}`, + category: item.parentTagCode === 'category' ? item.tagCode : item.parentTagCode, + author: item.author === null ? item.sourceName : item.author, + id: item.id, + image: item.coverImg, + })); + + const fullTextApi = 'https://api-we.foodtalks.cn/news/news/{id}?language=ZH'; + + // Assign the result of processItems to the items variable + const items = await processItems(list, fullTextApi); + + return { + title: namespace.name, + description: namespace.description, + link: 'https://' + namespace.url, + item: items, // Use the processed items here + image: 'https://www.foodtalks.cn/static/img/news-site-logo.7aaa5463.svg', + }; +} diff --git a/lib/routes/foodtalks/namespace.ts b/lib/routes/foodtalks/namespace.ts new file mode 100644 index 00000000000000..1f98d28b8ae755 --- /dev/null +++ b/lib/routes/foodtalks/namespace.ts @@ -0,0 +1,9 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: 'FoodTalks全球食品资讯网', + url: 'www.foodtalks.cn', + categories: ['new-media'], + lang: 'zh-CN', + description: 'FoodTalks全球食品资讯网是一个提供食品饮料行业新闻、资讯、分析和商业资源的领先在线平台。它涵盖行业趋势、市场动态、产品创新、投融资信息以及企业新闻,连接行业内的专业人士、企业和消费者。', +};