Files
RSSHub/lib/v2/leetcode/dailyquestion-solution-en.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

217 lines
7.4 KiB
JavaScript

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');
const path = require('path');
module.exports = async (ctx) => {
const baseurl = `https://leetcode.com`;
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 {
activeDailyCodingChallengeQuestion {
date
link
question {
frontendQuestionId: questionFrontendId
titleSlug
}
}
}`,
variables: {},
},
headers,
})
).data.data;
const questionTitle = data.activeDailyCodingChallengeQuestion.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] || '';
// 获取题解(en网站仅一个题解)
const article = (
await got({
method: 'post',
url,
json: {
operationName: 'QuestionNote',
query: `query QuestionNote($titleSlug: String!) {
question(titleSlug: $titleSlug) {
questionId
article
solution {
id
content
contentTypeId
canSeeDetail
paidOnly
hasVideoSolution
paidOnlyVideo
rating {
id
count
average
userRating {
score
}
}
}
}
}`,
variables: {
titleSlug: questionTitle,
},
},
headers,
})
).data.data.question.solution;
if (article.content === null) {
article.content = 'Sorry, the solution of this question may be locked.';
}
// 图片处理
const parsePngSlide = async (s) => {
const pattern = /!\?!(.+)!\?!/;
if (!pattern.test(s)) {
return s;
}
const matched = s.match(new RegExp(pattern, 'g'));
const fn = async (m) => {
const relaurl = m.match(pattern)[1].split(':')[0];
const fullurl = path.resolve('/' + questionUrl + 'solution/', relaurl).slice(1);
const pngList = (
await got({
url: fullurl,
method: 'get',
headers,
})
).data.timeline;
return pngList.map((v) => `![pic](${path.resolve(`/problems/${questionTitle}/solution/`, v.image)})`).join('\n');
};
const strs = await Promise.all(matched.map((v) => fn(v)));
for (const [i, element] of matched.entries()) {
s = s.replace(element, strs[i]);
}
return s;
};
// iframe代码框处理
const parseIframe = async (s) => {
const pattern = /<iframe.*? src=".*?playground\/(.*?)\/shared".*<\/iframe>/;
if (!pattern.test(s)) {
return s;
}
const matched = s.match(new RegExp(pattern, 'g'));
const fn = async (m) => {
const uuid = m.match(pattern)[1];
const code = (
await got({
method: 'post',
url,
json: {
operationName: 'fetchPlayground',
query: `query fetchPlayground {
playground(uuid: "${uuid}") {
testcaseInput
name
isUserOwner
isLive
showRunCode
showOpenInPlayground
selectedLangSlug
isShared
__typename
}
allPlaygroundCodes(uuid: "${uuid}") {
code
langSlug
__typename
}
}`,
variables: {},
},
headers,
})
).data.data.allPlaygroundCodes;
return code.map((c) => `###${c.langSlug}\n\r \`\`\`${c.langSlug}\n ${c.code}\n\`\`\``).join('\n\r');
};
const strs = await Promise.all(matched.map((v) => fn(v)));
for (const [i, element] of matched.entries()) {
s = s.replace(element, strs[i]);
}
return s;
};
const handleText = async (s) => {
// 处理代码iframe嵌入问题
s = await parseIframe(s);
// 处理图片展示问题
s = await parsePngSlide(s);
return s;
};
article.content = await handleText(article.content);
ctx.state.data = {
title: 'LeetCode DailyQuestion Solution',
description: 'LeetCode DailyQuestion Solution',
link: questionUrl,
item: [
{
title: `DailyQuestion-${question.title}${diffEmoji}`,
link: questionUrl,
description: question.content,
pubDate: timezone(parseDate(data.activeDailyCodingChallengeQuestion.date), +8),
},
{
title: `Solution-${question.title}`,
link: `${questionUrl}solution/`,
description: md.render(article.content),
pubDate: timezone(parseDate(data.activeDailyCodingChallengeQuestion.date), +8),
author: 'leetcode',
},
],
};
};