2012-03-28 22:49:43 +00:00
|
|
|
// ==UserScript==
|
2015-06-10 12:59:12 +00:00
|
|
|
// @name Import Bandcamp releases to MusicBrainz
|
2015-05-28 08:26:38 +00:00
|
|
|
// @description Add a button on Bandcamp's album pages to open MusicBrainz release editor with pre-filled data for the selected release
|
2015-06-12 22:35:12 +00:00
|
|
|
// @version 2015.06.12.0
|
2012-03-28 22:49:43 +00:00
|
|
|
// @namespace http://userscripts.org/users/22504
|
2014-02-22 09:33:26 +00:00
|
|
|
// @downloadURL https://raw.github.com/murdos/musicbrainz-userscripts/master/bandcamp_importer.user.js
|
|
|
|
// @updateURL https://raw.github.com/murdos/musicbrainz-userscripts/master/bandcamp_importer.user.js
|
2014-01-02 13:20:19 +00:00
|
|
|
// @include http*://*.bandcamp.com/album/*
|
|
|
|
// @include http*://*.bandcamp.com/track/*
|
2015-06-05 08:39:00 +00:00
|
|
|
// @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js
|
2015-06-05 09:13:42 +00:00
|
|
|
// @require lib/import_functions.js
|
|
|
|
// @require lib/logger.js
|
|
|
|
// @require lib/mblinks.js
|
2012-03-28 22:49:43 +00:00
|
|
|
// ==/UserScript==
|
|
|
|
|
|
|
|
|
2015-06-05 08:39:00 +00:00
|
|
|
// prevent JQuery conflicts, see http://wiki.greasespot.net/@grant
|
|
|
|
this.$ = this.jQuery = jQuery.noConflict(true);
|
|
|
|
|
2015-05-25 16:13:40 +00:00
|
|
|
if (!unsafeWindow) unsafeWindow = window;
|
2015-05-16 12:41:23 +00:00
|
|
|
|
2015-05-25 16:13:40 +00:00
|
|
|
var BandcampImport = {
|
2012-03-28 22:49:43 +00:00
|
|
|
|
2015-05-25 16:13:40 +00:00
|
|
|
// Analyze Bandcamp data and return a release object
|
|
|
|
retrieveReleaseInfo: function () {
|
2012-03-28 22:49:43 +00:00
|
|
|
|
|
|
|
var bandcampAlbumData = unsafeWindow.TralbumData;
|
2014-02-21 18:21:53 +00:00
|
|
|
var bandcampEmbedData = unsafeWindow.EmbedData;
|
2012-03-28 22:49:43 +00:00
|
|
|
|
2015-05-25 16:13:40 +00:00
|
|
|
var release = {
|
|
|
|
discs: [],
|
|
|
|
artist_credit: [],
|
|
|
|
title: '',
|
|
|
|
year: 0,
|
|
|
|
month: 0,
|
|
|
|
day: 0,
|
2015-06-06 23:26:56 +00:00
|
|
|
parent_album_url: '',
|
2015-05-25 16:13:40 +00:00
|
|
|
labels: [],
|
|
|
|
format: 'Digital Media',
|
|
|
|
country: 'XW',
|
|
|
|
type: '',
|
|
|
|
status: 'official',
|
|
|
|
packaging: 'None',
|
|
|
|
language: 'eng',
|
|
|
|
script: 'Latn',
|
2015-06-06 22:30:25 +00:00
|
|
|
urls: [],
|
|
|
|
url: bandcampAlbumData.url
|
2015-05-25 16:13:40 +00:00
|
|
|
};
|
|
|
|
|
2012-03-28 22:49:43 +00:00
|
|
|
// Release artist credit
|
2015-05-25 16:13:40 +00:00
|
|
|
release.artist_credit = [{
|
|
|
|
artist_name: bandcampAlbumData.artist
|
|
|
|
}];
|
2012-03-28 22:49:43 +00:00
|
|
|
|
2014-02-21 17:46:29 +00:00
|
|
|
// Grab release title
|
|
|
|
release.title = bandcampAlbumData.current.title;
|
2012-03-28 22:49:43 +00:00
|
|
|
|
|
|
|
// Grab release event information
|
2015-05-25 16:13:40 +00:00
|
|
|
var date = this.convdate(bandcampAlbumData.current.release_date);
|
2014-10-08 21:24:41 +00:00
|
|
|
if (date) {
|
|
|
|
if (!(date.year > 2008 || (date.year == 2008 && date.month >= 9))) {
|
|
|
|
// use publish date if release date is before Bandcamp launch (2008-09)
|
2015-05-25 16:13:40 +00:00
|
|
|
var pdate = this.convdate(bandcampAlbumData.current.publish_date);
|
|
|
|
if (pdate) {
|
|
|
|
date = pdate;
|
2014-10-08 21:24:41 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-25 16:13:40 +00:00
|
|
|
release.year = date.year;
|
|
|
|
release.month = date.month;
|
|
|
|
release.day = date.day;
|
2012-12-12 11:15:06 +00:00
|
|
|
}
|
2012-12-12 10:48:28 +00:00
|
|
|
|
2012-03-28 22:49:43 +00:00
|
|
|
// FIXME: implement a mapping between bandcamp release types and MB ones
|
2015-06-06 23:46:03 +00:00
|
|
|
if (bandcampAlbumData.current.type == "track") {
|
|
|
|
// map Bandcamp single tracks to singles
|
2015-05-25 16:13:40 +00:00
|
|
|
release.type = "single";
|
2015-06-06 23:46:03 +00:00
|
|
|
// if track belongs to an album, get its url.
|
|
|
|
if (bandcampEmbedData.album_embed_data) {
|
|
|
|
release.parent_album_url = bandcampEmbedData.album_embed_data.linkback;
|
|
|
|
release.type = 'track'; // <-- no import
|
|
|
|
}
|
2015-05-25 16:13:40 +00:00
|
|
|
}
|
2012-12-12 10:48:28 +00:00
|
|
|
|
2014-02-21 17:46:29 +00:00
|
|
|
// Tracks
|
2015-05-25 16:13:40 +00:00
|
|
|
var disc = {
|
|
|
|
tracks: [],
|
|
|
|
format: release.format
|
|
|
|
};
|
2012-03-28 22:49:43 +00:00
|
|
|
release.discs.push(disc);
|
2015-05-25 16:13:40 +00:00
|
|
|
$.each(bandcampAlbumData.trackinfo, function (index, bctrack) {
|
|
|
|
var track = {
|
|
|
|
'title': bctrack.title,
|
|
|
|
'duration': Math.round(bctrack.duration * 1000),
|
|
|
|
'artist_credit': []
|
|
|
|
};
|
|
|
|
disc.tracks.push(track);
|
2012-03-28 22:49:43 +00:00
|
|
|
});
|
|
|
|
|
2015-05-29 08:58:14 +00:00
|
|
|
// Check for hidden tracks (more tracks in the download than shown for streaming ie.)
|
|
|
|
var showntracks = bandcampAlbumData.trackinfo.length;
|
|
|
|
var numtracks = -1;
|
|
|
|
var nostream = false;
|
|
|
|
// album description indicates number of tracks in the download
|
|
|
|
var match = /^\d+ track album$/.exec($("meta[property='og:description']").attr("content"));
|
|
|
|
if (match) {
|
2015-06-10 22:21:39 +00:00
|
|
|
numtracks = parseInt(match, 10);
|
2015-05-29 08:58:14 +00:00
|
|
|
}
|
|
|
|
if (numtracks > 0 && numtracks > showntracks) {
|
|
|
|
// display a warning if tracks in download differs from tracks shown
|
|
|
|
$('h2.trackTitle').append(
|
|
|
|
'<p style="font-size:70%; font-style: italic; margin: 0.1em 0;">' +
|
|
|
|
'Warning: ' + numtracks + ' vs ' + showntracks + ' tracks' +
|
|
|
|
'</p>'
|
|
|
|
);
|
|
|
|
|
|
|
|
// append unknown tracks to the release
|
|
|
|
for (var i = 0; i < numtracks - showntracks; i++) {
|
|
|
|
var track = {
|
|
|
|
'title': '[unknown]',
|
|
|
|
'duration': null,
|
|
|
|
'artist_credit': []
|
|
|
|
};
|
|
|
|
disc.tracks.push(track);
|
|
|
|
}
|
|
|
|
// disable stream link as only part of the album can be streamed
|
|
|
|
nostream = true;
|
|
|
|
}
|
|
|
|
|
2014-02-21 19:05:25 +00:00
|
|
|
// URLs
|
2015-06-12 21:53:35 +00:00
|
|
|
var link_type = MBReleaseImportHelper.URL_TYPES;
|
2014-02-21 19:05:25 +00:00
|
|
|
// Download for free vs. for purchase
|
|
|
|
if (bandcampAlbumData.current.download_pref !== null) {
|
2015-05-25 16:13:40 +00:00
|
|
|
if (bandcampAlbumData.freeDownloadPage !== null || bandcampAlbumData.current.download_pref === 1 || (
|
|
|
|
bandcampAlbumData.current.download_pref === 2 && bandcampAlbumData.current.minimum_price === 0)) {
|
|
|
|
release.urls.push({
|
2015-06-06 22:30:25 +00:00
|
|
|
'url': release.url,
|
2015-05-25 16:13:40 +00:00
|
|
|
'link_type': link_type.download_for_free
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (bandcampAlbumData.current.download_pref === 2) {
|
|
|
|
release.urls.push({
|
2015-06-06 22:30:25 +00:00
|
|
|
'url': release.url,
|
2015-05-25 16:13:40 +00:00
|
|
|
'link_type': link_type.purchase_for_download
|
|
|
|
});
|
|
|
|
}
|
2014-02-21 19:05:25 +00:00
|
|
|
}
|
|
|
|
// Check if the release is streamable
|
2015-05-29 08:58:14 +00:00
|
|
|
if (bandcampAlbumData.hasAudio && !nostream) {
|
2015-05-25 16:13:40 +00:00
|
|
|
release.urls.push({
|
2015-06-06 22:30:25 +00:00
|
|
|
'url': release.url,
|
2015-05-25 16:13:40 +00:00
|
|
|
'link_type': link_type.stream_for_free
|
|
|
|
});
|
2014-02-21 19:05:25 +00:00
|
|
|
}
|
2014-02-21 19:05:25 +00:00
|
|
|
// Check if release is Creative Commons licensed
|
|
|
|
if ($("div#license a.cc-icons").length > 0) {
|
2015-05-25 16:13:40 +00:00
|
|
|
release.urls.push({
|
|
|
|
'url': $("div#license a.cc-icons").attr("href"),
|
|
|
|
'link_type': link_type.license
|
|
|
|
});
|
2014-02-21 19:05:25 +00:00
|
|
|
}
|
2015-05-20 12:01:35 +00:00
|
|
|
// Check if album has a back link to a label
|
2015-05-25 16:13:40 +00:00
|
|
|
var label = $("a.back-to-label-link span.back-to-label-name").text();
|
|
|
|
if (label) {
|
2015-05-20 12:01:35 +00:00
|
|
|
release.labels.push({
|
|
|
|
'name': label,
|
|
|
|
'mbid': '',
|
|
|
|
'catno': 'none'
|
|
|
|
});
|
|
|
|
}
|
2015-06-12 21:54:01 +00:00
|
|
|
|
2014-02-21 17:46:29 +00:00
|
|
|
return release;
|
2015-05-25 16:13:40 +00:00
|
|
|
},
|
2012-03-28 22:49:43 +00:00
|
|
|
|
2015-05-25 16:13:40 +00:00
|
|
|
// Insert links in page
|
|
|
|
insertLink: function (release) {
|
2015-06-06 23:46:03 +00:00
|
|
|
if (release.type == "track") {
|
|
|
|
// only import album or single, tracks belong to an album
|
2015-05-25 16:13:40 +00:00
|
|
|
return false;
|
2014-02-21 17:46:29 +00:00
|
|
|
}
|
2012-03-28 22:49:43 +00:00
|
|
|
// Form parameters
|
2015-06-06 22:30:25 +00:00
|
|
|
var edit_note = 'Imported from ' + release.url;
|
2014-02-21 17:46:29 +00:00
|
|
|
var parameters = MBReleaseImportHelper.buildFormParameters(release, edit_note);
|
|
|
|
// Build form
|
|
|
|
var innerHTML = MBReleaseImportHelper.buildFormHTML(parameters);
|
2015-05-25 16:13:40 +00:00
|
|
|
// Append MB import link
|
2012-03-28 22:49:43 +00:00
|
|
|
$('h2.trackTitle').append(innerHTML);
|
2015-05-25 16:13:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// helper to convert bandcamp date to MB date
|
|
|
|
convdate: function (date) {
|
|
|
|
if (typeof date != "undefined" && date !== "") {
|
|
|
|
var d = new Date(date);
|
|
|
|
return {
|
|
|
|
"year": d.getUTCFullYear(),
|
|
|
|
"month": d.getUTCMonth() + 1,
|
|
|
|
"day": d.getUTCDate()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$(document).ready(function () {
|
|
|
|
|
2015-06-05 08:39:00 +00:00
|
|
|
var mblinks = new MBLinks('BCI_MBLINKS_CACHE', 7*24*60); // force refresh of cached links once a week
|
|
|
|
|
2015-06-06 23:20:23 +00:00
|
|
|
var release = BandcampImport.retrieveReleaseInfo();
|
|
|
|
LOGGER.info("Parsed release: ", release);
|
|
|
|
BandcampImport.insertLink(release);
|
|
|
|
|
2015-06-06 23:22:49 +00:00
|
|
|
// add MB artist link
|
|
|
|
var artist_link = release.url.match(/^(http:\/\/[^\/]+)/)[1];
|
2015-06-05 08:39:00 +00:00
|
|
|
mblinks.searchAndDisplayMbLink(artist_link, 'artist', function (link) { $('div#name-section span[itemprop="byArtist"]').before(link); } );
|
|
|
|
|
2015-06-06 23:46:03 +00:00
|
|
|
if (release.type == 'track') {
|
|
|
|
// add MB links to parent album
|
|
|
|
mblinks.searchAndDisplayMbLink(release.parent_album_url, 'release', function (link) { $('div#name-section span[itemprop="inAlbum"] a:first').before(link); } );
|
2015-06-06 23:22:49 +00:00
|
|
|
} else {
|
2015-06-06 23:46:03 +00:00
|
|
|
// add MB release links to album or single
|
|
|
|
mblinks.searchAndDisplayMbLink(release.url, 'release', function (link) { $('div#name-section span[itemprop="byArtist"]').after(link); } );
|
2015-06-06 23:22:49 +00:00
|
|
|
}
|
2015-06-05 08:39:00 +00:00
|
|
|
|
2015-05-25 16:13:40 +00:00
|
|
|
// append a comma after each tag to ease cut'n'paste to MB
|
|
|
|
$("div.tralbum-tags a:not(:last-child)").after(",");
|
2012-03-28 22:49:43 +00:00
|
|
|
|
2015-05-25 16:13:40 +00:00
|
|
|
// append a link to the full size image
|
|
|
|
var fullsizeimageurl = $("div#tralbumArt a").attr("href").replace('_10', '_0');
|
2015-06-01 10:01:08 +00:00
|
|
|
$("div#tralbumArt").after("<div id='bci_link'><a class='custom-color' href='" + fullsizeimageurl +
|
|
|
|
"' title='Open original image in a new tab (Bandcamp importer)' target='_blank'>Original image</a></div>");
|
|
|
|
|
|
|
|
$("div#bci_link").css({ 'padding-top': '0.5em', 'text-align': 'right' });
|
|
|
|
$("div#bci_link a").css({ 'font-weight': 'bold' });
|
2015-05-25 16:13:40 +00:00
|
|
|
|
|
|
|
});
|