From dea94ae075250e5ad6b06fb959042a17f284367e Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Mon, 11 Sep 2023 07:09:12 +0800 Subject: [PATCH] feat(route): add DN.com (#13246) --- lib/v2/dn/maintainer.js | 3 + lib/v2/dn/news.js | 90 +++++++++++++++++++++++++++++ lib/v2/dn/radar.js | 55 ++++++++++++++++++ lib/v2/dn/router.js | 3 + lib/v2/dn/templates/description.art | 15 +++++ website/docs/routes/new-media.md | 23 ++++++++ 6 files changed, 189 insertions(+) create mode 100644 lib/v2/dn/maintainer.js create mode 100644 lib/v2/dn/news.js create mode 100644 lib/v2/dn/radar.js create mode 100644 lib/v2/dn/router.js create mode 100644 lib/v2/dn/templates/description.art diff --git a/lib/v2/dn/maintainer.js b/lib/v2/dn/maintainer.js new file mode 100644 index 000000000..ecd2af599 --- /dev/null +++ b/lib/v2/dn/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/:language/news/:category?': ['nczitzk'], +}; diff --git a/lib/v2/dn/news.js b/lib/v2/dn/news.js new file mode 100644 index 000000000..340881a27 --- /dev/null +++ b/lib/v2/dn/news.js @@ -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, + }; +}; diff --git a/lib/v2/dn/radar.js b/lib/v2/dn/radar.js new file mode 100644 index 000000000..cac836ad6 --- /dev/null +++ b/lib/v2/dn/radar.js @@ -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', + }, + ], + }, +}; diff --git a/lib/v2/dn/router.js b/lib/v2/dn/router.js new file mode 100644 index 000000000..def0a42c0 --- /dev/null +++ b/lib/v2/dn/router.js @@ -0,0 +1,3 @@ +module.exports = (router) => { + router.get('/:language/news/:category?', require('./news')); +}; diff --git a/lib/v2/dn/templates/description.art b/lib/v2/dn/templates/description.art new file mode 100644 index 000000000..d8a7f322b --- /dev/null +++ b/lib/v2/dn/templates/description.art @@ -0,0 +1,15 @@ +{{ if image }} +
+ {{ image.alt }} +
+{{ /if }} + +{{ if abstracts }} + + {{@ abstracts }} + +{{ /if }} + +{{ if description }} + {{@ description }} +{{ /if }} \ No newline at end of file diff --git a/website/docs/routes/new-media.md b/website/docs/routes/new-media.md index 25fc7eed4..9f98e10d8 100644 --- a/website/docs/routes/new-media.md +++ b/website/docs/routes/new-media.md @@ -429,6 +429,29 @@ Category of news +## DN.com {#dn.com} + +### News {#dn.com-news} + + + +#### Language + +| English | 中文 | +| ------- | ----- | +| en-us | zh-cn | + +#### Category + +| English Category | 中文分类 | Category id | +| -------------------- | -------- | ----------- | +| The Latest | 最新 | | +| Industry Information | 行业资讯 | category-1 | +| Knowledge | 域名知识 | category-2 | +| Investment | 域名投资 | category-3 | + + + ## DoNews {#donews} ### 栏目 {#donews-lan-mu}