From 75a087dd72a5efd29838f661dcc220dd2458e50c Mon Sep 17 00:00:00 2001 From: Xiaoxu Guo Date: Mon, 25 Sep 2023 00:07:22 +0800 Subject: [PATCH] feat(route): added hackyournews (#13317) * added hackyournews * removed the 'Archive' suffix in the title * cleaned up & fixed line breaks * cleaned up * fixed the doc * resolved --- lib/v2/hackyournews/index.js | 50 ++++++++++++++++++++++++++++++ lib/v2/hackyournews/maintainer.js | 4 +++ lib/v2/hackyournews/radar.js | 13 ++++++++ lib/v2/hackyournews/router.js | 3 ++ website/docs/routes/programming.md | 7 +++++ 5 files changed, 77 insertions(+) create mode 100644 lib/v2/hackyournews/index.js create mode 100644 lib/v2/hackyournews/maintainer.js create mode 100644 lib/v2/hackyournews/radar.js create mode 100644 lib/v2/hackyournews/router.js diff --git a/lib/v2/hackyournews/index.js b/lib/v2/hackyournews/index.js new file mode 100644 index 0000000000..e2223339aa --- /dev/null +++ b/lib/v2/hackyournews/index.js @@ -0,0 +1,50 @@ +// Require necessary modules +const got = require('@/utils/got'); // a customised got +const cheerio = require('cheerio'); // an HTML parser with a jQuery-like API +const { parseDate } = require('@/utils/parse-date'); + +module.exports = async (ctx) => { + const baseUrl = 'https://hackyournews.com'; + const { data: response } = await got(baseUrl); + const $ = cheerio.load(response); + + const item = $('tr.story') + .map((_, story) => { + const title = $(story).find('a').first().text(); + const nextRow = $(story).next(); + const metas = nextRow.text().trimStart().split('|'); + const upvotes = parseInt(metas[0].split(' points')[0].trim()); + const author = metas[0].split('by')[1].trim(); + const pubDate = parseDate(metas[1].trim()); + let category = []; + // NOTE: If the summary is not already proceeded, we cannot get the category. + if (metas.length === 5) { + category = [metas[2].trim(), metas[3].trim()]; + } + const a = nextRow.find('a'); + const link = a.attr('href'); + const comments = parseInt(a.text()); + const description = nextRow + .find('p') + .map((_, p) => $(p).text()) + .get() + .join('
'); + return { + title, + link, + author, + category, + comments, + upvotes, + pubDate, + description, + }; + }) + .get(); + + ctx.state.data = { + title: 'Index', + link: baseUrl, + item, + }; +}; diff --git a/lib/v2/hackyournews/maintainer.js b/lib/v2/hackyournews/maintainer.js new file mode 100644 index 0000000000..2b11a400b7 --- /dev/null +++ b/lib/v2/hackyournews/maintainer.js @@ -0,0 +1,4 @@ +module.exports = { + '/': ['ftiasch'], +}; + diff --git a/lib/v2/hackyournews/radar.js b/lib/v2/hackyournews/radar.js new file mode 100644 index 0000000000..f9aba35215 --- /dev/null +++ b/lib/v2/hackyournews/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'hackyournews.com': { + _name: 'HackYourNews', + '.': [ + { + title: 'Index', + docs: 'https://docs.rsshub.app/routes/programming#hack-your-news', + source: ['/'], + target: '/hackyournews', + }, + ], + }, +}; diff --git a/lib/v2/hackyournews/router.js b/lib/v2/hackyournews/router.js new file mode 100644 index 0000000000..7e427447c7 --- /dev/null +++ b/lib/v2/hackyournews/router.js @@ -0,0 +1,3 @@ +module.exports = (router) => { + router.get('/', require('./index')); +}; diff --git a/website/docs/routes/programming.md b/website/docs/routes/programming.md index 6163e73cc2..2831b3a57f 100644 --- a/website/docs/routes/programming.md +++ b/website/docs/routes/programming.md @@ -495,6 +495,13 @@ Subscribe to the updates (threads and submission) from a paritcular Hacker News + +## HackYourNews {#hack-your-news} + +### Index {#hack-your-news-index} + + + ## HelloGitHub {#hellogithub} ### 热门 {#hellogithub-re-men}