importers: move release type guessing to fnBuildFormParameters()

- use milliseconds for track durations everywhere for consistency
- use total duration in guessing only if all tracks have a duration
- improve a bit the differenciation single/EP/album
- it only happens if no release type was set by user script
This commit is contained in:
Laurent Monin 2015-06-13 04:25:31 +02:00
parent e9c648daa1
commit 6d44668600
5 changed files with 53 additions and 65 deletions

View file

@ -87,7 +87,6 @@ var BandcampImport = {
format: release.format format: release.format
}; };
release.discs.push(disc); release.discs.push(disc);
var total_duration = 0;
$.each(bandcampAlbumData.trackinfo, function (index, bctrack) { $.each(bandcampAlbumData.trackinfo, function (index, bctrack) {
var track = { var track = {
'title': bctrack.title, 'title': bctrack.title,
@ -95,7 +94,6 @@ var BandcampImport = {
'artist_credit': [] 'artist_credit': []
}; };
disc.tracks.push(track); disc.tracks.push(track);
total_duration += bctrack.duration;
}); });
// Check for hidden tracks (more tracks in the download than shown for streaming ie.) // 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; return release;
}, },

View file

@ -66,7 +66,6 @@ function retrieveReleaseInfo(release_url) {
var the_tracks = unsafeWindow.Playables.tracks; var the_tracks = unsafeWindow.Playables.tracks;
var seen_tracks = {}; // to shoot duplicates ... var seen_tracks = {}; // to shoot duplicates ...
var release_artists = []; var release_artists = [];
var total_duration = 0;
$.each(the_tracks, $.each(the_tracks,
function (idx, track) { function (idx, track) {
if (track.release.id != release_id) { if (track.release.id != release_id) {
@ -94,7 +93,6 @@ function retrieveReleaseInfo(release_url) {
'title': title, 'title': title,
'duration': track.duration.minutes 'duration': track.duration.minutes
}); });
total_duration += track.duration.milliseconds;
} }
); );
@ -114,7 +112,6 @@ function retrieveReleaseInfo(release_url) {
'format': release.format 'format': release.format
} ); } );
release.type = MBReleaseImportHelper.guessReleaseType(release.title, tracks.length, total_duration/1000);
LOGGER.info("Parsed release: ", release); LOGGER.info("Parsed release: ", release);
return release; return release;
} }

View file

@ -70,15 +70,13 @@ var CD1DImporter = {
// For each row that's "mapped", return an object that // For each row that's "mapped", return an object that
// describes the first and second <td> in the row. // describes the first and second <td> in the row.
var duration = row.find('td.tracklist-content-length').text().replace('"', '').replace('\' ', ':').split( var duration = row.find('td.tracklist-content-length').text().replace('"', '').replace('\' ', ':')
':');
duration = 60 * parseInt(duration[0], 10) + parseInt(duration[1], 10); // convert MM:SS to seconds
// drop track number prefix (A A2 C3 01 05 etc...) // 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]* /, ''); var title = row.find('td.tracklist-content-title').text().replace(/^[0-9A-F][0-9]* /, '');
return { return {
title: title, title: title,
duration: duration * 1000 // milliseconds in MB duration: MBReleaseImportHelper.hmsToMilliSeconds(duration)
}; };
}).get(); }).get();
discs.push(disc); discs.push(disc);
@ -236,8 +234,6 @@ var CD1DImporter = {
.get(); .get();
// Tracks // Tracks
var total_duration = 0;
var total_tracks = 0;
$.each(this.getTracks(format.id), function (ndisc, disc) { $.each(this.getTracks(format.id), function (ndisc, disc) {
var thisdisc = { var thisdisc = {
tracks: [], tracks: [],
@ -245,8 +241,6 @@ var CD1DImporter = {
}; };
release.discs.push(thisdisc); release.discs.push(thisdisc);
$.each(this, function (ntrack, track) { $.each(this, function (ntrack, track) {
total_duration += track.duration / 1000;
total_tracks += 1;
thisdisc.tracks.push({ thisdisc.tracks.push({
'title': track.title, 'title': track.title,
'duration': track.duration, '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); LOGGER.info("Parsed release: ", format.name, release);
return release; return release;
}, },

View file

@ -338,31 +338,6 @@ function insertLink(release, current_page_key) {
// Parsing of Discogs data // // 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 // Analyze Discogs data and return a release object
function parseDiscogsRelease(data) { function parseDiscogsRelease(data) {
@ -490,8 +465,6 @@ function parseDiscogsRelease(data) {
var heading = ""; var heading = "";
var releaseNumber = 1; var releaseNumber = 1;
var lastPosition = 0; var lastPosition = 0;
var total_tracks = 0;
var total_duration = 0;
$.each(discogsRelease.tracklist, function(index, discogsTrack) { $.each(discogsRelease.tracklist, function(index, discogsTrack) {
if (discogsTrack.type_ == 'heading') { if (discogsTrack.type_ == 'heading') {
@ -504,7 +477,7 @@ function parseDiscogsRelease(data) {
var track = new Object(); var track = new Object();
track.title = discogsTrack.title; 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
track.artist_credit = new Array(); track.artist_credit = new Array();
@ -538,7 +511,7 @@ function parseDiscogsRelease(data) {
return; return;
} }
if (subtrack.duration) { if (subtrack.duration) {
subtrack_total_duration += hmsToSeconds(subtrack.duration) * 1000; subtrack_total_duration += MBReleaseImportHelper.hmsToMilliSeconds(subtrack.duration);
} }
if (subtrack.title) { if (subtrack.title) {
subtrack_titles.push(subtrack.title); subtrack_titles.push(subtrack.title);
@ -552,7 +525,7 @@ function parseDiscogsRelease(data) {
} }
track.title += subtrack_titles.join(' / '); track.title += subtrack_titles.join(' / ');
} }
if (!track.duration && subtrack_total_duration) { if (isNaN(track.duration) && !isNaN(subtrack_total_duration)) {
track.duration = subtrack_total_duration; track.duration = subtrack_total_duration;
} }
} }
@ -617,15 +590,9 @@ function parseDiscogsRelease(data) {
// Trackposition is empty e.g. for release title // Trackposition is empty e.g. for release title
if (trackPosition != "" && trackPosition != null) { if (trackPosition != "" && trackPosition != null) {
release.discs[releaseNumber-1].tracks.push(track); 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); LOGGER.info("Parsed release: ", release);
return release; return release;
} }

View file

@ -126,7 +126,6 @@ var MBReleaseImportHelper = (function() {
appendParameter(parameters, 'type', release.secondary_types[i]); appendParameter(parameters, 'type', release.secondary_types[i]);
} }
} }
appendParameter(parameters, 'type', release.type);
appendParameter(parameters, 'status', release.status); appendParameter(parameters, 'status', release.status);
appendParameter(parameters, 'language', release.language); appendParameter(parameters, 'language', release.language);
appendParameter(parameters, 'script', release.script); appendParameter(parameters, 'script', release.script);
@ -162,6 +161,9 @@ var MBReleaseImportHelper = (function() {
} }
// Mediums // Mediums
var total_tracks = 0;
var total_tracks_with_duration = 0;
var total_duration = 0;
for (var i=0; i < release.discs.length; i++) { for (var i=0; i < release.discs.length; i++) {
var disc = release.discs[i]; var disc = release.discs[i];
appendParameter(parameters, 'mediums.'+i+'.format', disc.format); appendParameter(parameters, 'mediums.'+i+'.format', disc.format);
@ -170,15 +172,28 @@ var MBReleaseImportHelper = (function() {
// Tracks // Tracks
for (var j=0; j < disc.tracks.length; j++) { for (var j=0; j < disc.tracks.length; j++) {
var track = disc.tracks[j]; var track = disc.tracks[j];
total_tracks++;
appendParameter(parameters, 'mediums.'+i+'.track.'+j+'.number', track.number); appendParameter(parameters, 'mediums.'+i+'.track.'+j+'.number', track.number);
appendParameter(parameters, 'mediums.'+i+'.track.'+j+'.name', track.title); 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); appendParameter(parameters, 'mediums.'+i+'.track.'+j+'.length', tracklength);
buildArtistCreditsFormParameters(parameters, 'mediums.'+i+'.track.'+j+'.', track.artist_credit); 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 // Add Edit note parameter
appendParameter(parameters, 'edit_note', edit_note); appendParameter(parameters, 'edit_note', edit_note);
@ -204,20 +219,44 @@ var MBReleaseImportHelper = (function() {
return artists; return artists;
} }
// Try to guess release type using number of tracks, title and total duration (in secs) // Try to guess release type using number of tracks, title and total duration (in millisecs)
function fnGuessReleaseType(title, num_tracks, duration) { function fnGuessReleaseType(title, num_tracks, duration_ms) {
if (num_tracks < 1 || duration < 60) return ''; if (num_tracks < 1) return '';
var has_single = !!title.match(/\bsingle\b/i); var has_single = !!title.match(/\bsingle\b/i);
var has_EP = !!title.match(/\bEP\b/i); var has_EP = !!title.match(/\bEP\b/i);
if (has_single && has_EP) { if (has_single && has_EP) {
has_single = false; has_single = false;
has_EP = false; has_EP = false;
} }
if (((has_single && num_tracks <= 4) || num_tracks <= 2) && duration <= 11*60) return 'single'; var perhaps_single = ((has_single && num_tracks <= 4) || num_tracks <= 2);
if ((has_EP || num_tracks <= 6) && duration <= 30*60) return 'EP'; var perhaps_EP = has_EP || (num_tracks > 2 && num_tracks <= 6);
if (duration > 30*60 && num_tracks > 6) return 'album'; 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 ''; 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 ----------------------------------------- // // --------------------------------------- privates ----------------------------------------- //
@ -252,6 +291,7 @@ var MBReleaseImportHelper = (function() {
buildFormParameters: fnBuildFormParameters, buildFormParameters: fnBuildFormParameters,
makeArtistCredits: fnArtistCredits, makeArtistCredits: fnArtistCredits,
guessReleaseType: fnGuessReleaseType, guessReleaseType: fnGuessReleaseType,
hmsToMilliSeconds: hmsToMilliSeconds,
URL_TYPES: url_types URL_TYPES: url_types
}; };
})(); })();