feat: add Jandan articles (#6774)

* jandan: new route article

* jandan: add docs

* [CodeFactor] Apply fixes

[ci skip] [skip ci]

* jandan: fix

* jandan: codefactor fix

Co-authored-by: duhd1993 <duhd1993@gmail.com>
Co-authored-by: codefactor-io <support@codefactor.io>
This commit is contained in:
Haodong DU
2021-01-25 16:01:38 -05:00
committed by GitHub
parent ad88d2dc4b
commit 1d92ead1e6
3 changed files with 66 additions and 0 deletions

View File

@@ -336,6 +336,10 @@ R18 显示
| ------ | ---------- | ---------- |
| pic | top | top-4h |
### 首页
<Route author="lonelykid" example="/jandan/article" path="/jandan/article"/>
## 绝对领域
### 图集文章

View File

@@ -209,6 +209,7 @@ router.get('/douban/celebrity/:id/:sort?', require('./routes/douban/celebrity.js
router.get('/plainlaw/archives', require('./routes/plainlaw/archives.js'));
// 煎蛋
router.get('/jandan/article', require('./routes/jandan/article'));
router.get('/jandan/:sub_model', require('./routes/jandan/pic'));
// 喷嚏

View File

@@ -0,0 +1,61 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const response = await got({
method: 'get',
url: 'http://jandan.net/',
});
const $ = cheerio.load(response.data);
const list = $('div.list-post').get();
const items = await Promise.all(
list.map(async (li) => {
const $ = cheerio.load(li);
const link = $('a').first().attr('href');
const title = $('a')[1].children[0].data;
const summary = $('div.indexs').clone().children().remove().end().text().trim();
const author = $('div.time_s > a').first().text();
let imgUrl = 'http:' + ($('img').attr('src') || $('img').attr('data-original'));
if (imgUrl.slice(-7,) === "!square") {
imgUrl = imgUrl.slice(0, -7);
}
const [fullText, time] = await ctx.cache.tryGet(link, async () => {
const res = await got.get(link);
const $ = cheerio.load(res.data);
const [, year, month, day, hour, minute] = $('div.time_s')
.text()
.match(/.+@\s+(\d+)\.(\d+)\.(\d+)\s,\s(\d+):(\d+)/);
const time = new Date(`${year}-${month}-${day}T${hour}:${minute}:00+0800`).toUTCString();
const content = $('.f.post').first();
content.find('h1').prevAll().remove().end().remove();
content.find('div').nextAll().remove().end().remove();
content.contents().each(function() {
if (this.nodeType === 8) {
$(this).remove();
}
});
const text = content.html().trim();
return [text, time];
});
const description = `<blockquote><p>${summary}</p></blockquote><img src="${imgUrl}">${fullText}`;
const single = {
title: title,
description: description,
link: link,
pubDate: time,
author: author
};
return Promise.resolve(single);
}));
ctx.state.data = {
title: `煎蛋 - 首页`,
link: `http://jandan.net/`,
description: '煎蛋 - 地球上没有新鲜事',
item: items,
};
};