From c6fbc9ecf284fe768f2f2542bdaf6837b180e4a0 Mon Sep 17 00:00:00 2001 From: huangliangshusheng Date: Fri, 1 Apr 2022 11:24:31 +0800 Subject: [PATCH] feat(route): add hameln (#9429) * feat(route): add hameln * feat(route): add hameln * fix doc * Update lib/v2/hameln/chapter.js Co-authored-by: Tony * add timezone Co-authored-by: Tony --- docs/en/reading.md | 10 +++++++ docs/reading.md | 10 +++++++ lib/v2/hameln/chapter.js | 59 +++++++++++++++++++++++++++++++++++++ lib/v2/hameln/maintainer.js | 3 ++ lib/v2/hameln/radar.js | 13 ++++++++ lib/v2/hameln/router.js | 1 + 6 files changed, 96 insertions(+) create mode 100644 lib/v2/hameln/chapter.js create mode 100644 lib/v2/hameln/maintainer.js create mode 100644 lib/v2/hameln/radar.js create mode 100644 lib/v2/hameln/router.js diff --git a/docs/en/reading.md b/docs/en/reading.md index 401d5c189..55e47b04c 100644 --- a/docs/en/reading.md +++ b/docs/en/reading.md @@ -10,6 +10,16 @@ pageClass: routes +## hameln + +### chapter + + + +Eg: + + + ## kakuyomu ### episode diff --git a/docs/reading.md b/docs/reading.md index e4a10936b..147e4afdc 100644 --- a/docs/reading.md +++ b/docs/reading.md @@ -10,6 +10,16 @@ pageClass: routes +## hameln + +### 章节更新 + + + +举例网址: + + + ## kakuyomu ### 章节更新 diff --git a/lib/v2/hameln/chapter.js b/lib/v2/hameln/chapter.js new file mode 100644 index 000000000..6cae1218a --- /dev/null +++ b/lib/v2/hameln/chapter.js @@ -0,0 +1,59 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const timezone = require('@/utils/timezone'); +const { parseDate } = require('@/utils/parse-date'); + +module.exports = async (ctx) => { + const id = ctx.params.id; + const limit = parseInt(ctx.query.limit) || 5; + const link = `https://syosetu.org/novel/${id}`; + const $ = cheerio.load(await get(link)); + + const title = $('span[itemprop="name"]').text(); + const description = $('div.ss:nth-child(2)').text(); + + const chapter_list = $('tr[bgcolor]') + .map((_, chapter) => { + const $_chapter = $(chapter); + const chapter_link = $_chapter.find('a'); + return { + title: chapter_link.text(), + link: chapter_link.attr('href'), + pubDate: timezone(parseDate($_chapter.find('nobr').text(), 'YYYYMMDD HH:mm'), +9), + }; + }) + .toArray() + .sort((a, b) => (a.pubDate <= b.pubDate ? 1 : -1)) + .slice(0, limit); + + const item_list = await Promise.all( + chapter_list.map((chapter) => { + chapter.link = `${link}/${chapter.link}`; + return ctx.cache.tryGet(chapter.link, async () => { + const content = cheerio.load(await get(chapter.link)); + chapter.description = content('#honbun').html(); + return chapter; + }); + }) + ); + + ctx.state.data = { + title, + description, + link, + language: 'ja', + item: item_list, + }; +}; + +const get = async (url) => { + const response = await got({ + method: 'get', + url, + headers: { + cookie: 'over18=off', + }, + }); + + return response.data; +}; diff --git a/lib/v2/hameln/maintainer.js b/lib/v2/hameln/maintainer.js new file mode 100644 index 000000000..695fc5e37 --- /dev/null +++ b/lib/v2/hameln/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/chapter/:id': ['huangliangshusheng'], +}; diff --git a/lib/v2/hameln/radar.js b/lib/v2/hameln/radar.js new file mode 100644 index 000000000..61c717839 --- /dev/null +++ b/lib/v2/hameln/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'syosetu.org': { + _name: 'hameln', + '.': [ + { + title: '章节更新', + docs: 'https://docs.rsshub.app/reading.html#hameln-zhang-jie-geng-xin', + source: ['/novel/:id'], + target: '/hameln/chapter/:id', + }, + ], + }, +}; diff --git a/lib/v2/hameln/router.js b/lib/v2/hameln/router.js new file mode 100644 index 000000000..bc094a683 --- /dev/null +++ b/lib/v2/hameln/router.js @@ -0,0 +1 @@ +module.exports = (router) => router.get('/chapter/:id', require('./chapter.js'));