Skip to content

Commit

Permalink
feat: Feature/foodtalks (#17718)
Browse files Browse the repository at this point in the history
* 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?
  • Loading branch information
Geraldxm authored Nov 29, 2024
1 parent c85ecdc commit c106c5f
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
78 changes: 78 additions & 0 deletions lib/routes/foodtalks/index.ts
Original file line number Diff line number Diff line change
@@ -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',
};
}
9 changes: 9 additions & 0 deletions lib/routes/foodtalks/namespace.ts
Original file line number Diff line number Diff line change
@@ -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全球食品资讯网是一个提供食品饮料行业新闻、资讯、分析和商业资源的领先在线平台。它涵盖行业趋势、市场动态、产品创新、投融资信息以及企业新闻,连接行业内的专业人士、企业和消费者。',
};

0 comments on commit c106c5f

Please sign in to comment.