Skip to content

Instantly share code, notes, and snippets.

@lspitzner
Last active February 26, 2020 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lspitzner/59186eabc4dfe2e42546d079c6504508 to your computer and use it in GitHub Desktop.
Save lspitzner/59186eabc4dfe2e42546d079c6504508 to your computer and use it in GitHub Desktop.
fetch ghc proposal comments
# 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.
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);
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