Skip to content

Commit

Permalink
feat(route): 添加电鸭社区分类文章路由 (DIYgod#11758)
Browse files Browse the repository at this point in the history
* feat(route): 添加电鸭社区分类文章路由

* fix(route): 修复eslint错误

* Update lib/v2/eleduck/posts.js

* Update lib/v2/eleduck/posts.js

* fix(route): 增加维护者文件

* refactor: migrate to v2

---------
  • Loading branch information
running-grass authored Feb 2, 2023
1 parent 247643e commit d5ada18
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 40 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ node_modules
npm-debug.log
error.log
combined.log
.log
.vscode
.idea
.DS_Store
Expand All @@ -23,3 +24,4 @@ Session.vim

assets/build/
package-lock.json
pnpm-lock.yaml
27 changes: 26 additions & 1 deletion docs/bbs.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,32 @@ pageClass: routes

### 工作机会

<Route author="sfyumi" example="/eleduck/jobs" path="/eleduck/jobs"/>
<Route author="sfyumi" example="/eleduck/jobs" path="/eleduck/jobs" radar="1"/>

### 分类文章

<Route author="running-grass" example="/eleduck/posts/4" path="/eleduck/posts/:id?" :paramsDesc="['分类id,可以论坛的URL找到,默认为全部']" radar="1">

| id | 分类 |
| -- | ------ |
| 0 | 全部 |
| 1 | 讨论 |
| 2 | 分享 |
| 3 | 露个脸 |
| 4 | 访谈故事 |
| 5 | 招聘 |
| 10 | 海外移民 |
| 12 | 英语 |
| 14 | 电鸭官方 |
| 15 | 独立产品 |
| 17 | 闲话开源 |
| 19 | Web3 |
| 21 | 设计 |
| 22 | 人才库 |
| 23 | Upwork |
| 24 | 经验课 |

</Route>

## 斗鱼

Expand Down
2 changes: 1 addition & 1 deletion lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2033,7 +2033,7 @@ router.get('/network360/jobs', lazyloadRouteHandler('./routes/network360/jobs'))
router.get('/zhilian/:city/:keyword', lazyloadRouteHandler('./routes/zhilian/index'));

// 电鸭社区
router.get('/eleduck/jobs', lazyloadRouteHandler('./routes/eleduck/jobs'));
// router.get('/eleduck/jobs', lazyloadRouteHandler('./routes/eleduck/jobs'));

// 北华航天工业学院 - 新闻
router.get('/nciae/news', lazyloadRouteHandler('./routes/universities/nciae/news'));
Expand Down
38 changes: 0 additions & 38 deletions lib/routes/eleduck/jobs.js

This file was deleted.

38 changes: 38 additions & 0 deletions lib/v2/eleduck/jobs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const got = require('@/utils/got');
const { parseDate } = require('@/utils/parse-date');

module.exports = async (ctx) => {
const { data: response } = await got('https://svc.eleduck.com/api/v1/posts', {
searchParams: {
category: 5,
},
});

const data = response.posts.map((item) => ({
id: item.id,
title: item.title,
link: `https://eleduck.com/posts/${item.id}`,
author: item.user.nickname,
description: item.summary,
pubDate: parseDate(item.published_at),
category: item.tags.map((tag) => tag.name),
}));

const out = await Promise.all(
data.map((job) =>
ctx.cache.tryGet(job.link, async () => {
const { data: jobDetail } = await got(`https://svc.eleduck.com/api/v1/posts/${job.id}`);

job.description = jobDetail.post.content;

return job;
})
)
);

ctx.state.data = {
title: '招聘 | 电鸭社区',
link: 'https://eleduck.com/category/5',
item: out,
};
};
4 changes: 4 additions & 0 deletions lib/v2/eleduck/maintainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
'/jobs': ['sfyumi'],
'/posts/:id?': ['running-grass'],
};
42 changes: 42 additions & 0 deletions lib/v2/eleduck/posts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const got = require('@/utils/got');

const getCateName = async (ctx, cid = 0) => {
const key = 'eleduck-categories';
const cates = await ctx.cache.tryGet(key, async () => {
const res = await got(`https://svc.eleduck.com/api/v1/categories`);
const map = {};
res.data.categories.forEach((item) => {
map[item.id] = item.name;
});
return map;
});

return cates[cid] || '全部';
};

module.exports = async (ctx) => {
const cid = ctx.params.id || 0;

const response = await got(`https://svc.eleduck.com/api/v1/posts?category=${cid}`);

const { posts } = response.data;
if (posts === undefined) {
throw new Error("没有获取到数据");
}

const cname = await getCateName(ctx, cid);

ctx.state.data = {
title: `电鸭社区的文章--${cname}`,
link: `https://eleduck.com/categories/${cid}`,
description: `电鸭社区的文章,栏目为${cname}`,
item: posts.map((item) => ({
title: item.title,
description: item.summary,
pubDate: item.published_at,
link: `https://eleduck.com/${item.category.id === 22 ? 'tposts' : 'posts'}/${item.id}`,
author: item.user.nickname,
})),
};

};
25 changes: 25 additions & 0 deletions lib/v2/eleduck/radar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = {
'eleduck.com': {
_name: '电鸭社区',
'.': [
{
title: '工作机会',
docs: 'https://docs.rsshub.app/bbs.html#dian-ya-she-qu-fen-lei-wen-zhang',
source: ['/categories/5', '/'],
target: '/eleduck/jobs',
},
{
title: '分类文章',
docs: 'https://docs.rsshub.app/bbs.html#dian-ya-she-qu-fen-lei-wen-zhang',
source: '/categories/:cid',
target: (params) => `/eleduck/posts${params.cid}`,
},
{
title: '全部文章',
docs: 'https://docs.rsshub.app/bbs.html#dian-ya-she-qu-fen-lei-wen-zhang',
source: '*',
target: () => '/eleduck/posts',
},
],
},
};
4 changes: 4 additions & 0 deletions lib/v2/eleduck/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = (router) => {
router.get('/jobs', require('./jobs'));
router.get('/posts/:id?', require('./posts'));
};

0 comments on commit d5ada18

Please sign in to comment.