diff --git a/docs/en/programming.md b/docs/en/programming.md index 7dbddce89d..dfcc45b0f8 100644 --- a/docs/en/programming.md +++ b/docs/en/programming.md @@ -307,7 +307,7 @@ Subscribe to the updates (threads and submission) from a paritcular Hacker News ### Daily Question - + ## Linux Patchwork diff --git a/docs/programming.md b/docs/programming.md index 6a7ce49e50..e512d86342 100644 --- a/docs/programming.md +++ b/docs/programming.md @@ -449,7 +449,7 @@ GitHub 官方也提供了一些 RSS: ### 每日一题 - + ## LinkedKeeper diff --git a/lib/v2/leetcode/dailyquestion-cn.js b/lib/v2/leetcode/dailyquestion-cn.js new file mode 100644 index 0000000000..67c83df411 --- /dev/null +++ b/lib/v2/leetcode/dailyquestion-cn.js @@ -0,0 +1,115 @@ +const got = require('@/utils/got'); +const { art } = require('@/utils/render'); +const path = require('path'); + +const host = 'https://leetcode.cn'; + +module.exports = async (ctx) => { + const question = { + date: '', + link: '', + titleSlug: '', + content: '', + frontedId: '', + difficulty: '', + tags: '', + }; + const url = host + '/graphql'; + const dailyQuestionPayload = { + query: `query questionOfToday { + todayRecord { + date + question { + frontendQuestionId: questionFrontendId + titleSlug + } + } + } `, + variables: {}, + }; + const dailyQuestionResponse = await got({ + method: 'post', + url, + headers: { + 'content-type': 'application/json', + }, + body: JSON.stringify(dailyQuestionPayload), + }); + const data = dailyQuestionResponse.data.data.todayRecord[0]; + question.date = data.date; + question.titleSlug = data.question.titleSlug; + question.link = host + '/problems/' + question.titleSlug; + + const detailsPayload = { + operationName: 'questionData', + query: `query questionData($titleSlug: String!) { + question(titleSlug: $titleSlug) { + questionId + questionFrontendId + title + titleSlug + content + translatedTitle + translatedContent + difficulty + topicTags { + name + slug + translatedName + __typename + } + __typename + } + }`, + variables: { + titleSlug: question.titleSlug, + }, + }; + const detailsResponse = await got({ + method: 'post', + url, + headers: { + 'content-type': 'application/json', + }, + body: JSON.stringify(detailsPayload), + }); + const emoji = { + Medium: '🟡', + Easy: '🟢', + Hard: '🔴', + }; + + const details = detailsResponse.data.data.question; + question.content = details.translatedContent; + question.frontedId = details.questionFrontendId; + question.difficulty = emoji[details.difficulty]; + + let topicTags = details.topicTags; + topicTags = topicTags.map((item) => { + let slug = '#' + item.slug; + slug = slug.replaceAll('-', '_'); + return slug; + }); + question.tags = topicTags.join(' '); + + const rssData = { + title: question.frontedId + '.' + question.titleSlug, + description: art(path.join(__dirname, 'templates/question-description.art'), { + question, + }), + link: question.link, + }; + + ctx.state.data = { + title: 'LeetCode 每日一题', + link: 'https://leetcode.cn', + description: 'Leetcode 每日一题', + item: [ + { + title: rssData.title, + description: rssData.description + question.content, + link: rssData.link, + }, + ], + }; +}; diff --git a/lib/v2/leetcode/maintainer.js b/lib/v2/leetcode/maintainer.js index 6162804426..127d69bf0f 100644 --- a/lib/v2/leetcode/maintainer.js +++ b/lib/v2/leetcode/maintainer.js @@ -1,3 +1,4 @@ module.exports = { '/dailyquestion/en': ['NavePnow'], + '/dailyquestion/cn': ['NavePnow'], }; diff --git a/lib/v2/leetcode/router.js b/lib/v2/leetcode/router.js index 25ef26e010..011a86178d 100644 --- a/lib/v2/leetcode/router.js +++ b/lib/v2/leetcode/router.js @@ -1,3 +1,4 @@ module.exports = function (router) { router.get('/dailyquestion/en', require('./dailyquestion-en')); + router.get('/dailyquestion/cn', require('./dailyquestion-cn')); };