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

View File

@@ -1073,7 +1073,7 @@ router.get('/hackernews/:section/:type?', require('./routes/hackernews/story'));
// LeetCode
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
router.get('/segmentfault/channel/:name', require('./routes/segmentfault/channel'));

View File

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

View File

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