Created
February 5, 2014 02:31
-
-
Save piersroberts/8816515 to your computer and use it in GitHub Desktop.
Snippet to pull album from discogs and generate a .cue file
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
var url = 'http://api.discogs.com/masters/365003'; | |
function Cue(albumArtist, title, tracks) { | |
this.PERFORMER = albumArtist; | |
this.TITLE = title; | |
this.TRACKS = tracks; | |
}; | |
function Track(trackNumber, artist, title, offset) { | |
this.TITLE = title; | |
this.PERFORMER = artist; | |
this.TRACK = trackNumber + ' AUDIO'; | |
this.INDEX = '01 ' + offset; | |
}; | |
Number.prototype.toMMSS = function () { | |
var sec_num = parseInt(this, 10); | |
var minutes = Math.floor(sec_num / 60); | |
var seconds = sec_num - (minutes * 60); | |
if (minutes < 10) { | |
minutes = "0" + minutes; | |
} | |
if (seconds < 10) { | |
seconds = "0" + seconds; | |
} | |
var time = minutes + ':' + seconds; | |
return time; | |
} | |
var CueMaker = { | |
processDiscogs : function (response) { | |
//console.log(response); | |
discs = []; | |
response.tracklist.forEach(function (track) { | |
if (track.duration != '' && track.position != '') { | |
if (typeof seconds === 'undefined') { | |
seconds = 0 | |
} | |
_track = track.position.split('-'); | |
if (this[_track[0]] == undefined) { | |
this[_track[0]] = []; | |
} | |
trackId = ('00' + parseInt(_track[1])).substr(-2); | |
minutesSeconds = track.duration.split(':'); | |
seconds += minutesSeconds[0] * 60 + ~~minutesSeconds[1]; | |
offset = seconds.toMMSS(); | |
newTrack = new Track(trackId, track.artists[0].name, track.title, offset); | |
this[_track[0]].push(newTrack); | |
} | |
}, discs); | |
cues = []; | |
discs.forEach(function (disc) { | |
this.push(new Cue(response.artists[0].name, response.title, disc)) | |
}, cues); | |
console.log(cues); | |
} | |
} | |
$.get(url, CueMaker.processDiscogs); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment