diff --git a/docs/traditional-media.md b/docs/traditional-media.md index e90c269ca..20d3449d6 100644 --- a/docs/traditional-media.md +++ b/docs/traditional-media.md @@ -2593,6 +2593,12 @@ category 对应的关键词有 +## 中国科技网 + +### 科技日报 + + + ## 中国日报 ### 英语点津 diff --git a/lib/v2/stdaily/digitalpaper.js b/lib/v2/stdaily/digitalpaper.js new file mode 100644 index 000000000..16a4bcc58 --- /dev/null +++ b/lib/v2/stdaily/digitalpaper.js @@ -0,0 +1,125 @@ +const path = require('path'); +const { art } = require('@/utils/render'); +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +const renderDescription = ({ subtitle, quotation, pics, article }) => + art(path.join(__dirname, 'templates/description.art'), { + subtitle, + quotation, + pics, + article, + }); + +const getPageLength = async (url) => { + const response = await got({ + method: 'get', + url, + }); + + const $ = cheerio.load(response.data); + const pageLength = $('.right1 .bmname').children().length; + return { pageLength, $ }; +}; + +const getArticleList = ($, paperUrl) => { + const pageName = $('.zi .zi-top .banci strong').text(); + const list = $('.zi-meat ul>li') + .map((_, item) => { + const link = $(item).find('a').attr('href'); + const title = $(item).find('a div').text(); + return { + link: `${paperUrl}/${link}`, + title: `[${pageName}] ${title}`, + // pubDate, + }; + }) + .get(); + + return list; +}; +const createGetPageArticleList = (paperUrl, page = 1) => { + const getPageArticleList = async () => { + const currentUrl = `${paperUrl}/node_${page + 1}.htm`; + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + return getArticleList($, paperUrl); + }; + + return getPageArticleList; +}; + +const getListArticles = async (list, cache) => { + const items = await Promise.all( + list.map((item) => + cache.tryGet(item.link, async () => { + const { data } = await got(item.link); + const $ = cheerio.load(data); + + const quotation = $('.right-meat .yinti').text(); + const subtitle = $('.right-meat .futi').text(); + const article = $('.right-meat .tuwen .article #ozoom').html(); + const pics = $('.right-meat .tuwen .picture') + .map((_, item) => { + const pic = {}; + $(item) + .find('tr') + .map((_, row) => { + const src = $(row).find('img').attr('src'); + if (src) { + pic.src = src; + } else { + pic.des = $(row).find('td').text(); + } + return null; + }) + .get(); + return pic; + }) + .get(); + + item.author = $('.right-meat .author').text(); + item.description = renderDescription({ subtitle, quotation, article, pics }); + + return item; + }) + ) + ); + return items; +}; + +module.exports = async (ctx) => { + const date = new Date(); + const dateStr = date.toLocaleDateString('zh-CN', { + year: 'numeric', + month: '2-digit', + day: '2-digit', + }); + const formatedDate = dateStr.replace('/', '-'); // 'yyyy-mm/dd' + + const rootUrl = 'http://digitalpaper.stdaily.com/http_www.kjrb.com/kjrb/html'; + const currentUrl = `${rootUrl}/${formatedDate}`; + + // get page + const restPageLists = []; + const allPageArticleLists = []; + const { pageLength, $ } = await getPageLength(`${currentUrl}/node_2.htm`); + allPageArticleLists.push(...getArticleList($, currentUrl)); + for (let i = 2; i <= pageLength; i++) { + restPageLists.push(createGetPageArticleList(currentUrl, i)); + } + // get pages' article list + allPageArticleLists.push(...(await Promise.all(restPageLists.map((getPageArticleList) => getPageArticleList()))).flat()); + // get all articles + const items = await getListArticles(allPageArticleLists, ctx.cache); + + ctx.state.data = { + title: `科技日报`, + link: 'http://digitalpaper.stdaily.com', + item: items, + }; +}; diff --git a/lib/v2/stdaily/maintainer.js b/lib/v2/stdaily/maintainer.js new file mode 100644 index 000000000..b4cc9f141 --- /dev/null +++ b/lib/v2/stdaily/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/digitalpaper': ['lyqluis'], +}; diff --git a/lib/v2/stdaily/radar.js b/lib/v2/stdaily/radar.js new file mode 100644 index 000000000..209925db8 --- /dev/null +++ b/lib/v2/stdaily/radar.js @@ -0,0 +1,11 @@ +module.exports = { + 'stdaily.com': { + _name: '中国科技网', + digitalpaper: [ + { + title: '科技日报', + docs: 'https://docs.rsshub.app/traditional-media.html#zhong-guo-ke-ji-wang', + }, + ], + }, +}; diff --git a/lib/v2/stdaily/router.js b/lib/v2/stdaily/router.js new file mode 100644 index 000000000..10b4f7246 --- /dev/null +++ b/lib/v2/stdaily/router.js @@ -0,0 +1,3 @@ +module.exports = (router) => { + router.get('/digitalpaper', require('./digitalpaper')); +}; diff --git a/lib/v2/stdaily/templates/description.art b/lib/v2/stdaily/templates/description.art new file mode 100644 index 000000000..d12831395 --- /dev/null +++ b/lib/v2/stdaily/templates/description.art @@ -0,0 +1,16 @@ +{{if quotation}} +

{{quotation}}

+{{else if subtitle}} +

{{subtitle}}

+{{/if}} + +{{if pics.length}} + {{each pics}} +
+ +

{{$value.des}}

+
+ {{/each}} +{{/if}} + +{{@article}} \ No newline at end of file