Skip to content

Commit 0bbe5ef

Browse files
authored
feat: new article sub scriptions (#73)
* feat: api url for test by postMan * feat: add DEV Architecture for articleSubscription * feat: add React Status for articleSubscrption * feat: support every monday * feat: for new articleSubScriptions * feat: update juejin params
1 parent 94662df commit 0bbe5ef

File tree

9 files changed

+82
-7
lines changed

9 files changed

+82
-7
lines changed

app/consts/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// 订阅网站的一级分类
2+
export const SITE_NAME = {
3+
GITHUB: 'Github',
4+
JUEJIN: '掘金',
5+
ENGLISH: '英文'
6+
}
7+
8+
// 订阅网站的二级分类 - 话题
9+
export const TOPIC_NAME = {
10+
DEV_ARCHITECTURE: 'DEV Architecture',
11+
REACT_STATUS: 'React Status'
12+
}

app/controller/articleSubscription.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ class ArticleSubscriptionController extends Controller {
8484
if (_.isNil(data)) throw new Error('找不到该 id 下的相关信息');
8585
ctx.body = app.utils.response(true, data);
8686
}
87+
88+
// 用于 Postman 调试文章订阅源
89+
async testArticle() {
90+
const { ctx, app } = this;
91+
const { id } = ctx.query;
92+
const data = await ctx.service.articleSubscription.sendArticleSubscription(id);
93+
ctx.body = app.utils.response(true, data);
94+
}
8795
}
8896

8997
module.exports = ArticleSubscriptionController;

app/router.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ module.exports = app => {
7575
/**
7676
* 文章订阅
7777
*/
78+
app.get('/api/article-subscription/test-article', app.controller.articleSubscription.testArticle);
7879
app.post('/api/article-subscription/create-subscription', app.controller.articleSubscription.createSubscription);
7980
app.post('/api/article-subscription/delete-subscription', app.controller.articleSubscription.deleteSubscription);
8081
app.post('/api/article-subscription/update-subscription', app.controller.articleSubscription.updateSubscription);

app/service/articleSubscription.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const Service = require('egg').Service
2-
const { getGithubTrendingFromServerless, getJueJinHot, customMessage } = require('../utils/articleSubscription')
2+
const { SITE_NAME, TOPIC_NAME } = require('../consts')
3+
const { getGithubTrendingFromServerless, getJueJinHot, getDevArchitectureHot, getReactStatusHot, customMessage } = require('../utils/articleSubscription')
34

45
class ArticleSubscriptionService extends Service {
56
// 获取列表
@@ -50,8 +51,10 @@ class ArticleSubscriptionService extends Service {
5051

5152
for (let item of topicList) {
5253
const { siteName, topicName, topicUrl } = item
53-
siteName === 'Github' && getGithubTrendingFromServerless(id, groupName, siteName, topicName, topicUrl, webHook, this.app)
54-
siteName === '掘金' && getJueJinHot(id, groupName, siteName, topicName, topicUrl, webHook, this.app)
54+
siteName === SITE_NAME.GITHUB && getGithubTrendingFromServerless(id, groupName, siteName, topicName, topicUrl, webHook, this.app)
55+
siteName === SITE_NAME.JUEJIN && getJueJinHot(id, groupName, siteName, topicName, topicUrl, webHook, this.app)
56+
topicName === TOPIC_NAME.DEV_ARCHITECTURE && getDevArchitectureHot(id, groupName, siteName, topicName, topicUrl, webHook, this.app)
57+
topicName === TOPIC_NAME.REACT_STATUS && getReactStatusHot(id, groupName, siteName, topicName, topicUrl, webHook, this.app)
5558
}
5659
}
5760
}
@@ -66,7 +69,7 @@ class ArticleSubscriptionService extends Service {
6669
order: [['created_at', 'DESC']],
6770
raw: true
6871
})
69-
72+
7073
for (let i of subscriptionList) {
7174
const { id, sendCron } = i
7275
this.app.messenger.sendToAgent('createTimedTask', { id, sendCron })

app/utils/articleSubscription.js

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ const getGithubTrendingFromServerless = async (id, groupName, siteName, topicNam
7575
}
7676
}
7777

78-
// 掘金热门
78+
// 掘金热门 https://juejin.cn/frontend
7979
const getJueJinHot = async (id, groupName, siteName, topicName, topicUrl, webHook, app) => {
8080
try {
8181
const pageSize = app.config.articleSubscription.pageSize
8282
const params = {
8383
id_type: 2,
84-
sort_type: 3,
84+
sort_type: 200,
8585
cate_id: topicUrl,
8686
cursor: "0",
8787
limit: pageSize
@@ -99,6 +99,47 @@ const getJueJinHot = async (id, groupName, siteName, topicName, topicUrl, webHoo
9999
}
100100
}
101101

102+
// https://dev.to/t/architecture/top/week
103+
const getDevArchitectureHot = async (id, groupName, siteName, topicName, topicUrl, webHook, app) => {
104+
try {
105+
const pageSize = app.config.articleSubscription.pageSize
106+
const res = await axios.get(`https://dev.to/search/feed_content?per_page=5&page=0&tag=architecture&sort_by=public_reactions_count&sort_direction=desc&tag_names%5B%5D=architecture&approved=&class_name=Article&published_at%5Bgte%5D=2024-11-10T02%3A43%3A45Z`, { timeout })
107+
let msg = `## DEV Architecture 每周 Top5\n\n`
108+
109+
const data = res?.data?.result || []
110+
for (let i = 0; i < pageSize; i++) {
111+
msg += `${ i + 1 }、[${ data[i].title }](https://dev.to${ data[i].path })\n\n`
112+
}
113+
msg += `[点击查看更多内容](https://dev.to/t/architecture/top/week)`
114+
sendArticleMsg('DEV Architecture 每周 Top5', msg, webHook)
115+
logFunc(app, id, groupName, siteName, topicName, '成功')
116+
} catch (err) {
117+
logFunc(app, id, groupName, siteName, topicName, `失败`, `${ JSON.stringify(err) }`)
118+
}
119+
}
120+
121+
// https://react.statuscode.com/latest
122+
const getReactStatusHot = async (id, groupName, siteName, topicName, topicUrl, webHook, app) => {
123+
try {
124+
const pageSize = app.config.articleSubscription.pageSize
125+
const { data } = await axios.get(`https://react.statuscode.com/latest`, { timeout })
126+
let msg = `## React Status\n\n`
127+
128+
const $ = cheerio.load(data)
129+
const items = $('table.el-item').find('span.mainlink')
130+
for (let i = 0; i < pageSize; i++) {
131+
const name = items.eq(i).text()
132+
const url = items.eq(i).find('a').attr('href')
133+
msg += `${ i + 1 }、[${ name }](${ url })\n\n`
134+
}
135+
msg += `[点击查看更多内容](https://react.statuscode.com/latest)`
136+
sendArticleMsg('React Status', msg, webHook)
137+
logFunc(app, id, groupName, siteName, topicName, '成功')
138+
} catch (err) {
139+
logFunc(app, id, groupName, siteName, topicName, `失败`, `Github 网络不佳 ${ JSON.stringify(err) }`)
140+
}
141+
}
142+
102143
// 自定义消息
103144
const customMessage = async (id, groupName, siteName, messageTitle, message, isAtAll, webHook, app) => {
104145
try {
@@ -129,5 +170,7 @@ module.exports = {
129170
getGithubTrendingFromJueJin,
130171
getGithubTrendingFromServerless,
131172
getJueJinHot,
173+
getDevArchitectureHot,
174+
getReactStatusHot,
132175
customMessage
133176
}

app/web/pages/articleSubscription/components/ChooseSendTime/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class ChooseSendTime extends React.Component<IProps, IState> {
5050
return (
5151
<div className="send-time">
5252
<Select style={{ width: 120 }} value={sendType} onChange={(val) => { this.handleChange('sendType', val) }}>
53+
<Option value={SUBSCRIPTIONSENDTYPE.MONDAY}>{SUBSCRIPTIONSENDTYPECN[SUBSCRIPTIONSENDTYPE.MONDAY]}</Option>
5354
<Option value={SUBSCRIPTIONSENDTYPE.FRIDAY}>{SUBSCRIPTIONSENDTYPECN[SUBSCRIPTIONSENDTYPE.FRIDAY]}</Option>
5455
<Option value={SUBSCRIPTIONSENDTYPE.WORKDAY}>{SUBSCRIPTIONSENDTYPECN[SUBSCRIPTIONSENDTYPE.WORKDAY]}</Option>
5556
<Option value={SUBSCRIPTIONSENDTYPE.EVERYDAY}>{SUBSCRIPTIONSENDTYPECN[SUBSCRIPTIONSENDTYPE.EVERYDAY]}</Option>

app/web/pages/articleSubscription/components/SubscriptionModal/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ const SubscriptionModal = (props: any) => {
8282
cron = `0 ${ minute } ${ hour } ? * *`
8383
} else if (type === SUBSCRIPTIONSENDTYPE.FRIDAY) {
8484
cron = `0 ${ minute } ${ hour } ? * FRI`
85+
} else if (type === SUBSCRIPTIONSENDTYPE.MONDAY) {
86+
cron = `0 ${ minute } ${ hour } ? * MON`
8587
}
8688
return cron
8789
}

app/web/pages/articleSubscription/consts/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ export enum SUBSCRIPTIONSTATUS {
88
export enum SUBSCRIPTIONSENDTYPE {
99
FRIDAY = 0,
1010
WORKDAY = 1,
11-
EVERYDAY = 2
11+
EVERYDAY = 2,
12+
MONDAY = 3,
1213
}
1314

1415
// 订阅的推送时间方式 1 周一至周五 2 每天
1516
export const SUBSCRIPTIONSENDTYPECN = {
17+
[SUBSCRIPTIONSENDTYPE.MONDAY]: '每周一',
1618
[SUBSCRIPTIONSENDTYPE.FRIDAY]: '每周五',
1719
[SUBSCRIPTIONSENDTYPE.WORKDAY]: '周一至周五',
1820
[SUBSCRIPTIONSENDTYPE.EVERYDAY]: '每天'

sql/doraemon.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ INSERT INTO article_topic (siteName, topicName, topicUrl, sort) VALUES ('Github'
8383
INSERT INTO article_topic (siteName, topicName, topicUrl, sort) VALUES ('掘金', '前端', '6809637767543259144', 5);
8484
INSERT INTO article_topic (siteName, topicName, topicUrl, sort) VALUES ('掘金', '后端', '6809637769959178254', 6);
8585

86+
INSERT INTO article_topic (siteName, topicName, topicUrl, sort) VALUES ('英文', 'DEV Architecture', '', 7);
87+
INSERT INTO article_topic (siteName, topicName, topicUrl, sort) VALUES ('英文', 'React Status', '', 8);
88+
8689
-- ----------------------------
8790
-- Table structure for config_management
8891
-- ----------------------------

0 commit comments

Comments
 (0)