fix: 力扣 (#2340)

This commit is contained in:
Nathan
2019-06-08 13:49:48 +08:00
committed by DIYgod
parent d461cf8308
commit 983bb09c57
4 changed files with 16 additions and 47 deletions

View File

@@ -152,7 +152,7 @@ GitHub 官方也提供了一些 RSS:
### 打卡 ### 打卡
<Route author="NathanDai" example="/leetcode/submission/us/nathandai" path="/leetcode/submission/:country/:user" :paramsDesc="['国家 country, 中国(cn)和美国(us)', '用户名 user, 可在LeetCode用户主页的 URL 中找到']"/> <Route author="NathanDai" example="/leetcode/submission/us/nathandai" path="/leetcode/submission/us/:user" :paramsDesc="['现在只支持国际版的leetcode', '用户名 user, 可在LeetCode用户主页的 URL 中找到']"/>
## LinkedKeeper ## LinkedKeeper

View File

@@ -1073,7 +1073,7 @@ router.get('/hackernews/:section/:type?', require('./routes/hackernews/story'));
// LeetCode // LeetCode
router.get('/leetcode/articles', require('./routes/leetcode/articles')); router.get('/leetcode/articles', require('./routes/leetcode/articles'));
router.get('/leetcode/submission/:country/:user', require('./routes/leetcode/submission')); router.get('/leetcode/submission/us/:user', require('./routes/leetcode/check-us'));
// segmentfault // segmentfault
router.get('/segmentfault/channel/:name', require('./routes/segmentfault/channel')); router.get('/segmentfault/channel/:name', require('./routes/segmentfault/channel'));

View File

@@ -1,16 +1,9 @@
const got = require('@/utils/got'); const got = require('@/utils/got');
const cheerio = require('cheerio'); const cheerio = require('cheerio');
const util = require('./utils'); const util = require('./utils');
module.exports = async (ctx) => { module.exports = async (ctx) => {
const country = ctx.params.country;
const user = ctx.params.user; const user = ctx.params.user;
let url, state, description; const url = 'https://leetcode.com/';
if (country === 'cn') {
url = 'https://leetcode-cn.com/';
} else {
url = 'https://leetcode.com/';
}
const response = await got({ const response = await got({
method: 'get', method: 'get',
url: url + `${user}`, url: url + `${user}`,
@@ -21,8 +14,8 @@ module.exports = async (ctx) => {
const data = response.data; const data = response.data;
const $ = cheerio.load(data); const $ = cheerio.load(data);
const username = $('div.panel-body') const username = $('div.panel-body')
.find('h4') .find('p')
.text(); .text(); // 用户名
const img = $('div.panel-body') const img = $('div.panel-body')
.find('img') .find('img')
.attr('src'); // 用户的头像 .attr('src'); // 用户的头像
@@ -45,19 +38,13 @@ module.exports = async (ctx) => {
.eq(2) .eq(2)
.find('span') .find('span')
.text(); // 通过率 .text(); // 通过率
if (country === 'cn') { const state = ' Most recent submissions';
state = ' 的刷题动态'; const description = 'Solved Question: ' + solvedQuestion + '<br>Accepted Submission: ' + acceptedSubmission + '<br>Acceptance Rate: ' + acceptanceRate + '<br>' + src;
description = '解决的题目: ' + solvedQuestion + '<br>通过的提交: ' + acceptedSubmission + '<br>通过率: ' + acceptanceRate + '<br>' + src;
} else {
state = ' Most recent submissions';
description = 'Solved Question: ' + solvedQuestion + '<br>Accepted Submission: ' + acceptedSubmission + '<br>Acceptance Rate: ' + acceptanceRate + '<br>' + src;
}
const list = $('ul.list-group') const list = $('ul.list-group')
.eq(-1) .eq(-1)
.children() .children()
.get(); .get();
const result = await util.ProcessFeed(list, country); const result = await util.ProcessFeed(list);
ctx.state.data = { ctx.state.data = {
title: username + state, title: username + state,
description: description, description: description,

View File

@@ -1,12 +1,7 @@
const cheerio = require('cheerio'); const cheerio = require('cheerio');
const ProcessFeed = async (list, country) => { const ProcessFeed = async (list) => {
let host; const host = 'https://leetcode.com';
if (country === 'us') {
host = 'https://leetcode.com';
} else {
host = 'https://leetcode-cn.com';
}
return await Promise.all( return await Promise.all(
list.map(async (item) => { list.map(async (item) => {
const $ = cheerio.load(item); const $ = cheerio.load(item);
@@ -19,7 +14,6 @@ const ProcessFeed = async (list, country) => {
.eq(1) .eq(1)
.text(); .text();
// 还原相对链接为绝对链接 // 还原相对链接为绝对链接
const pubDate = $('span') const pubDate = $('span')
.eq(2) .eq(2)
.text(); .text();
@@ -27,23 +21,12 @@ const ProcessFeed = async (list, country) => {
const itemUrl = host + $(bb).attr('href'); const itemUrl = host + $(bb).attr('href');
let n = 0, let n = 0,
h = 0; h = 0;
let n1, n2, n3, n4, n5, n6; const n1 = pubDate.search(/year/);
if (country === 'us') { const n2 = pubDate.search(/month/);
n1 = pubDate.search(/year/); const n3 = pubDate.search(/week/);
n2 = pubDate.search(/month/); const n4 = pubDate.search(/day/);
n3 = pubDate.search(/week/); const n5 = pubDate.search(/hour/);
n4 = pubDate.search(/day/); const n6 = pubDate.search(/minute/);
n5 = pubDate.search(/hour/);
n6 = pubDate.search(/minute/);
} else {
n1 = pubDate.search(/年/);
n2 = pubDate.search(/月/);
n3 = pubDate.search(/周/);
n4 = pubDate.search(/日/);
n5 = pubDate.search(/小时/);
n6 = pubDate.search(/分钟/);
}
if (n1 !== -1) { if (n1 !== -1) {
n = n + parseInt(pubDate[n1 - 2]) * 365; n = n + parseInt(pubDate[n1 - 2]) * 365;
} }
@@ -76,7 +59,6 @@ const ProcessFeed = async (list, country) => {
}) })
); );
}; };
module.exports = { module.exports = {
ProcessFeed, ProcessFeed,
}; };