2012-03-29 00:49:43 +02:00
// ==UserScript==
// @name Import Bandcamp releases into MB
2015-05-20 14:01:35 +02:00
// @version 2015.05.20.0
2012-03-29 00:49:43 +02:00
// @namespace http://userscripts.org/users/22504
2014-02-22 10:33:26 +01: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 14:20:19 +01:00
// @include http*://*.bandcamp.com/album/*
// @include http*://*.bandcamp.com/track/*
2013-03-04 11:38:13 +01:00
// @require https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js
2012-03-29 00:51:26 +02:00
// @require https://raw.github.com/murdos/musicbrainz-userscripts/master/lib/import_functions.js
2015-01-18 14:13:32 +01:00
// @require https://raw.github.com/murdos/musicbrainz-userscripts/master/lib/logger.js
2012-03-29 00:49:43 +02:00
// ==/UserScript==
2012-03-29 00:50:21 +02:00
if ( ! unsafeWindow ) unsafeWindow = window ;
2012-03-29 00:49:43 +02:00
$ ( document ) . ready ( function ( ) {
var release = retrieveReleaseInfo ( ) ;
insertLink ( release ) ;
2015-05-16 14:39:51 +02:00
// append a comma after each tag to ease cut'n'paste to MB
$ ( "div.tralbum-tags a:not(:last-child)" ) . after ( "," ) ;
2015-05-16 14:41:23 +02:00
// append a link to the full size image
fullsizeimageurl = $ ( "div#tralbumArt a" ) . attr ( "href" ) . replace ( '_10' , '_0' ) ;
$ ( "div#tralbumArt a" ) . after ( "<div id='bci_link'><a class='custom-color' href='" + fullsizeimageurl + "' title='Link to the original image (Bandcamp importer)'>Original image</a></div>" ) ;
2012-03-29 00:49:43 +02:00
} ) ;
// Analyze Bandcamp data and return a release object
function retrieveReleaseInfo ( ) {
var release = new Object ( ) ;
2014-02-21 18:46:29 +01:00
release . discs = [ ] ;
2012-03-29 00:49:43 +02:00
var bandcampAlbumData = unsafeWindow . TralbumData ;
2014-02-21 19:21:53 +01:00
var bandcampEmbedData = unsafeWindow . EmbedData ;
2012-03-29 00:49:43 +02:00
// Release artist credit
release . artist _credit = [ { artist _name : bandcampAlbumData . artist } ] ;
2014-02-21 18:46:29 +01:00
// Grab release title
release . title = bandcampAlbumData . current . title ;
2012-03-29 00:49:43 +02:00
// Grab release event information
2014-10-08 23:24:41 +02:00
var date = convdate ( bandcampAlbumData . current . release _date ) ;
if ( date ) {
if ( ! ( date . year > 2008 || ( date . year == 2008 && date . month >= 9 ) ) ) {
// use publish date if release date is before Bandcamp launch (2008-09)
var pdate = convdate ( bandcampAlbumData . current . publish _date ) ;
if ( pdate ) date = pdate ;
}
release . year = date . year
release . month = date . month
release . day = date . day
}
2012-03-29 00:49:43 +02:00
2014-10-08 23:24:41 +02:00
function convdate ( date ) {
if ( typeof date != "undefined" && date != "" ) {
var d = new Date ( date ) ;
return {
"year" : d . getUTCFullYear ( ) ,
2014-10-18 14:33:07 +02:00
"month" : d . getUTCMonth ( ) + 1 ,
2014-10-08 23:24:41 +02:00
"day" : d . getUTCDate ( )
}
}
return false ;
2012-12-12 04:15:06 -07:00
}
2012-12-12 03:48:28 -07:00
2014-10-08 23:24:41 +02:00
if ( bandcampEmbedData . album _title ) {
release . parent _album = bandcampEmbedData . album _title ;
2014-02-21 18:42:25 +01:00
}
2012-03-29 00:49:43 +02:00
release . labels = new Array ( ) ;
release . format = "Digital Media" ;
release . country = "XW" ; // Worldwide
// FIXME: implement a mapping between bandcamp release types and MB ones
release . type = bandcampAlbumData . current . type ;
release . status = 'official' ;
2014-09-26 11:47:44 +02:00
release . packaging = 'none' ;
release . language = 'eng' ;
release . script = 'Latn' ;
2012-03-29 00:49:43 +02:00
2014-02-21 19:21:53 +01:00
// map Bandcamp single tracks to singles
if ( release . type == "track" )
{ release . type = "single" ; }
2012-12-12 03:48:28 -07:00
2014-02-21 18:46:29 +01:00
// Tracks
2012-03-29 00:49:43 +02:00
var disc = new Object ( ) ;
disc . tracks = new Array ( ) ;
disc . format = release . format ;
release . discs . push ( disc ) ;
$ . each ( bandcampAlbumData . trackinfo , function ( index , bctrack ) {
var track = {
'title' : bctrack . title ,
2012-03-29 00:50:21 +02:00
'duration' : Math . round ( bctrack . duration * 1000 ) ,
2012-03-29 00:49:43 +02:00
'artist_credit' : [ ]
}
disc . tracks . push ( track ) ;
} ) ;
2014-02-21 20:05:25 +01:00
// URLs
// link_type mapping:
// - 74: purchase for download
// - 75: download for free
// - 85: stream {video} for free
2014-02-21 20:05:25 +01:00
// - 301: license
2014-09-26 11:51:15 +02:00
LINK _PURCHASE _FOR _DOWNLOAD = 74
LINK _DOWNLOAD _FOR _FREE = 75
LINK _STREAM _FOR _FREE = 85
LINK _LICENSE = 301
2014-02-21 20:05:25 +01:00
release . urls = new Array ( ) ;
// Download for free vs. for purchase
if ( bandcampAlbumData . current . download _pref !== null ) {
2014-09-26 11:51:15 +02:00
if ( bandcampAlbumData . freeDownloadPage !== null || bandcampAlbumData . current . download _pref === 1 || (
bandcampAlbumData . current . download _pref === 2 && bandcampAlbumData . current . minimum _price === 0 ) ) {
release . urls . push ( {
'url' : window . location . href ,
'link_type' : LINK _DOWNLOAD _FOR _FREE
} ) ;
2014-02-21 20:05:25 +01:00
}
2014-09-26 11:51:15 +02:00
if ( bandcampAlbumData . current . download _pref === 2 ) {
// - 74: purchase for download
release . urls . push ( {
'url' : window . location . href ,
'link_type' : LINK _PURCHASE _FOR _DOWNLOAD
} ) ;
2014-02-21 20:05:25 +01:00
}
}
// Check if the release is streamable
if ( bandcampAlbumData . hasAudio ) {
2014-09-26 11:51:15 +02:00
release . urls . push ( {
'url' : window . location . href ,
'link_type' : LINK _STREAM _FOR _FREE
} ) ;
2014-02-21 20:05:25 +01:00
}
2014-02-21 20:05:25 +01:00
// Check if release is Creative Commons licensed
if ( $ ( "div#license a.cc-icons" ) . length > 0 ) {
2014-09-26 11:51:15 +02:00
release . urls . push ( {
'url' : $ ( "div#license a.cc-icons" ) . attr ( "href" ) ,
'link_type' : LINK _LICENSE
} ) ;
2014-02-21 20:05:25 +01:00
}
2014-02-21 20:05:25 +01:00
2015-05-20 14:01:35 +02:00
// Check if album has a back link to a label
label = $ ( "a.back-to-label-link span.back-to-label-name" ) . text ( ) ;
if ( label != "" ) {
release . labels . push ( {
'name' : label ,
'mbid' : '' ,
'catno' : 'none'
} ) ;
}
2015-01-18 14:13:32 +01:00
LOGGER . info ( "Parsed release: " , release ) ;
2014-02-21 18:46:29 +01:00
return release ;
2012-03-29 00:49:43 +02:00
}
// Insert links in page
function insertLink ( release ) {
2014-02-21 18:46:29 +01:00
if ( release . type == "single" && typeof release . parent _album != "undefined" ) {
2015-01-18 14:13:32 +01:00
LOGGER . info ( "This is part of an album, not continuing." ) ;
2014-02-21 18:46:29 +01:00
return false ;
}
2012-12-12 03:48:28 -07:00
2012-03-29 00:49:43 +02:00
// Form parameters
var edit _note = 'Imported from ' + window . location . href ;
2014-02-21 18:46:29 +01:00
var parameters = MBReleaseImportHelper . buildFormParameters ( release , edit _note ) ;
2012-03-29 00:49:43 +02:00
2014-02-21 18:46:29 +01:00
// Build form
var innerHTML = MBReleaseImportHelper . buildFormHTML ( parameters ) ;
2012-03-29 00:49:43 +02:00
// Append search link
2014-02-21 18:46:29 +01:00
//innerHTML += ' <small>(' + MBReleaseImportHelper.buildSearchLink(release) + ')</small>';
2012-03-29 00:49:43 +02:00
$ ( 'h2.trackTitle' ) . append ( innerHTML ) ;
}