From 4e809d51fad4b3c367ee55fea2d0730a22805781 Mon Sep 17 00:00:00 2001 From: Tony Date: Thu, 10 Aug 2023 04:31:16 +0800 Subject: [PATCH] feat(route): thehindu topic (#12964) --- docs/en/traditional-media.md | 8 ++++- lib/v2/thehindu/maintainer.js | 3 ++ lib/v2/thehindu/radar.js | 13 ++++++++ lib/v2/thehindu/router.js | 3 ++ lib/v2/thehindu/topic.js | 57 +++++++++++++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 lib/v2/thehindu/maintainer.js create mode 100644 lib/v2/thehindu/radar.js create mode 100644 lib/v2/thehindu/router.js create mode 100644 lib/v2/thehindu/topic.js diff --git a/docs/en/traditional-media.md b/docs/en/traditional-media.md index 271983b9a..ddbb39b06 100644 --- a/docs/en/traditional-media.md +++ b/docs/en/traditional-media.md @@ -118,7 +118,7 @@ There is no RSS source for Al Jazeera Chinese, returning homepage content by def ### Topics - + ## BBC @@ -678,6 +678,12 @@ Provides a better reading experience (full text articles) over the official one. +## The Hindu + +### Topic + + + ## The New York Times ### News diff --git a/lib/v2/thehindu/maintainer.js b/lib/v2/thehindu/maintainer.js new file mode 100644 index 000000000..2d70ea702 --- /dev/null +++ b/lib/v2/thehindu/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/topic/:topic': ['TonyRL'], +}; diff --git a/lib/v2/thehindu/radar.js b/lib/v2/thehindu/radar.js new file mode 100644 index 000000000..22fe27c27 --- /dev/null +++ b/lib/v2/thehindu/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'thehindu.com': { + _name: 'The Hindu', + '.': [ + { + title: 'Topic', + docs: 'https://docs.rsshub.app/en/traditional-media.html#the-hindu', + source: ['/topic/:topic'], + target: '/thehindu/topic/:topic', + }, + ], + }, +}; diff --git a/lib/v2/thehindu/router.js b/lib/v2/thehindu/router.js new file mode 100644 index 000000000..3327b76de --- /dev/null +++ b/lib/v2/thehindu/router.js @@ -0,0 +1,3 @@ +module.exports = (router) => { + router.get('/topic/:topic', require('./topic')); +}; diff --git a/lib/v2/thehindu/topic.js b/lib/v2/thehindu/topic.js new file mode 100644 index 000000000..07de003ba --- /dev/null +++ b/lib/v2/thehindu/topic.js @@ -0,0 +1,57 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); + +module.exports = async (ctx) => { + const baseUrl = 'https://www.thehindu.com'; + const { topic } = ctx.params; + const link = `${baseUrl}/topic/${topic}/`; + const apiLink = `${baseUrl}/topic/${topic}/fragment/showmoreTag`; + + const { data: response } = await got(link); + const { data: apiResponse } = await got(apiLink); + + const $ = cheerio.load(response); + + const $api = cheerio.load(apiResponse); + const list = $('.element') + .toArray() + .map((item) => { + item = $api(item); + const a = item.find('.title a'); + return { + title: a.text().trim(), + link: a.attr('href'), + author: item.find('.author-name').text(), + }; + }); + + const items = await Promise.all( + list.map((item) => + ctx.cache.tryGet(item.link, async () => { + const { data: response } = await got(item.link); + const $ = cheerio.load(response); + + $('.position-relative, .articleblock-container, .article-ad, .comments-shares').remove(); + item.description = $('.sub-title').prop('outerHTML') + $('div.article-picture').html() + $('div[itemprop="articleBody"]').html(); + item.pubDate = parseDate($('meta[itemprop="datePublished"]').attr('content')); + item.updated = parseDate($('meta[itemprop="dateModified"]').attr('content')); + item.category = $('meta[property="article:tag"]') + .toArray() + .map((item) => $(item).attr('content')); + + return item; + }) + ) + ); + + ctx.state.data = { + title: $('head title').text().trim(), + link: `${baseUrl}/topic/${topic}/`, + image: $('meta[property="og:image"]').attr('content'), + logo: $('link[rel="apple-touch-icon"]').attr('href'), + icon: $('link[rel="icon"]').attr('href'), + language: 'en', + item: items, + }; +};