From c5f8dd19c6c22342a55622bbb6bb71a9b628a70a Mon Sep 17 00:00:00 2001 From: DIYgod Date: Mon, 21 Dec 2020 20:21:14 +0800 Subject: [PATCH] feat: dida365, close #6486 --- lib/config.js | 4 ++ lib/router.js | 3 ++ lib/routes/dida365/habit-checkins.js | 69 ++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 lib/routes/dida365/habit-checkins.js diff --git a/lib/config.js b/lib/config.js index 5bc4afe63c..3b6c65431c 100644 --- a/lib/config.js +++ b/lib/config.js @@ -177,6 +177,10 @@ const calculateValue = () => { game4399: { cookie: envs.GAME_4399, }, + dida365: { + username: envs.DIDA365_USERNAME, + password: envs.DIDA365_PASSWORD, + }, }; }; calculateValue(); diff --git a/lib/router.js b/lib/router.js index cbad3a6780..4151d2282b 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3727,4 +3727,7 @@ router.get('/mathunion/fields-medal', require('./routes/mathunion/fields-medal') // ACM router.get('/acm/amturingaward', require('./routes/acm/amturingaward')); +// 滴答清单 +router.get('/dida365/habit/checkins', require('./routes/dida365/habit-checkins')); + module.exports = router; diff --git a/lib/routes/dida365/habit-checkins.js b/lib/routes/dida365/habit-checkins.js new file mode 100644 index 0000000000..7a7e2af647 --- /dev/null +++ b/lib/routes/dida365/habit-checkins.js @@ -0,0 +1,69 @@ +const got = require('@/utils/got'); +const { CookieJar } = require('tough-cookie'); +const config = require('@/config').value; +const logger = require('@/utils/logger'); + +let loginFlag; +const cookieJar = new CookieJar(); + +if (config.dida365.username && config.dida365.password) { + got({ + method: 'post', + url: 'https://api.dida365.com/api/v2/user/signon?wc=true&remember=true', + cookieJar, + json: { + username: config.dida365.username, + password: config.dida365.password, + }, + }) + .then(() => { + loginFlag = true; + logger.info('Dida365 login success.'); + }) + .catch(() => { + logger.error('Dida365 login fail.'); + }); +} + +module.exports = async (ctx) => { + if (loginFlag) { + const habitsListRes = await got.get({ + url: 'https://api.dida365.com/api/v2/habits', + cookieJar, + }); + const habitsList = habitsListRes.data; + + const paramsList = []; + habitsList.forEach((item) => { + paramsList.push(['habitIds', item.id]); + }); + const searchParams = new URLSearchParams(paramsList); + + const checkinsRes = await got.get({ + url: 'https://api.dida365.com/api/v2/habitCheckins', + cookieJar, + searchParams, + }); + const checkins = checkinsRes.data; + let list = []; + for (const habitId in checkins.checkins) { + const info = habitsList.find((item) => item.id === habitId); + list = list.concat( + checkins.checkins[habitId].map((checkin, index) => ({ + title: `第${index + 1}天${info.name}打卡`, + pubDate: checkin.checkinTime, + link: `https://dida365.com/webapp/#q/all/habit/${checkin.habitId}`, + guid: checkin.id, + })) + ); + } + + ctx.state.data = { + title: '滴答清单打卡', + link: 'https://dida365.com/webapp/#q/all/habit/', + item: list, + }; + } else { + throw Error('Login required'); + } +};