feat(route): add DN.com (#13246)

This commit is contained in:
Ethan Shen
2023-09-11 07:09:12 +08:00
committed by GitHub
parent 59222eca18
commit dea94ae075
6 changed files with 189 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
module.exports = {
'/:language/news/:category?': ['nczitzk'],
};
+90
View File
@@ -0,0 +1,90 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const timezone = require('@/utils/timezone');
const { parseDate } = require('@/utils/parse-date');
const { art } = require('@/utils/render');
const path = require('path');
module.exports = async (ctx) => {
const { language, category = '' } = ctx.params;
const limit = ctx.query.limit ? parseInt(ctx.query.limit, 10) : 10;
const rootUrl = 'https://dn.com';
const currentUrl = new URL(`/${language}/news/${category}`, rootUrl).href;
const { data: response } = await got(currentUrl);
const $ = cheerio.load(response);
let items = $('a.list-item')
.slice(0, limit)
.toArray()
.map((item) => {
item = $(item);
const image = item.find('div.img img');
return {
title: item.find('h2.ellipse2').text(),
link: new URL(item.prop('href'), rootUrl).href,
description: art(path.join(__dirname, 'templates/description.art'), {
image: image
? {
src: image.prop('src'),
alt: image.prop('alt'),
}
: undefined,
abstracts: item.find('p.abstract').html(),
}),
category: item
.find('span.cat')
.toArray()
.map((c) => $(c).text()),
pubDate: timezone(parseDate(item.find('span.time').text()), +8),
};
});
items = await Promise.all(
items.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const { data: detailResponse } = await got(item.link);
const content = cheerio.load(detailResponse);
item.title = content('h1.tit').text();
item.description = art(path.join(__dirname, 'templates/description.art'), {
abstracts: content('div.abstract').html(),
description: content('div.detail').html(),
});
item.author = content('span.author')
.text()
.replace(/(By|作者)\s/, '');
item.category = [
...item.category,
...content('div.tags p a')
.toArray()
.map((c) => content(c).text()),
];
item.pubDate = timezone(parseDate(content('span.date').text()), +8);
return item;
})
)
);
const title = $('a.logo img').prop('alt');
const icon = $('link[rel="icon"]').prop('href');
ctx.state.data = {
item: items,
title: `${title} - ${$('div.group a.active').text()}`,
link: currentUrl,
description: $('meta[name="description"]').prop('content'),
language: $('html').prop('lang'),
image: new URL($('a.logo img').prop('src'), rootUrl).href,
icon,
logo: icon,
subtitle: $('title').text(),
author: title,
};
};
+55
View File
@@ -0,0 +1,55 @@
module.exports = {
'dn.com': {
_name: 'DN.com',
'.': [
{
title: 'The Latest',
docs: 'https://docs.rsshub.app/routes/new-media#dn.com-news',
source: ['/en-us/news/p1', '/en-us/news'],
target: '/dn/en-us/news',
},
{
title: 'Industry Information',
docs: 'https://docs.rsshub.app/routes/new-media#dn.com-news',
source: ['/en-us/news/category-1'],
target: '/dn/en-us/news/category-1',
},
{
title: 'Knowledge',
docs: 'https://docs.rsshub.app/routes/new-media#dn.com-news',
source: ['/en-us/news/category-2'],
target: '/dn/en-us/news/category-2',
},
{
title: 'Investment',
docs: 'https://docs.rsshub.app/routes/new-media#dn.com-news',
source: ['/en-us/news/category-3'],
target: '/dn/en-us/news/category-3',
},
{
title: '最新',
docs: 'https://docs.rsshub.app/routes/new-media#dn.com-news',
source: ['/zh-cn/news/p1', '/zh-cn/news'],
target: '/dn/zh-cn/news',
},
{
title: '行业资讯',
docs: 'https://docs.rsshub.app/routes/new-media#dn.com-news',
source: ['/zh-cn/news/category-1'],
target: '/dn/zh-cn/news/category-1',
},
{
title: '域名知识',
docs: 'https://docs.rsshub.app/routes/new-media#dn.com-news',
source: ['/zh-cn/news/category-2'],
target: '/dn/zh-cn/news/category-2',
},
{
title: '域名投资',
docs: 'https://docs.rsshub.app/routes/new-media#dn.com-news',
source: ['/zh-cn/news/category-3'],
target: '/dn/zh-cn/news/category-3',
},
],
},
};
+3
View File
@@ -0,0 +1,3 @@
module.exports = (router) => {
router.get('/:language/news/:category?', require('./news'));
};
+15
View File
@@ -0,0 +1,15 @@
{{ if image }}
<figure>
<img src="{{ image.src }}" alt="{{ image.alt }}">
</figure>
{{ /if }}
{{ if abstracts }}
<backquote>
{{@ abstracts }}
</backquote>
{{ /if }}
{{ if description }}
{{@ description }}
{{ /if }}
+23
View File
@@ -429,6 +429,29 @@ Category of news
<Route author="nczitzk" example="/deepmind/blog" path="/deepmind/blog" radar="1" rssbud="1"/>
## DN.com {#dn.com}
### News {#dn.com-news}
<Route author="nczitzk" example="/dn/en-us/news" path="/dn/:language/news/:category?" paramsDesc={['Language, see below', 'Category, see below, The Latest by default']} radar="1" rssbud="1">
#### Language
| English | 中文 |
| ------- | ----- |
| en-us | zh-cn |
#### Category
| English Category | 中文分类 | Category id |
| -------------------- | -------- | ----------- |
| The Latest | 最新 | |
| Industry Information | 行业资讯 | category-1 |
| Knowledge | 域名知识 | category-2 |
| Investment | 域名投资 | category-3 |
</Route>
## DoNews {#donews}
### 栏目 {#donews-lan-mu}