feat(route): selected new books or specified book list on FreeComputerBooks.com (#12234)

This commit is contained in:
cubroe
2023-04-07 20:18:20 +08:00
committed by GitHub
parent 6c89b3804a
commit c121ba30fa
6 changed files with 124 additions and 0 deletions
+10
View File
@@ -10,6 +10,16 @@ pageClass: routes
<RouteEn author="HenryQW" example="/allpoetry/newest" path="/allpoetry/:order?" :paramsDesc="['Ordering, `best` or `newest`, `best` by default']"/>
## Free Computer Books
### Selected New Books
<RouteEn author="cubroe" example="/freecomputerbooks" path="/freecomputerbooks" radar="1" />
### Current Book List
<RouteEn author="cubroe" example="/freecomputerbooks/compscAlgorithmBooks" path="/freecomputerbooks/:category" :paramsDesc="['A category id., which should be the HTML file name (but **without** the `.html` suffix) in the URL path of a book list page.']" radar="1" />
## hameln
### chapter
+10
View File
@@ -10,6 +10,16 @@ pageClass: routes
<Route author="HenryQW" example="/allpoetry/newest" path="/allpoetry/:order?" :paramsDesc="['排序方式, `best` 或 `newest`, 缺省 `best`']"/>
## Free Computer Books
### 精选上新
<Route author="cubroe" example="/freecomputerbooks" path="/freecomputerbooks" radar="1" />
### 当前书单
<Route author="cubroe" example="/freecomputerbooks/compscAlgorithmBooks" path="/freecomputerbooks/:category" :paramsDesc="['分类标识符,为源站URL路径中的HTML文件名(去除`.html`后缀)']" radar="1" />
## hameln
### 章节更新
+66
View File
@@ -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 <ul> 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;
}
+4
View File
@@ -0,0 +1,4 @@
module.exports = {
'/': ['cubroe'],
'/:category': ['cubroe'],
};
+31
View File
@@ -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;
},
},
],
},
};
+3
View File
@@ -0,0 +1,3 @@
module.exports = function (router) {
router.get('/:category?', require('./index'));
};