mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-17 20:28:21 +08:00
fix: leetcode articles (#6319)
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
const got = require('@/utils/got');
|
||||
const cheerio = require('cheerio');
|
||||
const url = require('url');
|
||||
const showdown = require('showdown');
|
||||
|
||||
const host = 'https://leetcode.com';
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const link = 'https://leetcode.com/articles/';
|
||||
const link = url.resolve(host, '/articles');
|
||||
const response = await got.get(link);
|
||||
const $ = cheerio.load(response.data);
|
||||
|
||||
@@ -26,16 +27,33 @@ module.exports = async (ctx) => {
|
||||
const out = await Promise.all(
|
||||
list.map(async (info) => {
|
||||
const itemUrl = url.resolve(host, info.link);
|
||||
const titelSlug = info.link.split('/')[2];
|
||||
|
||||
const cache = await ctx.cache.get(itemUrl);
|
||||
if (cache) {
|
||||
return Promise.resolve(JSON.parse(cache));
|
||||
}
|
||||
|
||||
const response = await got.get(itemUrl);
|
||||
const $ = cheerio.load(response.data);
|
||||
const questionData = await got
|
||||
.post(url.resolve(host, '/graphql'), {
|
||||
json: { operationName: 'questionData', variables: { titleSlug: titelSlug }, query: 'query questionData($titleSlug: String!) {\n question(titleSlug: $titleSlug) {\n content\n }\n}\n' },
|
||||
})
|
||||
.json();
|
||||
|
||||
const description = $('#question-preview').html().trim() + $('.article-body').html().trim();
|
||||
const questionNote = await got
|
||||
.post(url.resolve(host, '/graphql'), {
|
||||
json: {
|
||||
operationName: 'QuestionNote',
|
||||
variables: { titleSlug: titelSlug },
|
||||
query: 'query QuestionNote($titleSlug: String!) {\n question(titleSlug: $titleSlug) {\n solution {\n content\n }\n }\n}\n',
|
||||
},
|
||||
})
|
||||
.json();
|
||||
|
||||
const converter = new showdown.Converter();
|
||||
const solution = converter.makeHtml(questionNote.data.question.solution.content);
|
||||
|
||||
const description = questionData.data.question.content.trim() + solution;
|
||||
|
||||
const single = {
|
||||
title: info.title,
|
||||
|
||||
Reference in New Issue
Block a user