feat: 新增龙空分区、帖子 (#2707)

This commit is contained in:
ma jia cong
2019-08-01 18:22:11 +08:00
committed by DIYgod
parent 975e264763
commit 3c652fe159
4 changed files with 144 additions and 0 deletions
+10
View File
@@ -598,6 +598,16 @@ pageClass: routes
<Route author="DIYgod HenryQW" example="/jianshu/user/yZq3ZV" path="/jianshu/user/:id" :paramsDesc="['作者 id, 可在作者主页 URL 中找到']"/>
## 龙空
### 分区
<Route author="ma6254" example="/lkong/forum/60" path="/lkong/forum/:id?" :paramsDesc="['分区 id, 可在分区的URL里找到']">
### 帖子
<Route author="ma6254" example="/lkong/thread/2356933" path="/lkong/thread/:id?" :paramsDesc="['帖子 id, 可在帖子的URL里找到']">
## 龙腾网
### 转译网贴
+5
View File
@@ -1583,6 +1583,11 @@ router.get('/yidoutang/case/:type', require('./routes/yidoutang/case.js'));
// 开眼
router.get('/kaiyan/index', require('./routes/kaiyan/index'));
// 龙空
router.get('/lkong/forum/:id', require('./routes/lkong/forum'));
router.get('/lkong/thread/:id', require('./routes/lkong/thread'));
// router.get('/lkong/user/:id', require('./routes/lkong/user'));
// 乃木坂46官网
router.get('/nogizaka46/news', require('./routes/nogizaka46/news'));
+73
View File
@@ -0,0 +1,73 @@
const got = require('@/utils/got');
module.exports = async (ctx) => {
const id = ctx.params.id;
const response = await got({
method: 'get',
url: `http://lkong.cn/forum/index.php?mod=ajax&action=forumconfig_${id}`,
});
const forumconfig = response.data;
const response_forum_item = await got({
method: 'get',
url: `http://lkong.cn/forum/index.php?mod=data&sars=forum/${id}`,
});
const data = response_forum_item.data;
const items = await Promise.all(
data.data.map(async (i) => {
const item = {
title: i.subject,
pubDate: new Date(i.dateline + ' +8').toUTCString(),
};
if (i.id.startsWith('thread_')) {
const thread_id = i.id.substr('thread_'.length);
const thread_list_url = `http://lkong.cn/thread/index.php?mod=data&sars=thread/${thread_id}`;
const thread_config_url = `http://lkong.cn/forum/index.php?mod=ajax&action=threadconfig_${thread_id}`;
let thread_list;
let thread_config;
const cache = await ctx.cache.get(thread_list_url);
if (cache) {
thread_list = JSON.parse(cache);
} else {
const response_thread_list = await got({
method: 'get',
url: thread_list_url,
});
thread_list = response_thread_list.data;
ctx.cache.set(thread_list_url, JSON.stringify(thread_list));
}
const cache1 = await ctx.cache.get(thread_config_url);
if (cache1) {
thread_config = JSON.parse(cache1);
} else {
const response_thread_config = await got({
method: 'get',
url: thread_config_url,
});
thread_config = response_thread_config.data;
ctx.cache.set(thread_config_url, JSON.stringify(thread_config));
}
item.link = `http://lkong.cn/thread/${thread_id}`;
item.description = thread_list.data[0].message;
item.title = thread_config.subject;
}
return Promise.resolve(item);
})
);
ctx.state.data = {
title: `龙空 ${forumconfig.name}`,
link: `http://lkong.cn/forum/${id}`,
description: forumconfig.blackboard,
image: `http://img.lkong.cn/forumavatar/000/00/00/${id}_avatar_middle.jpg`,
item: items,
};
};
+56
View File
@@ -0,0 +1,56 @@
const got = require('@/utils/got');
module.exports = async (ctx) => {
const id = ctx.params.id;
const response_thread_config = await got({
method: 'get',
url: `http://lkong.cn/forum/index.php?mod=ajax&action=threadconfig_${id}`,
});
const threadConfig = response_thread_config.data;
function* genPage(i) {
for (let a = 0; a < i; a++) {
yield a + 1;
}
}
const thread_item = await Promise.all(
[...genPage(threadConfig.replies / 20)].map(async (page) => {
const thread_page_url = `http://lkong.cn/forum/index.php?mod=data&sars=thread/${id}/${page}`;
let thread_page;
const cache = await ctx.cache.get(thread_page_url);
if (cache) {
thread_page = JSON.parse(cache);
} else {
const response = await got({
method: 'get',
url: `http://lkong.cn/forum/index.php?mod=data&sars=thread/${id}/${page}`,
});
thread_page = response.data;
ctx.cache.set(thread_page_url, JSON.stringify(thread_page));
}
const new_item = thread_page.data.map((i) => ({
title: `${i.lou}`,
pubDate: new Date(i.dateline + ' +8').toUTCString(),
author: i.author,
description: i.message,
link: `http://lkong.cn/thread/${id}/${page}.p_${i.pid}`,
}));
return new_item;
})
);
const thread_items = [];
thread_item.forEach((page_item) => {
page_item.forEach((item) => {
thread_items.push(item);
});
});
ctx.state.data = {
title: `龙空 ${threadConfig.forumname} ${threadConfig.subject}`,
link: `http://lkong.cn/thread/${id}`,
item: thread_items,
};
};