-
Notifications
You must be signed in to change notification settings - Fork 4
Callmeboy #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Callmeboy #3
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f14be78
add:自动拉取题解&自动生成json
zhenbushidashen 8a8ebcd
mod:git忽略spider目录
zhenbushidashen c533170
mod:修改constants配置
zhenbushidashen 6c15e9c
mod: spider
zhenbushidashen 1188cc9
del: 删除spider
zhenbushidashen ccd998d
mod:修改review建议
zhenbushidashen 77d6457
add:页面接入抓取数据
zhenbushidashen a6d05c3
mod:抽取常量&去除静态类
zhenbushidashen 640ae0a
add: 匹配前置知识和关键点
zhenbushidashen e97404a
mod:tag标签固定颜色
zhenbushidashen 7befae0
mod:修改Panel头部样式
zhenbushidashen 6627306
mod:抽离请求
zhenbushidashen 37aeabc
mod:修复pr建议
zhenbushidashen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
|
||
# production | ||
/build | ||
/spider/** | ||
|
||
# misc | ||
.DS_Store | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
|
||
const request = require('request') | ||
const Iconv = require('iconv-lite') | ||
const cheerio = require('cheerio') | ||
|
||
const Logger = require('./logger') | ||
const Utils = require('./utils') | ||
const { PROBLEMS_URL, QUESTION_DOM_SELECTOR, BASE_MARKDWON_DOWNLOAD_URL, ENGLISH_MARKDOWN_SIGN } = require('./constants') | ||
|
||
module.exports = LeetCodeProvider = { | ||
|
||
|
||
|
||
getProblemsTitle() { | ||
|
||
return Utils.httpGet(PROBLEMS_URL) | ||
.then((body)=> { | ||
let titles = [] | ||
let sHtml = Iconv.decode(body, 'utf-8').toString() | ||
cheerio.load(sHtml)(QUESTION_DOM_SELECTOR).each((idx, ele) => titles.push(ele.attribs['title'])) | ||
Logger.success('获取问题列表成功') | ||
|
||
return titles.filter(name => !name.endsWith(ENGLISH_MARKDOWN_SIGN)) | ||
}) | ||
.catch(error => { | ||
Logger.error('获取问题列表失败', error) | ||
}) | ||
}, | ||
|
||
|
||
|
||
|
||
getProblemDetail(problemNameWithExt) { | ||
|
||
return Utils.httpGet(`${BASE_MARKDWON_DOWNLOAD_URL}${problemNameWithExt}`) | ||
.then(body => { | ||
|
||
let markdown = Iconv.decode(body, 'utf-8').toString() | ||
Logger.success(`抓取问题 "${problemNameWithExt}" 成功!`) | ||
return markdown | ||
}) | ||
.catch(error => { | ||
Logger.error(`抓取问题 "${problemNameWithExt}" 失败`, error) | ||
}) | ||
} | ||
|
||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
const log4js = require("log4js"); | ||
|
||
const logger = log4js.getLogger() | ||
|
||
logger.level = 'debug' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 建议根据不同环境切换日志级别 |
||
|
||
logger.category = 'LeetCode' | ||
|
||
const Logger = { | ||
|
||
success(...args) { | ||
logger.info(...args) | ||
}, | ||
error(...args) { | ||
logger.error(...args) | ||
} | ||
|
||
} | ||
|
||
module.exports = Logger |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
|
||
|
||
|
||
module.exports = { | ||
|
||
/** | ||
* 需解析的语言类型 | ||
*/ | ||
SUPPORT_LANGUAGE: [ | ||
'java', | ||
'js', | ||
'cpp', | ||
'py' | ||
], | ||
|
||
/** | ||
* 是否启用强制更新 | ||
* 如开启,会跳过读取本地缓存,拉取最新文件 | ||
*/ | ||
IS_FORCE_UPDATE_MODE: true, | ||
|
||
/** | ||
* 请求处理频率 ms | ||
*/ | ||
REQUEST_RATE: 300, | ||
|
||
/** | ||
* markdown输出目录 | ||
*/ | ||
RAW_MARKDOWN_OUTPUT_DIR: 'spider/raw-markdown', | ||
|
||
/** | ||
* 转化后的json输出目录 | ||
*/ | ||
DB_JSON_OUTPUT_DIR: 'spider/yield-db-json', | ||
|
||
/** | ||
* 获取问题列表地址 | ||
*/ | ||
PROBLEMS_URL: 'https://github.com/azl397985856/leetcode/tree/master/problems', | ||
|
||
/** | ||
* 抓取页面问题内容的dom元素选择器 | ||
*/ | ||
QUESTION_DOM_SELECTOR: '.js-navigation-item .content .js-navigation-open', | ||
|
||
/** | ||
* markdwon下载地址 | ||
*/ | ||
BASE_MARKDWON_DOWNLOAD_URL: 'https://raw.githubusercontent.com/azl397985856/leetcode/master/problems/', | ||
|
||
/** | ||
* 过滤英文文档末尾标识 | ||
*/ | ||
ENGLISH_MARKDOWN_SIGN: '.en.md' | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
const LeetCodeProvider = require('./leetcodeprovider') | ||
|
||
const Logger = require('./logger') | ||
|
||
const Utils = require('./utils') | ||
|
||
|
||
const { RAW_MARKDOWN_OUTPUT_DIR, REQUEST_RATE, IS_FORCE_UPDATE_MODE } = require('./constants') | ||
|
||
|
||
/** | ||
* 当前请求问题索引 | ||
*/ | ||
let requsetNumber = 0 | ||
|
||
|
||
Utils.mkdirSync(RAW_MARKDOWN_OUTPUT_DIR) | ||
|
||
const getProblemDetail = (questionsName, requsetNumber) => { | ||
|
||
const cachedFilesName = Utils.getDirsFileNameSync(RAW_MARKDOWN_OUTPUT_DIR) | ||
|
||
if (!IS_FORCE_UPDATE_MODE && cachedFilesName.includes(questionsName[requsetNumber])) { | ||
|
||
Logger.success(`${questionsName[requsetNumber]}命中缓存, 跳过。。。`) | ||
|
||
requsetNumber++ | ||
|
||
|
||
getProblemDetail(questionsName, requsetNumber) | ||
|
||
} | ||
else { | ||
|
||
questionsName[requsetNumber] && LeetCodeProvider.getProblemDetail(questionsName[requsetNumber]).then(markDown => { | ||
if (markDown) { | ||
|
||
Logger.success(`问题: "${questionsName[requsetNumber]}" | 结果: ${JSON.stringify(markDown)}`) | ||
|
||
Utils.writeFileSync(RAW_MARKDOWN_OUTPUT_DIR, questionsName[requsetNumber], markDown) | ||
|
||
requsetNumber++ | ||
} else { | ||
Logger.error(`获取${questionsName[requsetNumber]} markdown 失败!`) | ||
} | ||
|
||
}).catch(Logger.error).then(() => { | ||
|
||
setTimeout(() => { | ||
|
||
questionsName[requsetNumber] && getProblemDetail(questionsName, requsetNumber) | ||
|
||
}, REQUEST_RATE) | ||
}) | ||
} | ||
|
||
} | ||
|
||
|
||
LeetCodeProvider.getProblemsTitle().then(questionsName => { | ||
|
||
getProblemDetail(questionsName, requsetNumber) | ||
|
||
}) | ||
|
||
|
||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.