Skip to content

Instantly share code, notes, and snippets.

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 gmarceau/2f419ef3a8951971cd8f to your computer and use it in GitHub Desktop.
Save gmarceau/2f419ef3a8951971cd8f to your computer and use it in GitHub Desktop.
structure of the code follows structure of the data
// loadtweets.js: exports init function, which loads tweets from ./tweets folder into tweet_collection global variable.
var fs = require('fs');
var _und = require('./underscore-min')._;
tweet_collection = [];
function Tweet(diskTweet, donor) {
// if encapsulated RT, act on retweeted_status value instead of entire tweet
if (tweet_json.retweeted_status) {
tweet_json = tweet_json.retweeted_status
};
_und.extend(this, tweet_json);
this.created_at = new Date(this.created_at);
this.donors = [];
this.donors.push(donor);
// console.log(this.id_str + " donated by " + this.donors);
}
function parseDiskTweet(disktweet, donor)
{
return new Tweet(tweet, donor);
}
function parseDiskTweets(diskTweets, donor)
{
_.map(function (t) { return diskTweets(t, donor) }, parseDiskTweet);
}
function parseDiskTweetsFileContent(content, donor)
{
var diskTweets = JSON.parse(content.substring(fileContents.indexOf("[")));
parseDiskTweets(diskTweets);
}
function parseDiskTweetFile(filename, callback, donor)
{
fs.readFile(file, 'utf8', function(err, fileContents) { // Read in file and pass to callback
if (err) {
console.log(err);
}
else if (fileContents.substr(0,9) !== "Grailbird") { // If the file is not valid, skip and notify.
console.log("-- File " + file + " was not a valid Grailbird data file and was skipped.");
callback([]); // Still call back an empty array so it's accounted for.
}
else
callback(parseDiskTweetsFileContent(fileContent, donor));
});
}
function parseDirectory(dirname)
{
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment