Last active
February 26, 2020 13:13
-
-
Save lspitzner/59186eabc4dfe2e42546d079c6504508 to your computer and use it in GitHub Desktop.
fetch ghc proposal comments
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
# if you run this 5+ times, you will get rate-limited. | |
node fetch-comments.js | |
node process-comments.js > comments_.txt | |
cat comments_.txt | sed 's/#/\\#/g' | pandoc -f markdown -o comments.html | |
# Plus manual embedding into a html with <meta http-equiv="Content-Type" content="text/html; charset=utf-8">. | |
# Or serve with appropriate Content-Type. |
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 https = require('https'); | |
const fs = require('fs'); | |
// const headers = {Accept: 'application/vnd.github.squirrel-girl-preview'}; | |
const options = { | |
hostname: 'api.github.com', | |
port: 443, | |
path: '/repos/ghc-proposals/ghc-proposals/issues/282/comments?per_page=100', | |
method: 'GET', | |
headers: {'Accept': 'application/vnd.github.v3.raw+json', 'User-Agent': 'some-garbage-script-dont-blockme' } | |
}; | |
var comments = []; | |
const getPage = function(n) { | |
const pageOptions = JSON.parse(JSON.stringify(options)); | |
pageOptions.path = options.path + "&page=" + n; | |
console.error(pageOptions); | |
https.get(url, pageOptions, (res) => { | |
const { statusCode } = res; | |
const contentType = res.headers['content-type']; | |
let error; | |
if (statusCode !== 200) { | |
error = new Error('Request Failed.\n' + | |
`Status Code: ${statusCode}`); | |
console.log(res); | |
} else if (!/^application\/json/.test(contentType)) { | |
error = new Error('Invalid content-type.\n' + | |
`Expected application/json but received ${contentType}`); | |
} | |
if (error) { | |
console.error(error.message); | |
// Consume response data to free up memory | |
res.resume(); | |
return; | |
} | |
res.setEncoding('utf8'); | |
let rawData = ''; | |
res.on('data', (chunk) => { rawData += chunk; }); | |
res.on('end', () => { | |
try { | |
const parsedData = JSON.parse(rawData); | |
comments = comments.concat(parsedData); | |
if(parsedData[0]) { | |
getPage(n+1); | |
} else { | |
fs.writeFileSync('comments.json', JSON.stringify(comments)); | |
} | |
// console.log(parsedData); | |
} catch (e) { | |
console.error(e.message); | |
} | |
}); | |
}).on('error', (e) => { | |
console.error(`Got error: ${e.message}`); | |
}); | |
} | |
getPage(1); |
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 fs = require('fs'); | |
const commentData = JSON.parse(fs.readFileSync('comments.json', 'utf8')); | |
commentData.forEach(f => { | |
console.log(f.user.login); | |
console.log(); | |
const x = f.body.split('\n'); | |
var first = true; | |
x.forEach(l => { | |
console.log((first?': ':' ') + l); | |
first = false; | |
}) | |
console.log(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment