From c121ba30fadac74d52e3427bf9a61dcd72ec968d Mon Sep 17 00:00:00 2001 From: cubroe Date: Fri, 7 Apr 2023 20:18:20 +0800 Subject: [PATCH] feat(route): selected new books or specified book list on FreeComputerBooks.com (#12234) --- docs/en/reading.md | 10 ++++ docs/reading.md | 10 ++++ lib/v2/freecomputerbooks/index.js | 66 ++++++++++++++++++++++++++ lib/v2/freecomputerbooks/maintainer.js | 4 ++ lib/v2/freecomputerbooks/radar.js | 31 ++++++++++++ lib/v2/freecomputerbooks/router.js | 3 ++ 6 files changed, 124 insertions(+) create mode 100644 lib/v2/freecomputerbooks/index.js create mode 100644 lib/v2/freecomputerbooks/maintainer.js create mode 100644 lib/v2/freecomputerbooks/radar.js create mode 100644 lib/v2/freecomputerbooks/router.js diff --git a/docs/en/reading.md b/docs/en/reading.md index 8cd10f2fd..4da6df1f6 100644 --- a/docs/en/reading.md +++ b/docs/en/reading.md @@ -10,6 +10,16 @@ pageClass: routes +## Free Computer Books + +### Selected New Books + + + +### Current Book List + + + ## hameln ### chapter diff --git a/docs/reading.md b/docs/reading.md index 8a3ca6b80..86903b19c 100644 --- a/docs/reading.md +++ b/docs/reading.md @@ -10,6 +10,16 @@ pageClass: routes +## Free Computer Books + +### 精选上新 + + + +### 当前书单 + + + ## hameln ### 章节更新 diff --git a/lib/v2/freecomputerbooks/index.js b/lib/v2/freecomputerbooks/index.js new file mode 100644 index 000000000..e9c5668c9 --- /dev/null +++ b/lib/v2/freecomputerbooks/index.js @@ -0,0 +1,66 @@ +const cheerio = require('cheerio'); + +const got = require('@/utils/got'); +const { parseDate } = require('@/utils/parse-date'); + +const baseURL = 'https://freecomputerbooks.com/'; + +module.exports = async (ctx) => { + const categoryId = ctx.params.category?.trim(); + const requestURL = categoryId ? new URL(`${categoryId}.html`, baseURL).href : baseURL; + + const response = await got({ method: 'get', url: requestURL }); + const $ = cheerio.load(response.data); + + // As observation has shown that each page only has one element of the + // class, thus to simplify the processing the text is directly extracted. + // Needing more robust processing if some day more such elements show up. + const categoryTitle = $('.maintitlebar').text(); + + ctx.state.data = { + title: 'Free Computer Books - ' + categoryTitle, + link: requestURL, + description: $('title').text(), + + // For a "Selected New Books" page, the
    element's id. is + // `newBooksG`; for an ordinary category page, it's `newBooksL`. + item: $('ul[id^=newBooks] > li') + .toArray() + .map((elem) => buildPostItem($(elem), categoryTitle, $)), + }; +}; + +function buildPostItem(listItem, categoryTitle, $) { + // Cover in original size, e.g., `covers/xyz_43x55.jpg` -> `covers/xyz.jpg` + const image = listItem.find('img'); + image.attr('src', image.attr('src').replace(/_\d+x\d+/, '')); + + const postLink = listItem.find('a:first'); + const postInfo = listItem.find('p:contains("Post under")'); + const postItem = { + title: postLink.text(), + link: postLink.attr('href'), + description: listItem.html(), + + // Only a "Selected New Books" page has exclicit categorization info. + // for posts; an ordinary category page hasn't, then in which case the + // category title is used (after all, it's already a *category page*). + category: postInfo.length + ? postInfo + .find('a') + .toArray() + .map((elem) => $(elem).text()) + : categoryTitle, + }; + + const pubDateText = postInfo.find('span:last').text().replace(/^on /, ''); + if (pubDateText) { + // Pretty much the same situation: Only a "Selected New Books" page has + // explicit publication dates for posts; even on each post's details + // page, there seems to be only the publication date for the book, but + // the post's creation date is still missing. + postItem.pubDate = parseDate(pubDateText); + } + + return postItem; +} diff --git a/lib/v2/freecomputerbooks/maintainer.js b/lib/v2/freecomputerbooks/maintainer.js new file mode 100644 index 000000000..81dc4f9e7 --- /dev/null +++ b/lib/v2/freecomputerbooks/maintainer.js @@ -0,0 +1,4 @@ +module.exports = { + '/': ['cubroe'], + '/:category': ['cubroe'], +}; diff --git a/lib/v2/freecomputerbooks/radar.js b/lib/v2/freecomputerbooks/radar.js new file mode 100644 index 000000000..e1df3a525 --- /dev/null +++ b/lib/v2/freecomputerbooks/radar.js @@ -0,0 +1,31 @@ +module.exports = { + 'freecomputerbooks.com': { + _name: 'Free Computer Books', + '.': [ + { + title: 'Selected New Books', + docs: 'https://docs.rsshub.app/en/reading.html#free-computer-books', + source: ['/', '/index.html'], + target: '/freecomputerbooks', + }, + { + title: 'Current Book List', + docs: 'https://docs.rsshub.app/en/reading.html#free-computer-books', + source: ['/:category'], + target: (params, _, document) => { + const categoryId = params.category.replace('.html', ''); + + if (categoryId === 'index') { + return; // only matching the "Selected New Books" rule above + } + + if (!document.querySelector('ul[id^=newBooks]')) { + return; // not a proper book list page + } + + return '/freecomputerbooks/' + categoryId; + }, + }, + ], + }, +}; diff --git a/lib/v2/freecomputerbooks/router.js b/lib/v2/freecomputerbooks/router.js new file mode 100644 index 000000000..cf781366f --- /dev/null +++ b/lib/v2/freecomputerbooks/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/:category?', require('./index')); +};