Last active
March 8, 2018 08:21
-
-
Save lefloh/68603ec8b7bcefdbc1acca4c20b81ff8 to your computer and use it in GitHub Desktop.
Parse Apples Security Framework Result Codes
This file contains 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
const fetch = require('node-fetch') | |
const cheerio = require('cheerio') | |
const baseUri = 'https://developer.apple.com' | |
const sourceUri = `${baseUri}/documentation/security/1542001-security_framework_result_codes?language=objc` | |
const printRow = (left, right) => { | |
console.info(`| ${left.padEnd(40)} | ${right.padStart(6)} |`) | |
} | |
const dumpResult = result => { | |
console.info('# Apple Security Framework Result Codes\n') | |
console.info(`Source: [${sourceUri}](${sourceUri})\n`) | |
Object.keys(result).forEach(section => { | |
const errorCodes = result[section] | |
// filter empty sections | |
if (errorCodes.find(e => e.code == null) != null) { | |
return | |
} | |
console.info(`## ${section}\n`) | |
printRow('Constant', 'Code') | |
printRow(':---', '---:') | |
errorCodes.forEach(errorCode => { | |
if (errorCode.code == null) { | |
return | |
} | |
printRow(errorCode.constant, errorCode.code) | |
}) | |
console.info('\n') | |
}) | |
} | |
const fetchSubpage = async (title, path) => { | |
try { | |
const response = await fetch(`${baseUri}${path}`) | |
const data = await response.text() | |
const $ = cheerio.load(data) | |
const text = $('.declaration pre').text().trim() | |
const tokens = text.split(' = ') | |
return { | |
constant: tokens[0], | |
code: tokens[1], | |
} | |
} catch (err) { | |
console.error(err) | |
} | |
} | |
const fetchErrorCodes = async () => { | |
const result = {} | |
try { | |
const response = await fetch(sourceUri) | |
const data = await response.text() | |
const $ = cheerio.load(data) | |
for (let section of $('#topics section').toArray()) { | |
const title = $('h3', section).text() | |
const requests = [] | |
for (let a of $('a', section).toArray()) { | |
const path = $(a).attr('href') | |
requests.push(fetchSubpage(title, path)) | |
} | |
result[title] = await Promise.all(requests) | |
} | |
dumpResult(result) | |
} catch (err) { | |
console.error(err) | |
} | |
} | |
fetchErrorCodes() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment