Skip to content

Instantly share code, notes, and snippets.

@bryanmr
Created June 17, 2023 04:09
Show Gist options
  • Save bryanmr/6aeb6f4481f71f9e6f806b0f408e8297 to your computer and use it in GitHub Desktop.
Save bryanmr/6aeb6f4481f71f9e6f806b0f408e8297 to your computer and use it in GitHub Desktop.
/* exported fetchComments */
'use strict';
/** Fetches Reddit comment thread and then writes it to console
* @param {string} siteURL - URL of the post we are checking for */
exports.fetchComments = () => {
const siteURL = process.env.site_url;
const bucketName = process.env.bucket_short_name;
const bucketDir = process.env.bucket_dir;
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const myBucket = storage.bucket(bucketName);
const file = myBucket.file(bucketDir+'/reddit_comment_threads.json');
const https = require('https');
https.get('https://www.reddit.com/search.json?q=site%3A"'+
siteURL+'"', (res) => {
const {statusCode} = res;
const contentType = res.headers['content-type'];
let error;
if (statusCode !== 200) {
error = new Error('Request Failed.\n'+
`Status Code: ${statusCode}`);
} 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);
return;
}
let rawData = '';
res.setEncoding('utf8');
res.on('data', (chunk) => {
rawData += chunk;
});
res.on('end', () => {
file.save(rawData).then(function() {
console.log('Saved to file!');
}).catch(function(e) {
console.error('We failed at saving! Got: '+e.message);
});
});
}).on('error', (e) => {
console.error(`Got error: ${e.message}`);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment