Skip to content

Instantly share code, notes, and snippets.

@stickbyatlas
Created September 8, 2011 18:14
Show Gist options
  • Save stickbyatlas/1204158 to your computer and use it in GitHub Desktop.
Save stickbyatlas/1204158 to your computer and use it in GitHub Desktop.
Scans Eventbrite for events created by a specific group of users, mirrors these events on a CouchDB instance and notifies an administrator by email of any updates or additions.
// 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('YOUR API KEY');
// Initialize your couchbase client:
var db = CouchClient('PATH TO YOUR DATABASE');
// Initialize mailer, with SMTP information below:
var mailer = require('nodemailer');
// one time action to set up SMTP information
mailer.SMTP = {
host: 'smtp.gmail.com',
port: 465,
ssl: true,
use_authentication: true,
user: 'ACCOUNT NAME',
pass: 'PASSWORD'
}
// 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;
var approved_users =
{
LIST OF EVENTBRITE ACCOUNT EMAIL ADDRESSES & INFO
"bob@example.com" : {...},
"mary@example.com" : {...}
}
var user_array = Object.keys(approved_users);
var user_count = 0;
var approved_events = [];
function getEvents(){
console.log("Firing off all requests for user events.");
for(var i = 0; i < user_array.length ; i++){
addUserEvents(i);
}
}
function addUserEvents(index){
eb_client.user_list_events( {'user': user_array[index]}, function(err, data){
console.log(" In iteration " + index + " we got " + data.events.length + " events.");
user_count++;
if(data != null){
for(var j = 0; j < data.events.length ; j++){
approved_events[approved_events.length] = data.events[j];
if((user_count == user_array.length) && ((j+1) == data.events.length) ) //this conditional ensures that processEvents is only called on the last iteration
processEvents(approved_events);
}
}
else{
if( user_count == user_array.length)
processEvents(approved_events);
}
});
}
function processEvents(data){ // 'data' is the approved_events array
var index=0;
console.log("Checking the " + data.length + " events we found against our current database.");
checkForEvent(index, data, function callback(data){
if((index + 1) != data.length){
index++;
checkForEvent(index, data, callback);
}
else{
console.log("finished updating! time to check for items to delete...");
collectOldEvents(data);
}
});
}
function checkForEvent(index, records, next){
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);
if(e.venue && e.venue.latitude && e.venue.longitude){
db.save({_id: e.id.toString(), 'eb_event': e, 'loc': [ e.venue.longitude, e.venue.latitude ], "draft": true, "geometry": {"type":"Point","coordinates": [ e.venue.longitude, e.venue.latitude ]}}, function(err, result) {
if (err) throw err;
sendEmailAlert(e, "new");
next(records);
});
}
}
else{
console.log(" " + e.id + ' already exists! Checking 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.
if(e.venue && e.venue.latitude && e.venue.longitude){
db.save({_id: e.id.toString(), 'eb_event': e, 'loc': [ e.venue.longitude, e.venue.latitude ], "draft": true, "geometry": {"type":"Point","coordinates": [ e.venue.longitude, e.venue.latitude ]}}, function(err, result) {
if (err) throw err;
console.log(" Updated " + e.id);
sendEmailAlert(e, "update");
next(records);
});
}
}
else //they're the same, so no update needed
next(records);
}
});
}
function collectOldEvents(records){
db.view("/experiment/_all_docs",{},function(err,data){
for(var i=0; i<records.length; i++){
for(var j = 0; j<data.rows.length; j++){
if(records[i].event.id == data.rows[j].id){
data.rows.splice(j,1);
console.log(data.rows.length + " items left to match");
}
}
if((i+1) == records.length){
console.log("Finished checking duplicates. there are " + data.rows.length + " items to delete. Deleting them now.");
for(var k = 0; k <data.rows.length; k++){
deleteOldEvent(data.rows[k].id);
if((k+1) == data.length)
console.log("Finished sending last instruction to delete a record!");
}
}
}
});
};
function deleteOldEvent(key){
db.remove(key, function(err, result){
console.log("Deleted " + key);
})
};
function sendEmailAlert(e, type){
var subject = "New event for approval";
var message = "A new event has just been posted. Here are the details. <br><br>" + e.title + "<br><br>To approve this event, click <a href=http://www.torusoft.com>here</a>.";
if(type == "update"){
var subject = e.title + " has been updated";
var message = "An event has just been updated. Here are the details. <br><br>" + e.title + "<br><br>To approve this update, click <a href=http://www.torusoft.com>here</a>.";
}
mailer.send_mail(
{
sender: 'YOUR NAME <EMAIL ADDRESS>',
to:'RECIPIENT NAME <EMAIL ADDRESS>',
subject: subject,
html: message,
body:'HTML email is not supported.'
},
function(error, success){
console.log(success ? "Message sent regarding " + e.id : "Message regarding " + e.id + " failed with following error: \n" + error);
}
);
}
getEvents();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment