From ecd784fc4aa160fab864fa6c6438dfe505f85434 Mon Sep 17 00:00:00 2001
From: KotoriK <52659125+KotoriK@users.noreply.github.com>
Date: Sun, 8 Nov 2020 09:18:01 +0800
Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9Aadd=208kcosplay=20(#6097)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
docs/picture.md | 10 ++++++++++
lib/router.js | 4 ++++
lib/routes/8kcos/article.js | 17 +++++++++++++++++
lib/routes/8kcos/cat.js | 28 ++++++++++++++++++++++++++++
lib/routes/8kcos/const.js | 4 ++++
lib/routes/8kcos/latest.js | 27 +++++++++++++++++++++++++++
6 files changed, 90 insertions(+)
create mode 100644 lib/routes/8kcos/article.js
create mode 100644 lib/routes/8kcos/cat.js
create mode 100644 lib/routes/8kcos/const.js
create mode 100644 lib/routes/8kcos/latest.js
diff --git a/docs/picture.md b/docs/picture.md
index b52bed09b..a5d076e7f 100644
--- a/docs/picture.md
+++ b/docs/picture.md
@@ -36,6 +36,16 @@ pageClass: routes
+## 8KCosplay
+
+### 最新
+
+
+
+### 分类
+
+
+
## Bing 壁纸
### 每日壁纸
diff --git a/lib/router.js b/lib/router.js
index 1b029baef..ebd7f3875 100644
--- a/lib/router.js
+++ b/lib/router.js
@@ -3468,4 +3468,8 @@ router.get('/grandchallenge/challenges', require('./routes/grandchallenge/challe
// 西北工业大学
router.get('/nwpu/:column', require('./routes/nwpu/index'));
+// 8kcos
+router.get('/8kcos/', require('./routes/8kcos/latest'));
+router.get('/8kcos/cat/:cat*', require('./routes/8kcos/cat'));
+
module.exports = router;
diff --git a/lib/routes/8kcos/article.js b/lib/routes/8kcos/article.js
new file mode 100644
index 000000000..5ddbfe7a1
--- /dev/null
+++ b/lib/routes/8kcos/article.js
@@ -0,0 +1,17 @@
+const Cheerio = require('cheerio');
+const got = require('got');
+async function loadArticle(link) {
+ const resp = await got(link);
+ const article = Cheerio.load(resp.body);
+ const title = article('.entry-title').text();
+ const entry_children = article('div.entry-content').children().toArray();
+ const imgs = Cheerio.load(entry_children.shift()).text(); // 去除懒加载代码减少返回体积
+ const txt = Cheerio.load(entry_children).html();
+ return {
+ title,
+ description: imgs.concat(txt),
+ pubDate: article('time')[0].attribs.datetime.replace('>', ''),
+ link,
+ };
+}
+module.exports = loadArticle;
diff --git a/lib/routes/8kcos/cat.js b/lib/routes/8kcos/cat.js
new file mode 100644
index 000000000..a5038a193
--- /dev/null
+++ b/lib/routes/8kcos/cat.js
@@ -0,0 +1,28 @@
+const got = require('got');
+const Cheerio = require('cheerio');
+const { SUB_NAME_PREFIX, SUB_URL } = require('./const');
+const loadArticle = require('./article');
+module.exports = async (ctx) => {
+ const { cat = '8kasianidol' } = ctx.params;
+ const url = `${SUB_URL}category/${cat}/`;
+ const resp = await got(url);
+ const $ = Cheerio.load(resp.body);
+ ctx.state.data = resp.body
+ ? {
+ title: `${SUB_NAME_PREFIX}-${$('#wrap > div > div > div > div.sec-panel-head > h1 > span').text()}`,
+ link: url,
+ item: await Promise.all(
+ Cheerio.load(resp.body)('#wrap > div > div > div > ul > li.item')
+ .map((_, e) => {
+ const { href } = Cheerio.load(e)('h2 > a')[0].attribs;
+ return ctx.cache.tryGet(href, async () => loadArticle(href));
+ })
+ .toArray()
+ ),
+ }
+ : {
+ title: `${SUB_NAME_PREFIX}-${cat}`,
+ link: url,
+ item: [{ title: '获取失败' }],
+ };
+};
diff --git a/lib/routes/8kcos/const.js b/lib/routes/8kcos/const.js
new file mode 100644
index 000000000..78a66d65b
--- /dev/null
+++ b/lib/routes/8kcos/const.js
@@ -0,0 +1,4 @@
+module.exports = {
+ SUB_NAME_PREFIX: '8KCosplay',
+ SUB_URL: 'https://www.8kcosplay.com/',
+};
diff --git a/lib/routes/8kcos/latest.js b/lib/routes/8kcos/latest.js
new file mode 100644
index 000000000..367ed4b21
--- /dev/null
+++ b/lib/routes/8kcos/latest.js
@@ -0,0 +1,27 @@
+const got = require('got');
+const Cheerio = require('cheerio');
+const { SUB_NAME_PREFIX, SUB_URL } = require('./const');
+const loadArticle = require('./article');
+const url = SUB_URL;
+
+module.exports = async (ctx) => {
+ const response = await got(url);
+ ctx.state.data = {
+ title: `${SUB_NAME_PREFIX}-最新`,
+ link: url,
+ item: response.body
+ ? await Promise.all(
+ Cheerio.load(response.body)('#wrap > div > div > div > ul > li.item')
+ .map((_, e) => {
+ const { href } = Cheerio.load(e)('div > h2 > a')[0].attribs;
+ return ctx.cache.tryGet(href, async () => loadArticle(href));
+ })
+ .toArray()
+ )
+ : [
+ {
+ title: '获取失败!',
+ },
+ ],
+ };
+};