Skip to content

Instantly share code, notes, and snippets.

@martindale
Created January 30, 2015 20:56
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 martindale/4990813bd82423ec1666 to your computer and use it in GitHub Desktop.
Save martindale/4990813bd82423ec1666 to your computer and use it in GitHub Desktop.
var singleString = 'Some Artist - Some Title';
var duration = 600; // length in seconds (required!)
// ideally, you have an object with the artist and title separate (and maybe credits!)
// you can use soundtrack.trackFromSource('object', ... , ...) for this
// see https://github.com/martindale/soundtrack.io/blob/soundtrack.io/lib/soundtrack.js#L326-L352
// however...
// if you only have a single string (like youtube), you can do this...
var parts = util.parseTitleString( singleString , function(parts) {
// collect the artist BY ITS SLUG – this is normalization
// it will find the first matching artist (i.e., first come first serve for names)
Artist.findOne({ slug: slug( parts.artist ) }).exec(function(err, artist) {
// doesn't exist? create one. you'll have to save it, though.
if (!artist) artist = new Artist({ name: parts.artist });
// similarly, find a track (normalized by slug(title), but only with the located artist
// this will fail if it's a brand new artist, and will subsequently create one
Track.findOne({
_artist: artist._id,
slug: slug( parts.title )
}).exec(function(err, track) {
// no track found, create one with the title
if (!track) track = new Track({ title: parts.title });
// set the track's artist to the one we found before
track._artist = artist._id;
// set its duration (this is MANDATORY or track advance events won't be broadcast!)
track.duration = duration;
// save artist first...
artist.save(function(err) {
// then save the track...
track.save(function(err) {
// all done!
// do some callback here, for example with `async`:
// done( null , track )
});
});
});
});
});
@martindale
Copy link
Author

You'll find the current parseTitleString code insightful on how we extract "additional artists" from things like remixes. I keep a standalone version here so recommendations can be provided.

Also, direct audio streaming from soundtrack may interest you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment