fix: leetcode articles (#6319)

This commit is contained in:
Joey Chai
2020-12-04 23:53:13 +08:00
committed by GitHub
parent 6d162fd3c0
commit 052652f2f4
3 changed files with 65 additions and 5 deletions

View File

@@ -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,