diff --git a/bandcamp_importer.user.js b/bandcamp_importer.user.js index 7cd7f58..e064c86 100644 --- a/bandcamp_importer.user.js +++ b/bandcamp_importer.user.js @@ -87,7 +87,6 @@ var BandcampImport = { format: release.format }; release.discs.push(disc); - var total_duration = 0; $.each(bandcampAlbumData.trackinfo, function (index, bctrack) { var track = { 'title': bctrack.title, @@ -95,7 +94,6 @@ var BandcampImport = { 'artist_credit': [] }; disc.tracks.push(track); - total_duration += bctrack.duration; }); // Check for hidden tracks (more tracks in the download than shown for streaming ie.) @@ -170,10 +168,6 @@ var BandcampImport = { }); } - if (!release.type) { - release.type = MBReleaseImportHelper.guessReleaseType(release.title, disc.tracks.length, total_duration); - } - return release; }, diff --git a/beatport_pro_importer.user.js b/beatport_pro_importer.user.js index 5e8a221..2bfc2b7 100644 --- a/beatport_pro_importer.user.js +++ b/beatport_pro_importer.user.js @@ -66,7 +66,6 @@ function retrieveReleaseInfo(release_url) { var the_tracks = unsafeWindow.Playables.tracks; var seen_tracks = {}; // to shoot duplicates ... var release_artists = []; - var total_duration = 0; $.each(the_tracks, function (idx, track) { if (track.release.id != release_id) { @@ -94,7 +93,6 @@ function retrieveReleaseInfo(release_url) { 'title': title, 'duration': track.duration.minutes }); - total_duration += track.duration.milliseconds; } ); @@ -114,7 +112,6 @@ function retrieveReleaseInfo(release_url) { 'format': release.format } ); - release.type = MBReleaseImportHelper.guessReleaseType(release.title, tracks.length, total_duration/1000); LOGGER.info("Parsed release: ", release); return release; } diff --git a/cd1d_importer.user.js b/cd1d_importer.user.js index 6378237..4f83bb7 100644 --- a/cd1d_importer.user.js +++ b/cd1d_importer.user.js @@ -70,15 +70,13 @@ var CD1DImporter = { // For each row that's "mapped", return an object that // describes the first and second in the row. - var duration = row.find('td.tracklist-content-length').text().replace('"', '').replace('\' ', ':').split( - ':'); - duration = 60 * parseInt(duration[0], 10) + parseInt(duration[1], 10); // convert MM:SS to seconds + var duration = row.find('td.tracklist-content-length').text().replace('"', '').replace('\' ', ':') // drop track number prefix (A A2 C3 01 05 etc...) var title = row.find('td.tracklist-content-title').text().replace(/^[0-9A-F][0-9]* /, ''); return { title: title, - duration: duration * 1000 // milliseconds in MB + duration: MBReleaseImportHelper.hmsToMilliSeconds(duration) }; }).get(); discs.push(disc); @@ -236,8 +234,6 @@ var CD1DImporter = { .get(); // Tracks - var total_duration = 0; - var total_tracks = 0; $.each(this.getTracks(format.id), function (ndisc, disc) { var thisdisc = { tracks: [], @@ -245,8 +241,6 @@ var CD1DImporter = { }; release.discs.push(thisdisc); $.each(this, function (ntrack, track) { - total_duration += track.duration / 1000; - total_tracks += 1; thisdisc.tracks.push({ 'title': track.title, 'duration': track.duration, @@ -255,10 +249,6 @@ var CD1DImporter = { }); }); - if (!release.type) { - release.type = MBReleaseImportHelper.guessReleaseType(release.title, total_tracks, total_duration); - } - LOGGER.info("Parsed release: ", format.name, release); return release; }, diff --git a/discogs_importer.user.js b/discogs_importer.user.js index ebedede..1194460 100644 --- a/discogs_importer.user.js +++ b/discogs_importer.user.js @@ -338,31 +338,6 @@ function insertLink(release, current_page_key) { // Parsing of Discogs data // //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -// TODO: move utility functions to lib -// convert HH:MM:SS or MM:SS to seconds -function hmsToSeconds(str) { - var t = str.split(':'); - var s = 0; - var m = 1; - while (t.length > 0) { - s += m * parseInt(t.pop(), 10); - m *= 60; - } - return s; -} - -// convert seconds to H:M:S or M:SS -function secondsToHms(secs) { - var sep = ':'; - var h = parseInt(secs/3600, 10) % 24; - var m = parseInt(secs/60, 10) % 60; - var s = secs % 60; - var r = ""; - if (h > 0) { - return h + sep + (m < 10 ? "0" + m : m) + sep + (s < 10 ? "0" + s : s); - } - return m + sep + (s < 10 ? "0" + s : s); -} // Analyze Discogs data and return a release object function parseDiscogsRelease(data) { @@ -490,8 +465,6 @@ function parseDiscogsRelease(data) { var heading = ""; var releaseNumber = 1; var lastPosition = 0; - var total_tracks = 0; - var total_duration = 0; $.each(discogsRelease.tracklist, function(index, discogsTrack) { if (discogsTrack.type_ == 'heading') { @@ -504,7 +477,7 @@ function parseDiscogsRelease(data) { var track = new Object(); track.title = discogsTrack.title; - track.duration = hmsToSeconds(discogsTrack.duration) * 1000; // MB in milliseconds + track.duration = MBReleaseImportHelper.hmsToMilliSeconds(discogsTrack.duration); // MB in milliseconds // Track artist credit track.artist_credit = new Array(); @@ -538,7 +511,7 @@ function parseDiscogsRelease(data) { return; } if (subtrack.duration) { - subtrack_total_duration += hmsToSeconds(subtrack.duration) * 1000; + subtrack_total_duration += MBReleaseImportHelper.hmsToMilliSeconds(subtrack.duration); } if (subtrack.title) { subtrack_titles.push(subtrack.title); @@ -552,7 +525,7 @@ function parseDiscogsRelease(data) { } track.title += subtrack_titles.join(' / '); } - if (!track.duration && subtrack_total_duration) { + if (isNaN(track.duration) && !isNaN(subtrack_total_duration)) { track.duration = subtrack_total_duration; } } @@ -617,15 +590,9 @@ function parseDiscogsRelease(data) { // Trackposition is empty e.g. for release title if (trackPosition != "" && trackPosition != null) { release.discs[releaseNumber-1].tracks.push(track); - total_tracks += 1; - total_duration += track.duration; } }); - if (!release.type) { - release.type = MBReleaseImportHelper.guessReleaseType(release.title, total_tracks, total_duration / 1000); - } - LOGGER.info("Parsed release: ", release); return release; } diff --git a/lib/import_functions.js b/lib/import_functions.js index c1773b1..e6fd545 100644 --- a/lib/import_functions.js +++ b/lib/import_functions.js @@ -126,7 +126,6 @@ var MBReleaseImportHelper = (function() { appendParameter(parameters, 'type', release.secondary_types[i]); } } - appendParameter(parameters, 'type', release.type); appendParameter(parameters, 'status', release.status); appendParameter(parameters, 'language', release.language); appendParameter(parameters, 'script', release.script); @@ -162,6 +161,9 @@ var MBReleaseImportHelper = (function() { } // Mediums + var total_tracks = 0; + var total_tracks_with_duration = 0; + var total_duration = 0; for (var i=0; i < release.discs.length; i++) { var disc = release.discs[i]; appendParameter(parameters, 'mediums.'+i+'.format', disc.format); @@ -170,15 +172,28 @@ var MBReleaseImportHelper = (function() { // Tracks for (var j=0; j < disc.tracks.length; j++) { var track = disc.tracks[j]; + total_tracks++; appendParameter(parameters, 'mediums.'+i+'.track.'+j+'.number', track.number); appendParameter(parameters, 'mediums.'+i+'.track.'+j+'.name', track.title); - var tracklength = (typeof track.duration != 'undefined' && track.duration != '') ? track.duration : "?:??"; + var tracklength = "?:??"; + var duration_ms = hmsToMilliSeconds(track.duration); + if (!isNaN(duration_ms)) { + tracklength = duration_ms; + total_tracks_with_duration++; + total_duration += duration_ms; + } appendParameter(parameters, 'mediums.'+i+'.track.'+j+'.length', tracklength); buildArtistCreditsFormParameters(parameters, 'mediums.'+i+'.track.'+j+'.', track.artist_credit); } } + // Guess release type if not given + if (!release.type && release.title && total_tracks == total_tracks_with_duration) { + release.type = fnGuessReleaseType(release.title, total_tracks, total_duration); + } + appendParameter(parameters, 'type', release.type); + // Add Edit note parameter appendParameter(parameters, 'edit_note', edit_note); @@ -204,21 +219,45 @@ var MBReleaseImportHelper = (function() { return artists; } - // Try to guess release type using number of tracks, title and total duration (in secs) - function fnGuessReleaseType(title, num_tracks, duration) { - if (num_tracks < 1 || duration < 60) return ''; + // Try to guess release type using number of tracks, title and total duration (in millisecs) + function fnGuessReleaseType(title, num_tracks, duration_ms) { + if (num_tracks < 1) return ''; var has_single = !!title.match(/\bsingle\b/i); var has_EP = !!title.match(/\bEP\b/i); if (has_single && has_EP) { has_single = false; has_EP = false; } - if (((has_single && num_tracks <= 4) || num_tracks <= 2) && duration <= 11*60) return 'single'; - if ((has_EP || num_tracks <= 6) && duration <= 30*60) return 'EP'; - if (duration > 30*60 && num_tracks > 6) return 'album'; + var perhaps_single = ((has_single && num_tracks <= 4) || num_tracks <= 2); + var perhaps_EP = has_EP || (num_tracks > 2 && num_tracks <= 6); + var perhaps_album = (num_tracks > 8); + if (isNaN(duration_ms)) { + // no duration, try to guess with title and number of tracks + if (perhaps_single && !perhaps_EP && !perhaps_album) return 'single'; + if (!perhaps_single && perhaps_EP && !perhaps_album) return 'EP'; + if (!perhaps_single && !perhaps_EP && perhaps_album) return 'album'; + return ''; + } + var duration_mn = duration_ms / (60*1000); + if (perhaps_single && duration_mn >= 1 && duration_mn < 7) return 'single'; + if (perhaps_EP && duration_mn > 7 && duration_mn <= 30) return 'EP'; + if (perhaps_album && duration_mn > 30) return 'album'; return ''; } + // convert HH:MM:SS or MM:SS to milliseconds + function hmsToMilliSeconds(str) { + if (typeof str == 'number') return str; + var t = str.split(':'); + var s = 0; + var m = 1; + while (t.length > 0) { + s += m * parseInt(t.pop(), 10); + m *= 60; + } + return s*1000; + } + // --------------------------------------- privates ----------------------------------------- // function appendParameter(parameters, paramName, paramValue) { @@ -252,6 +291,7 @@ var MBReleaseImportHelper = (function() { buildFormParameters: fnBuildFormParameters, makeArtistCredits: fnArtistCredits, guessReleaseType: fnGuessReleaseType, + hmsToMilliSeconds: hmsToMilliSeconds, URL_TYPES: url_types }; })();