Last active
January 2, 2016 00:09
-
-
Save cadecairos/8221548 to your computer and use it in GitHub Desktop.
A script for counting remixes of makes between two dates.
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
// Title: remixes.js | |
// Description: a script for counting remixes of makes between two dates. | |
// Author: Christopher De Cairos | |
// Dependencies: makeapi-client ( https://github.com/mozilla/makeapi-client ) and async ( https://npmjs.org/package/async ) | |
// Usage: `node remixes "January 1, 2013 12:00:00 AM EST" "December 31, 2013 12:59:59 PM EST" | |
// you can change the time strings to whatever you wish. | |
// edit the URLs array with all the make URL's you wish to count remixes for. | |
// node must be able to find these packages | |
var Make = require( "makeapi-client" ), | |
async = require( "async" ); | |
var client = new Make({ | |
// Change this to any makeAPI endpoint that you wish to check metrics on | |
apiURL: "https://makeapi.webmaker.org" | |
}); | |
var from = process.argv[ 2 ] || 0, | |
to = process.argv[ 3 ] || Date.now(); | |
var LIMIT = 1000; | |
// Add as many Make URL's as required here | |
var urls = ["https://webmaker.makes.org/thimble/keep-calm"]; | |
if ( typeof from === "string" ) { | |
try { | |
from = new Date( from ).getTime(); | |
} catch( e ) { | |
console.error( "Invalid date string for `from`"); | |
} | |
} | |
if ( typeof to === "string" ) { | |
try { | |
to = new Date( to ).getTime(); | |
} catch( e ) { | |
console.error( "Invalid date string for `to`"); | |
} | |
} | |
function processMakes(url, makes, cb ) { | |
var remixCount = 0; | |
makes.forEach(function( make ) { | |
if ( make.createdAt < +from || make.createdAt > +to ) { | |
return; | |
} | |
remixCount++; | |
}); | |
console.log( url + "\nHas " + makes.length + " remixes in total and " + remixCount + " remixes in the specified date range.\n" ); | |
cb(); | |
} | |
console.log( "FROM DATE:\n" + (new Date( from )).toString() + "\n\nTO DATE:\n" + (new Date( to )).toString() + "\n" ); | |
async.eachSeries( urls, function( url, cb ) { | |
client.find({ | |
url: url | |
}) | |
.then(function( err, makes ) { | |
if ( err ) { | |
return cb( err ); | |
} | |
var id = id = makes[0]._id; | |
client.find({ | |
remixedFrom: id, | |
limit: LIMIT | |
}).then(function( err, makes, total ) { | |
if ( err ) { | |
return cb( err ); | |
} | |
var q, | |
tasks = [], | |
p = 2; | |
if ( makes.length < total ) { | |
q = async.queue(function( task, cb ) { | |
client.find({ | |
remixedFrom: id, | |
limit: LIMIT, | |
page: task | |
}).then(function( err, pageMakes ) { | |
if ( err ) { | |
return cb( err ); | |
} | |
makes = makes.concat( pageMakes ); | |
cb(); | |
}); | |
}, 1 ); | |
while ( total > LIMIT ) { | |
tasks.push( p++ ); | |
total -= LIMIT; | |
} | |
q.drain = function() { | |
processMakes( url, makes, cb ); | |
}; | |
q.push( tasks ); | |
} else { | |
processMakes( url, makes, cb ); | |
} | |
}); | |
}); | |
}, function( err ) { | |
if ( err ) { | |
console.log( JSON.stringify( err, null, 2 ) ); | |
} else { | |
console.log( "done!" ); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment