Skip to content

Instantly share code, notes, and snippets.

@wayne-o
Created February 11, 2015 13:57
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 wayne-o/00bdb81be4d5c47678b1 to your computer and use it in GitHub Desktop.
Save wayne-o/00bdb81be4d5c47678b1 to your computer and use it in GitHub Desktop.
var request = require("request");
var Waterline = require('waterline');
var cloudinary = require('cloudinary');
cloudinary.config({
cloud_name: '****',
api_key: '****',
api_secret: '****'
});
//add urls of cf data that you want to import here
var urls = [
"http://clashfinder.com/data/event/buggedout2015.json",
"http://clashfinder.com/data/event/g2013.json"
];
var orm = new Waterline();
var mongoAdapter = require('sails-mongo');
var _ = require('lodash');
var RSVP = require('rsvp');
var echojs = require('echojs');
var echo = echojs({
key: '****'
});
var config = {
adapters: {
'default': mongoAdapter,
mongo: mongoAdapter
},
connections: {
myLocalMongo: {
adapter: 'mongo',
host: 'localhost',
port: 27017
}
},
defaults: {
migrate: 'drop'
}
};
var EventInstance = Waterline.Collection.extend({
identity: 'eventinstance',
connection: 'myLocalMongo',
attributes: {
name: {type: 'string'},
dateFrom: {type: 'date'},
dateTo: {type: 'date'},
locations: {
collection: 'location',
via: 'eventInstance'
}
}
});
var EventLocation = Waterline.Collection.extend({
identity: 'location',
connection: 'myLocalMongo',
attributes: {
name: {type: 'string'},
eventInstance: {
model: 'eventInstance'
}
}
});
var EventEvent = Waterline.Collection.extend({
identity: 'listingEvent',
connection: 'myLocalMongo',
attributes: {
name: {type: 'string'},
start: {type: 'date'},
end: {type: 'date'},
eventInstanceSlug: {type: 'string'},
location: {
model: 'location'
},
artists: {
collection: 'artist',
via: 'listingEventsPlayedAt'
}
}
});
var Artist = Waterline.Collection.extend({
identity: 'artist',
connection: 'myLocalMongo',
attributes: {
name: {type: 'string'},
listingEventsPlayedAt: {
collection: 'listingEvent',
via: 'artists'
},
images: {
collection: 'image',
via: 'artist'
}
}
});
var Image = Waterline.Collection.extend({
identity: 'image',
connection: 'myLocalMongo',
attributes: {
signature: {type: 'string'},
artist: {
model: 'artist'
},
width: {type: 'integer'},
height: {type: 'integer'},
format: {type: 'string'},
url: {type: 'string'},
secureUrl: {type: 'string'},
tags: {type: 'array'}
}
});
orm.loadCollection(EventLocation);
orm.loadCollection(EventInstance);
orm.loadCollection(EventEvent);
orm.loadCollection(Artist);
orm.loadCollection(Image);
var app = {};
orm.initialize(config, function (err, models) {
if (err) throw err;
app.models = models.collections;
app.connections = models.connections;
//console.log(app.models);
_.forEach(urls, function (u) {
importEvent(u);
});
});
function slugify(text) {
return text.toString().toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, ''); // Trim - from end of text
}
function importEvent(url) {
request({
url: url,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
var ei = {
name: body.name,
slug: slugify(body.name)
};
//TODO: need to add the locations to the event isntance being saved and update
try {
app.models.eventinstance.create(ei, function (err, savedEventInstance) {
if (err) {
console.log(err);
}
for (var i = body.locations.length - 1; i >= 0; i--) {
var l = {
name: body.locations[i].name,
eventInstance: savedEventInstance
};
app.models.location.create(l, function (err, savedLocation) {
if (err) {
console.log(err);
}
var originalLocation = _.find(body.locations, function (chr) {
return chr.name == savedLocation.name;
})
for (var listingEventIndex = originalLocation.events.length - 1; listingEventIndex >= 0; listingEventIndex--) {
var importListingEvent = originalLocation.events[listingEventIndex];
var le = {
name: importListingEvent.name,
start: importListingEvent.start,
end: importListingEvent.end,
location: savedLocation,
eventInstanceSlug: slugify(body.name),
slug: slugify(importListingEvent.name)
};
app.models.listingevent.create(le, function (err, savedListingEvent) {
if (err) {
console.log(err);
}
//TODO: check to see if we already have the artist
// if we do use that one
// else make a new one
app.models.artist.find({where: {name: savedListingEvent.name}})
.exec(function (err, artists) {
if (artists && artists.length > 0) {
console.log('sonatribe does know about this artist - adding le to it' + savedListingEvent.name);
artists[0].listingEventsPlayedAt.add(savedListingEvent);
artists[0].save(function (err) {
if (err) console.log(err);
console.log('added le to existing artist' + savedListingEvent.name);
});
}
else {
console.log('sonatribe does not know about this artist - creating a new one and adding le to it' + savedListingEvent.name);
echo('artist/search', { bucket: ['genre', 'images', 'biographies']})
.get({name: savedListingEvent.name}, function (err, json) {
if (json.response.artists != null && json.response.artists.length > 0) {
var jsonArtist = json.response.artists[0];
var stArtist = {
name: jsonArtist.name,
slug: slugify(jsonArtist.name)
};
app.models. artist.create(stArtist, function(err, modelArtist){
modelArtist.listingEventsPlayedAt.add(savedListingEvent);
modelArtist.save(function (err) {
if (err) console.log(err);
console.log('added le to new artist' + savedListingEvent.name);
});
processArtistImages(err, modelArtist, jsonArtist)
});
} else {
//TODO: if echonest does not know about the artist
// create one
}
});
}
});
});
}
});
}
});
}
catch (err) {
console.log(err);
}
} else {
console.log(error);
}
});
console.log('done');
}
function processArtistImages(err, modelArtist, artist) {
if (err) {
console.log(err);
}
_.forEach(artist.images, function (image) {
cloudinary.uploader.upload(image.url, function(result) {
console.log(result);
var img = {
artist: modelArtist,
signature: result.signature,
width: result.width,
height: result.height,
format: result.format,
url: result.url,
secureUrl: result.secure_url,
tags: result.tags
};
app.models.image.create(img, function (err, simg) {
if(err) console.log(err);
console.log('saved image for artist');
modelArtist.images.add(simg);
modelArtist.save(function (err) {
if (err) console.log(err);
console.log('added image to artist' + modelArtist.name);
});
});
},{
crop: 'limit',
width: 2000,
height: 2000,
tags: [ 'artist' ]
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment