Skip to content

Commit

Permalink
24/11/26
Browse files Browse the repository at this point in the history
  • Loading branch information
Geraldxm committed Nov 26, 2024
1 parent 4ee77c7 commit 0d87a9e
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
105 changes: 105 additions & 0 deletions lib/routes/foodtalks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { Route } from '@/types';
import { load } from 'cheerio';
import puppeteer from '@/utils/puppeteer';
import { namespace } from './namespace';

export const route: Route = {
path: '/',
categories: ['new-media', 'journal'],
example: '/foodtalks',
radar: [
{
source: ['www.foodtalks.cn'],
},
],
name: 'FoodTalks global food information network',
maintainers: ['Geraldxm'],
handler,
url: 'www.foodtalks.cn',
};

const cleanString = (str) => str.replaceAll(/[\u200B-\u200D\uFEFF]/g, '');
// 解析日期字符串
const handleDate = (dateString) => {
// 标准化输入的日期字符串
dateString = dateString.trim().normalize('NFKC'); // 标准化为兼容分解形式
dateString = cleanString(dateString);

// 初始化当前时间,并调整到当地时区时间
const localTime = new Date();
let calculatedDate = new Date(localTime);

// 使用标准化后或通过正则匹配判断
if (dateString.includes('分钟前')) {
const minutesAgo = Number.parseInt(dateString);
calculatedDate.setTime(calculatedDate.getTime() - minutesAgo * 60000); // 减去相应的分钟数
} else if (dateString.includes('小时前')) {
const hoursAgo = Number.parseInt(dateString);
calculatedDate.setTime(calculatedDate.getTime() - hoursAgo * 3_600_000); // 减去相应的小时数
} else if (dateString.includes('昨天')) {
calculatedDate.setDate(calculatedDate.getDate() - 1);
calculatedDate.setHours(0, 0, 0, 0); // 重置时间为午夜
} else {
// 获取其他格式的日期,并将其转化为当地时间
calculatedDate = new Date(new Date(dateString).getTime() + 8 * 3_600_000); // 修正输入日期为GMT+8时区
}

return calculatedDate;
};

// handler should return a Data object
async function handler() {
const url = `https://${route.url}`;

// 导入 puppeteer 工具类并初始化浏览器实例
const browser = await puppeteer();
const page = await browser.newPage();
await page.goto(url);
await page.waitForSelector('.news');

const resule = await page.content();
const response = resule;

// const result = await page.evaluate(() => ({
// html: document.documentElement.outerHTML,
// }));
// const response = result.html;

page.close();

const $ = load(response);

const itemList = $('.news');

return {
title: namespace.name,
link: namespace.url,
description: namespace.description,
item: itemList?.toArray().map((item) => {
const element = $(item);
// title
const title = element.find('.title').text().trim();
// description
const description = element.find('.summary').text().trim();
// link
const partialLink = element.find('a');
const fullLink = `${url}${partialLink.attr('href')}`;
// image of the news
const image = element.find('.news-cover img').attr('src');
// author
const author = element.find('.info-bottom img').text().trim();
// date
const date = element.find('.time').text();
const pubDate = handleDate(date).toUTCString();

return {
title,
description,
pubDate,
link: fullLink,
image,
author,
};
}),
};
}
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', 'journal'],
lang: 'zh-CN',
description: 'FoodTalks全球食品资讯网是一个提供食品饮料行业新闻、资讯、分析和商业资源的领先在线平台。它涵盖行业趋势、市场动态、产品创新、投融资信息以及企业新闻,连接行业内的专业人士、企业和消费者。',
};

0 comments on commit 0d87a9e

Please sign in to comment.