添加CSDN猿如意linux-command接口爬取
This commit is contained in:
		
							
								
								
									
										2
									
								
								linux-command/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								linux-command/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1,2 @@
 | 
				
			|||||||
 | 
					commands/*
 | 
				
			||||||
 | 
					list.json
 | 
				
			||||||
							
								
								
									
										1
									
								
								linux-command/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								linux-command/README.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
				
			|||||||
 | 
					Api是从 CSDN猿如意 的插件中扒出来的
 | 
				
			||||||
							
								
								
									
										55
									
								
								linux-command/index.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								linux-command/index.js
									
									
									
									
									
										Normal 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()
 | 
				
			||||||
		Reference in New Issue
	
	Block a user