Files
RSSHub/lib/v2/leetcode/dailyquestion-solution-cn.js
T
Tony 6d901cb7dc style(eslint): add eslint-unicorn (#14257)
* style: add eslint-unicorn

* style: fix unicorn/no-useless-spread

* style: fix unicorn/no-useless-promise-resolve-reject

* style: fix unicorn/no-for-loop

* fix: codeql bad HTML filtering regexp

* fix: codeql incomplete replace

* fix: unicorn/no-abusive-eslint-disable

* style: fix unicorn/no-new-array

* style: fix unicorn/no-typeof-undefined

* style: fix unicorn/no-zero-fractions

* style: fix unicorn/no-empty-file

* style: fix unicorn/prefer-date-now

* revert: auto fix unicorn/prefer-switch on lib/v2/kuaidi100/utils.js

* style: fix unicorn/prefer-array-find

* style: fix unicorn/prefer-array-flat

* style: fix unicorn/prefer-array-flat-map

* style: fix unicorn/prefer-at

* style: fix unicorn/prefer-string-starts-ends-with

* style: fix unicorn/prefer-includes

* fix: codeql URL substring sanitization

* style: fix unicorn/prefer-optional-catch-binding

* style: fix unicorn/catch-error-name

* style: fix unicorn/escape-case

* style: fix unicorn/prefer-native-coercion-functions

* style: fix unicorn/prefer-regexp-test

* style: fix unicorn/require-array-join-separator

* style: fix unicorn/prefer-math-trunc

* style: fix unicorn/prefer-negative-index

* style: fix unicorn/prefer-dom-node-dataset

* style: fix unicorn/prefer-dom-node-text-content

* style: fix unicorn/prefer-query-selector

* style: fix unicorn/no-array-for-each

* style: fix unicorn/no-negated-condition

* style: fix unicorn/prefer-add-event-listener

* style: fix unicorn/import-style

* style: fix prefer-regex-literals

* style: disable unicorn/no-useless-switch-case

* style: disable unicorn/text-encoding-identifier-case

* style: fix unicorn/prefer-set-has

* style: fix unicorn/prefer-spread

* revert: auto fix on lib/routes/universities/ynnu/edu/base64.js

* style: fix unicorn/no-useless-undefined

* style: fix unicorn/no-array-push-push

* style: fix unicorn/no-useless-undefined again

* style: fix unicorn/no-lonely-if

* style: fix unicorn/prefer-reflect-apply

* style: fix unicorn/switch-case-braces

* style: fix unicorn/prefer-switch

* style: fix unicorn/prefer-array-some

* fix: deepscan UNUSED_VAR_ASSIGN

* style: fix unicorn/prefer-ternary

* fix: follow-up of unicorn/prefer-ternary

* revert: auto fix of unicorn/prefer-string-slice for substring()

* style: disable unicorn/prefer-string-slice

fix: auto fix slice over deprecated substr

* style: fix unicorn/throw-new-error

* style: fix unicorn/filename-case

* test: fix dateParser renaming

* style: fix unicorn/better-regex

* style: fix unicorn/prefer-string-replace-all

* fix(deps): add sanitize-html

* style: fix no-prototype-builtins

* style: fix unicorn/consistent-destructuring

* style: fix unicorn/consistent-function-scoping

* style: fix unicorn/prefer-regexp-test

* style: fix unicorn/prefer-logical-operator-over-ternary

* style: fix unicorn/no-array-callback-reference

* style: add prefer-object-has-own

* style: warn unicorn/no-empty-file

* style: fix unicorn/prefer-number-properties

* style: fix no-useless-undefined again

* style: fix unicorn/numeric-separators-style

* style: disable unicorn/no-array-callback-reference

false postive with cheerio
2024-01-18 20:43:40 +08:00

187 lines
6.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const got = require('@/utils/got');
const md = require('markdown-it')({
html: true,
breaks: true,
});
const { parseDate } = require('@/utils/parse-date');
const timezone = require('@/utils/timezone');
module.exports = async (ctx) => {
const baseurl = `https://leetcode.cn`;
const url = `${baseurl}/graphql/`;
const headers = {
'content-type': 'application/json',
};
const emoji = {
Medium: '🟡',
Easy: '🟢',
Hard: '🔴',
};
// 获取每日一题
const data = (
await got({
method: 'post',
url,
json: {
operationName: 'questionOfToday',
query: `query questionOfToday {
todayRecord {
date
userStatus
question {
questionId
frontendQuestionId: questionFrontendId
difficulty
title
titleCn: translatedTitle
titleSlug
}
}
}`,
variables: {},
},
headers,
})
).data.data;
const questionTitle = data.todayRecord[0].question.titleSlug;
const questionUrl = `${baseurl}/problems/${questionTitle}/`;
// 获取题目内容
const question = (
await got({
method: 'post',
url,
json: {
operationName: 'questionData',
query: `query questionData($titleSlug: String!) {
question(titleSlug: $titleSlug) {
questionId
questionFrontendId
categoryTitle
boundTopicId
title
titleSlug
content
translatedTitle
translatedContent
isPaidOnly
difficulty
likes
}
}`,
variables: {
titleSlug: questionTitle,
},
},
headers,
})
).data.data.question;
const diffEmoji = emoji[question.difficulty] || '';
// 获取题解(点赞前3)
const articles = (
await got({
method: 'post',
url,
json: {
operationName: 'questionSolutionArticles',
query: `query questionSolutionArticles($questionSlug: String!, $skip: Int, $first: Int, $orderBy: SolutionArticleOrderBy, $userInput: String, $tagSlugs: [String!]) {
questionSolutionArticles(questionSlug: $questionSlug, skip: $skip, first: $first, orderBy: $orderBy, userInput: $userInput, tagSlugs: $tagSlugs) {
totalNum
edges {
node {
...solutionArticle
__typename
}
__typename
}
__typename
}
}
fragment solutionArticle on SolutionArticleNode {
uuid
title
slug
createdAt
thumbnail
author {
username
}
summary
}`,
variables: {
questionSlug: questionTitle,
first: 3,
skip: 0,
orderBy: 'MOST_UPVOTE',
},
},
headers,
})
).data.data.questionSolutionArticles.edges;
// 获取题解内容
const articleContent = (
await Promise.all(
articles.map((art) =>
got({
method: 'post',
url,
json: {
operationName: 'solutionDetailArticle',
query: `query solutionDetailArticle($slug: String!, $orderBy: SolutionArticleOrderBy!) {
solutionArticle(slug: $slug, orderBy: $orderBy) {
...solutionArticle
content
question {
questionTitleSlug
__typename
}
}
}
fragment solutionArticle on SolutionArticleNode {
uuid
title
slug
createdAt
thumbnail
author {
username
}
summary
}`,
variables: {
slug: art.node.slug,
orderBy: 'DEFAULT',
},
},
headers,
})
)
)
).map((v) => v.data.data.solutionArticle);
const handleText = (s) => {
// 处理多语言代码展示问题
s = s.replaceAll(/(```)([\d#+A-Za-z-]+)\s*?(\[.*?])?\n/g, '\r\n###$2\r\n$1$2\r\n');
return s;
};
ctx.state.data = {
title: 'LeetCode 每日一题题解',
description: 'LeetCode 每日一题题解',
link: questionUrl,
item: [
{
title: `每日一题-${question.translatedTitle}${diffEmoji}`,
link: questionUrl,
description: question.translatedContent,
pubDate: timezone(parseDate(data.todayRecord[0].date), +8),
},
...articleContent.map((art, i) => ({
title: art.title,
link: `${questionUrl}/solution/${art.slug}`,
description: md.render(handleText(art.content)),
pubDate: timezone(parseDate(articles[i].node.createdAt), +8),
author: art.author.username,
})),
],
};
};