From aeb111cfe06f0b4e6796d2cc7bae6f4c3eaf204f Mon Sep 17 00:00:00 2001 From: Hwantae Ji Date: Sun, 8 Dec 2019 03:41:22 +0900 Subject: [PATCH] feat: add naver webtoon (#3376) --- docs/anime.md | 4 +++ docs/en/anime.md | 6 +++++ lib/router.js | 1 + lib/routes/webtoons/naver.js | 50 ++++++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 lib/routes/webtoons/naver.js diff --git a/docs/anime.md b/docs/anime.md index 8dd1cf93c7..16599a0cd1 100644 --- a/docs/anime.md +++ b/docs/anime.md @@ -165,6 +165,10 @@ pageClass: routes 比如漫画公主彻夜未眠的网址为https://www.webtoons.com/zh-hant/drama/gongzhuweimian/list?title_no=894, 则`lang=zh-hant`,`category=drama`,`name=gongzhucheyeweimian`,`id=894`. +### [Naver](https://comic.naver.com) + + + ## 嘀哩嘀哩 - dilidili ### 嘀哩嘀哩番剧更新 diff --git a/docs/en/anime.md b/docs/en/anime.md index d7a5444f37..8abc6b2072 100644 --- a/docs/en/anime.md +++ b/docs/en/anime.md @@ -15,3 +15,9 @@ pageClass: routes | serial | finish | + +## Webtoons + +### [Naver](https://comic.naver.com) + + diff --git a/lib/router.js b/lib/router.js index fc1bd32cd1..51191b282f 100644 --- a/lib/router.js +++ b/lib/router.js @@ -926,6 +926,7 @@ router.get('/vol/:mode?', require('./routes/vol/lastupdate')); router.get('/dongmanmanhua/:category/:name/:id', require('./routes/dongmanmanhua/comic')); // webtoons router.get('/webtoons/:lang/:category/:name/:id', require('./routes/webtoons/comic')); +router.get('/webtoons/naver/:id', require('./routes/webtoons/naver')); // Tits Guru router.get('/tits-guru/home', require('./routes/titsguru/home')); diff --git a/lib/routes/webtoons/naver.js b/lib/routes/webtoons/naver.js new file mode 100644 index 0000000000..809b6ae444 --- /dev/null +++ b/lib/routes/webtoons/naver.js @@ -0,0 +1,50 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const qs = require('querystring'); +const domain = 'https://comic.naver.com'; + +async function getDescription(ctx, link) { + const { titleId, no } = qs.parse(link.substr(link.indexOf('?') + 1)); + const key = `webtoons/naver/${titleId}/${no}`; + + let result = await ctx.cache.get(key); + if (result) { + return result; + } + + const { body } = await got.get(link); + const $ = cheerio.load(body); + result = $('#comic_view_area > div.wt_viewer').html(); + ctx.cache.set(key, result); + return result; +} + +async function getItems(ctx, $) { + return Promise.all( + $('#content > table > tbody > tr') + .toArray() + .filter((ep) => !ep.attribs.class) + .map(async (ep) => ({ + title: $('td.title > a', ep).text(), + pubDate: new Date($('.num', ep).text()).toUTCString(), + link: `${domain}${$('td.title > a', ep).attr('href')}`, + description: await getDescription(ctx, `${domain}${$('td.title > a', ep).attr('href')}`), + })) + ); +} + +module.exports = async (ctx) => { + const { id } = ctx.params; + const comicLink = `${domain}/webtoon/list.nhn?titleId=${id}`; + + const { body } = await got.get(comicLink); + const $ = cheerio.load(body); + const rss = { + title: $('head title').text(), + link: comicLink, + description: $('#content > div.comicinfo > div.detail > p:nth-child(2)').text(), + item: await getItems(ctx, $), + }; + rss.item = rss.item.sort((a, b) => new Date(b.pubDate) - new Date(a.pubDate)); + ctx.state.data = rss; +};