1
0
Code Issues Pull Requests Projects Releases Wiki Activity GitHub Gitee

添加CSDN猿如意linux-command接口爬取

This commit is contained in:
程序员小墨 2023-01-30 15:20:57 +08:00
parent 412f39f78e
commit 1eefb07eba
3 changed files with 58 additions and 0 deletions

2
linux-command/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
commands/*
list.json

1
linux-command/README.md Normal file
View File

@ -0,0 +1 @@
Api是从 CSDN猿如意 的插件中扒出来的

55
linux-command/index.js Normal file
View File

@ -0,0 +1,55 @@
const fs = require('fs');
const path = require('path');
const os = require('os');
const { getApiResult } = require('../utils/requestUtils');
const url = {
list: 'https://cdn-static-devbit.csdn.net/devbit-static/plugin/linux-command/dist/data.min.json',
content: (command) => `https://cdn-static-devbit.csdn.net/devbit-static/plugin/linux-command/command/${command}.md`
}
async function get_list() {
let saveFilePath = './list.json'
if (fs.existsSync(saveFilePath) && fs.statSync(saveFilePath).isFile()) {
console.log('File exists')
let list = fs.readFileSync(saveFilePath, 'utf8')
let list_data = JSON.parse(list)
return list_data
} else {
console.log('File not exists')
let list = await getApiResult(url.list)
let list_data = JSON.parse(list)
fs.writeFileSync(saveFilePath, JSON.stringify(list_data, null, 4))
return list_data
}
}
async function get_content_by_command(command) {
// console.log(element)
let saveFilePath = `./commands/${command}.md`
if (fs.existsSync(saveFilePath) && fs.statSync(saveFilePath).isFile()) {
console.log(`[${command}] File exists, skip ...`)
} else {
console.log(`[${command}] File not exists, get content ...`)
let content = await getApiResult(url.content(command))
fs.writeFileSync(saveFilePath, content)
}
}
async function main() {
let list = await get_list()
let commands = Object.keys(list)
// console.log(commands.join(', '))
let folderPath = './commands'
if (!fs.existsSync(folderPath) || !fs.statSync(folderPath).isDirectory()) {
fs.mkdirSync(folderPath, { recursive: true })
}
for (let i = 0; i < commands.length; i++) {
const element = commands[i]
get_content_by_command(element)
}
}
main()