Skip to content

Commit

Permalink
feat(route): add 太平洋科技 (#16700)
Browse files Browse the repository at this point in the history
* feat(route): add 太平洋科技

* add article content

* use cache.tryGet()

* remove currentUrl cache

* promise return content string

* cache the whole returned object
  • Loading branch information
CH563 committed Sep 15, 2024
1 parent cbfd29d commit e5fc2b2
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 0 deletions.
143 changes: 143 additions & 0 deletions lib/routes/pconline/focus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';

const rootUrl = 'https://www.pconline.com.cn';
interface Item {
id: string;
title: string;
channelName: string;
wap_3g_url: string;
bigImage: string;
cover: string;
largeImage: string;
authorname: string;
authorImg: string;
pc_pubDate: string;
artType: string;
summary: string;
url: string;
}

const categories = {
all: {
title: '全部',
path: '',
},
tech: {
title: '科技',
path: 'tech/',
},
finance: {
title: '财经',
path: 'finance/',
},
life: {
title: '生活',
path: 'life/',
},
company: {
title: '公司',
path: 'company/',
},
character: {
title: '人物',
path: 'character/',
},
};

const getContent = (item) =>
cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: `https:${item.link}`,
responseType: 'arrayBuffer',
});

const utf8decoder = new TextDecoder('GBK');
const html = utf8decoder.decode(detailResponse.data);
const $ = load(html);
item.description = $('.context-box .context-table tbody td').html();
return item;
});

export const handler = async (ctx) => {
const { category = 'all' } = ctx.req.param();
const cate = categories[category] || categories.all;
const currentUrl = `${rootUrl}/3g/other/focus/${cate.path}index.html`;

const response = await got({
method: 'get',
url: currentUrl,
});

const resString = response.data
.replace(/Module\.callback\((.*)\)/s, '$1')
.split('\n')
.filter((e) => e.indexOf('"tags":') !== 0)
.join('\n')
.replaceAll("'", '"');
const tinyData = resString.replaceAll(/[\n\r]/g, '');
const dataString = tinyData.replaceAll(',}', '}');
const data = JSON.parse(dataString || '');
const { articleList } = data;
const list = articleList.map((item: Item) => ({
id: item.id,
title: item.title,
author: [
{
name: item.authorname,
avatar: item.authorImg,
},
],
pubDate: parseDate(item.pc_pubDate),
link: item.url,
description: item.summary,
category: item.channelName,
image: item.cover,
}));

const items = await Promise.all(list.map((item) => getContent(item)));

return {
title: `太平洋科技-${cate.title}`,
link: currentUrl,
item: items,
};
};

export const route: Route = {
path: '/focus/:category?',
categories: ['new-media'],
example: '/pconline/focus',
parameters: {
category: {
description: '科技新闻的类别,获取最新的一页,分别:all, tech, finance, life, company, character',
default: 'all',
},
},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['pconline.com.cn/focus/', 'pconline.com.cn/'],
target: '/focus',
},
],
name: '科技新闻',
maintainers: ['CH563'],
handler,
description: `::: tip
| 全部 | 科技 | 财经 | 生活 | 公司 | 人物 |
| --- | --- | --- | --- | --- | --- |
| all | tech | finance | life | company | character |
:::`,
};
10 changes: 10 additions & 0 deletions lib/routes/pconline/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: '太平洋科技',
url: 'pconline.com.cn',
description: `
:::tip
太平洋科技是专业IT门户网站,为用户和经销商提供IT资讯和行情报价,涉及电脑,手机,数码产品,软件等.
:::`,
};

0 comments on commit e5fc2b2

Please sign in to comment.