Created
September 6, 2011 21:12
-
-
Save ryanj/1198983 to your computer and use it in GitHub Desktop.
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
// Load the library - https://github.com/ryanjarvinen/eventbrite.npm | |
var Eventbrite = require('eventbrite'); | |
// Load creationix's couch-client | |
var CouchClient = require('couch-client'); | |
// Initialize the Eventbrite API client - http://www.eventbrite.com/api/key/ | |
var eb_client = Eventbrite('RWJPIH4B6FVFAIAK6Y'); | |
// Initialize your couchbase client: | |
var db = CouchClient('http://stickbyatlas.iriscouch.com/experiment/'); | |
// some local counters: | |
var total_events = 0; | |
var pub_events = 0; | |
var online_events = 0; | |
var pages = 0; | |
// 100 per page is Eventbrite's max | |
var max_per_page = 100; | |
// LIST_USER_EVENTS CODE HERE | |
var approved_users = | |
{ | |
"[email protected]" : "Titanfile", | |
"[email protected]" : "Torusoft" | |
} | |
var user_array = Object.keys(approved_users); | |
var approved_events = []; | |
function getEvents(){ | |
for(var i = 0; i < user_array.length ; i++){ | |
addUserEvents(i, user_array, function(err, data){ | |
console.log(data.length); | |
processEvents(data); | |
}); | |
} | |
} | |
function addUserEvents(index, records, callback){ | |
eb_client.user_list_events( {'user': records[index]}, function(err, data){ | |
for(var j = 0; j < data.events.length ; j++){ | |
approved_events[approved_events.length] = data.events[j]; | |
if( ( (index+1) == records.length) && ((j+1) == data.events.length) ) //this conditional ensures that processEvents is only called on the last iteration | |
callback(0,approved_events); | |
} | |
}); | |
} | |
function processEvents(data){ //data is the approved_events array | |
console.log("Length of approved_events is: " + data.length); | |
var index=0; | |
checkForEvent(index, data, function callback(err, data){ | |
if((index + 1) != data.length){ | |
index++; | |
checkForEvent(index, data, callback); | |
} | |
else{ | |
console.log("finished!"); | |
//function call goes here | |
} | |
}); | |
} | |
function checkForEvent(index, records, callback){ | |
console.log("checkForEvent index: " + index); | |
var e = records[index].event; | |
db.get(e.id.toString(), function(err, theData){ | |
//console.log(e.iteration + ", " + err + ", " + theData); | |
if(err){ | |
console.log(e.id.toString() + " wasn't found in the database. creating this record now."); | |
//console.log(err); | |
db.save({_id: e.id.toString(), 'eb_event': e, 'loc': [ e.venue.longitude, e.venue.latitude ], "geometry": {"type":"Point","coordinates": [ e.venue.longitude, e.venue.latitude ]}}, function(err, result) { | |
if (err) throw err; | |
callback(0, records); | |
}); | |
} | |
else{ | |
console.log(e.id + ' already exists! check date modified to see if it needs to be updated!'); | |
if(e.modified != theData.eb_event.modified){ // date modified doesn't match. update is needed. | |
db.save({_id: e.id.toString(), 'eb_event': e, 'loc': [ e.venue.longitude, e.venue.latitude ], "geometry": {"type":"Point","coordinates": [ e.venue.longitude, e.venue.latitude ]}}, function(err, result) { | |
if (err) throw err; | |
console.log("updated " + e.id); | |
callback(0, records); | |
}); | |
} | |
else //they're the same, so no update needed | |
callback(0, records); | |
} | |
//console.log(theData); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment