-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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全球食品资讯网是一个提供食品饮料行业新闻、资讯、分析和商业资源的领先在线平台。它涵盖行业趋势、市场动态、产品创新、投融资信息以及企业新闻,连接行业内的专业人士、企业和消费者。', | ||
}; |