Merge pull request #52 from zas/beatport_pro_fixes

BeatPort Pro: make it work again
This commit is contained in:
Laurent Monin 2015-06-12 23:32:28 +02:00
commit 2f66a3f2e3
2 changed files with 125 additions and 46 deletions

View file

@ -1,47 +1,59 @@
// ==UserScript== // ==UserScript==
// @name Import Beatport Pro releases to MusicBrainz // @name Import Beatport Pro releases to MusicBrainz
// @author VxJasonxV // @author VxJasonxV
// @namespace https://github.com/murdos/musicbrainz-userscripts/
// @description One-click importing of releases from pro.beatport.com/release pages into MusicBrainz // @description One-click importing of releases from pro.beatport.com/release pages into MusicBrainz
// @sourceURL https://github.com/VxJasonxV/musicbrainz-userscripts/blob/master/beatport_pro_importer.user.js
// @version 2015.06.10.1 // @version 2015.06.10.1
// @downloadURL https://raw.githubusercontent.com/murdos/musicbrainz-userscripts/master/beatport_pro_importer.user.js // @downloadURL https://raw.githubusercontent.com/murdos/musicbrainz-userscripts/master/beatport_pro_importer.user.js
// @updateURL https://raw.githubusercontent.com/murdos/musicbrainz-userscripts/master/beatport_pro_importer.user.js // @updateURL https://raw.githubusercontent.com/murdos/musicbrainz-userscripts/master/beatport_pro_importer.user.js
// @include http://pro.beatport.com/release/* // @include http://pro.beatport.com/release/*
// @include https://pro.beatport.com/release/* // @include https://pro.beatport.com/release/*
// @require https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js // @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js
// @require https://raw.githubusercontent.com/murdos/musicbrainz-userscripts/master/lib/import_functions.js // @require lib/import_functions.js
// @require https://raw.github.com/murdos/musicbrainz-userscripts/master/lib/logger.js // @require lib/logger.js
// ==/UserScript== // ==/UserScript==
// prevent JQuery conflicts, see http://wiki.greasespot.net/@grant
this.$ = this.jQuery = jQuery.noConflict(true);
if (!unsafeWindow) unsafeWindow = window; if (!unsafeWindow) unsafeWindow = window;
$(document).ready(function(){ $(document).ready(function(){
var release_url = window.location.href.replace('/\?.*$/', '').replace(/#.*$/, '');
eval( $( "#data-objects" )[0].textContent ); var release = retrieveReleaseInfo(release_url);
var release = retrieveReleaseInfo(); insertLink(release, release_url);
insertLink(release);
}); });
function retrieveReleaseInfo() { function retrieveReleaseInfo(release_url) {
var release = {}; var releaseDate = $( ".category:contains('Release Date')" ).next().text().split("-");
// Release information global to all Beatport releases // Release information global to all Beatport releases
release.packaging = 'None'; var release = {
release.country = "XW"; artist_credit: [],
release.status = 'official'; title: $( "h3.interior-type:contains('Release')" ).next().text().trim(),
release.urls = []; year: releaseDate[0],
release.urls.push( { 'url': window.location.href } ); month: releaseDate[1],
release.id = $( "a[data-release]" ).attr('data-release'); day: releaseDate[2],
format: 'Digital Media',
packaging: 'None',
country: 'XW',
status: 'official',
language: 'eng',
script: 'Latn',
type: '',
urls: [],
labels: [],
discs: [],
};
release.title = $( "h3.interior-type:contains('Release')" ).next().text().trim(); var release_id = $( "span.playable-play-all[data-release]" ).attr('data-release');
var releaseDate = $( ".category:contains('Release Date')" ).next().text().split("-"); // URLs
release.year = releaseDate[0]; release.urls.push({
release.month = releaseDate[1]; 'url': release_url,
release.day = releaseDate[2]; 'link_type': MBReleaseImportHelper.URL_TYPES.purchase_for_download
});
release.labels = [];
release.labels.push( release.labels.push(
{ {
name: $( ".category:contains('Labels')" ).next().text().trim(), name: $( ".category:contains('Labels')" ).next().text().trim(),
@ -51,46 +63,69 @@ function retrieveReleaseInfo() {
// Tracks // Tracks
var tracks = []; var tracks = [];
var the_tracks = unsafeWindow.Playables.tracks;
window.Playables.tracks.forEach( var seen_tracks = {}; // to shoot duplicates ...
function ( j, index, arr ) { var release_artists = [];
if(j.release.id != release.id) { var total_duration = 0;
$.each(the_tracks,
function (idx, track) {
if (track.release.id != release_id) {
return; return;
} }
if (seen_tracks[track.id]) {
return;
}
seen_tracks[track.id] = true;
var artist = []; var artists = [];
j.artists.forEach( $.each(track.artists,
function ( a, index, arr ) { function (idx2, artist) {
artist.push({ artists.push(artist.name);
'artist_name': a.name release_artists.push(artist.name);
});
} }
); );
release.artist_credit = artist;
var title = track.name;
if (track.mix && track.mix != 'Original Mix') {
title += ' (' + track.mix + ')';
}
tracks.push({ tracks.push({
'artist_credit': artist, 'artist_credit': MBReleaseImportHelper.makeArtistCredits(artists),
'title': j.title, 'title': title,
'duration': j.duration.minutes 'duration': track.duration.minutes
}); });
total_duration += track.duration.milliseconds;
} }
); );
release.discs = [];
var unique_artists = [];
$.each(release_artists, function(i, el){
if ($.inArray(el, unique_artists) === -1) {
unique_artists.push(el);
}
});
if (unique_artists.length > 4) {
unique_artists = [ 'Various Artists' ];
}
release.artist_credit = MBReleaseImportHelper.makeArtistCredits(unique_artists);
release.discs.push( { release.discs.push( {
'tracks': tracks, 'tracks': tracks,
'format': "Digital Media" '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;
} }
// Insert button into page under label information // Insert button into page under label information
function insertLink(release) { function insertLink(release, release_url) {
var edit_note = 'Imported from ' + window.location.href; var edit_note = 'Imported from ' + release_url;
var parameters = MBReleaseImportHelper.buildFormParameters(release, edit_note); var parameters = MBReleaseImportHelper.buildFormParameters(release, edit_note);
var innerHTML = MBReleaseImportHelper.buildFormHTML(parameters); var innerHTML = MBReleaseImportHelper.buildFormHTML(parameters);
$(".interior-release-chart-content-list").append(innerHTML); $(".interior-release-chart-content-list").append('<li class="interior-release-chart-content-item musicbrainz-import">' + innerHTML + '</li>');
$('.musicbrainz-import input[type="submit"]').css('background', '#eee');
} }

View file

@ -61,6 +61,13 @@ var MBReleaseImportHelper = (function() {
// --------------------------------------- publics ----------------------------------------- // // --------------------------------------- publics ----------------------------------------- //
var url_types = {
purchase_for_download: 74,
download_for_free: 75,
stream_for_free: 85,
license: 301
}
// compute HTML of search link // compute HTML of search link
function fnBuildSearchLink(release) { function fnBuildSearchLink(release) {
@ -175,6 +182,40 @@ var MBReleaseImportHelper = (function() {
return parameters; return parameters;
} }
// Convert a list of artists to a list of artist credits with joinphrases
function fnArtistCredits(artists_list) {
var artists = artists_list.map(function(item) { return {artist_name: item}; });
if (artists.length > 2) {
var last = artists.pop();
last.joinphrase = '';
var prev = artists.pop();
prev.joinphrase = ' & ';
for (var i = 0; i < artists.length; i++) {
artists[i].joinphrase = ', ';
}
artists.push(prev);
artists.push(last);
} else if (artists.length == 2) {
artists[0].joinphrase = ' & ';
}
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 '';
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';
return '';
}
// --------------------------------------- privates ----------------------------------------- // // --------------------------------------- privates ----------------------------------------- //
function appendParameter(parameters, paramName, paramValue) { function appendParameter(parameters, paramName, paramValue) {
@ -203,8 +244,11 @@ var MBReleaseImportHelper = (function() {
// ---------------------------------- expose publics here ------------------------------------ // // ---------------------------------- expose publics here ------------------------------------ //
return { return {
buildSearchLink: fnBuildSearchLink, buildSearchLink: fnBuildSearchLink,
buildFormHTML: fnBuildFormHTML, buildFormHTML: fnBuildFormHTML,
buildFormParameters: fnBuildFormParameters buildFormParameters: fnBuildFormParameters,
}; makeArtistCredits: fnArtistCredits,
guessReleaseType: fnGuessReleaseType,
URL_TYPES: url_types
};
})(); })();