forked from murdos/musicbrainz-userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbandcamp_importer.user.js
319 lines (284 loc) · 10.8 KB
/
bandcamp_importer.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// ==UserScript==
// @name Import Bandcamp releases to MusicBrainz
// @description Add a button on Bandcamp's album pages to open MusicBrainz release editor with pre-filled data for the selected release
// @version 2018.01.14.0
// @namespace http://userscripts.org/users/22504
// @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
// @include /^https?://[^/]+/(?:album|track)/[^/]+$/
// @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js
// @require lib/mbimport.js
// @require lib/logger.js
// @require lib/mblinks.js
// @require lib/mbimportstyle.js
// @icon https://raw.githubusercontent.com/murdos/musicbrainz-userscripts/master/assets/images/Musicbrainz_import_logo.png
// ==/UserScript==
// prevent JQuery conflicts, see http://wiki.greasespot.net/@grant
this.$ = this.jQuery = jQuery.noConflict(true);
if (!unsafeWindow) unsafeWindow = window;
var BandcampImport = {
// Analyze Bandcamp data and return a release object
retrieveReleaseInfo: function () {
var bandcampAlbumData = unsafeWindow.TralbumData;
var bandcampEmbedData = unsafeWindow.EmbedData;
var release = {
discs: [],
artist_credit: [],
title: '',
year: 0,
month: 0,
day: 0,
parent_album_url: '',
labels: [],
format: 'Digital Media',
country: 'XW',
type: '',
status: 'official',
packaging: 'None',
language: 'eng',
script: 'Latn',
urls: [],
url: bandcampAlbumData.url
};
// Grab release title
release.title = bandcampAlbumData.current.title;
// Grab release event information
var date = this.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 = this.convdate(bandcampAlbumData.current.publish_date);
if (pdate) {
date = pdate;
}
}
release.year = date.year;
release.month = date.month;
release.day = date.day;
}
// FIXME: implement a mapping between bandcamp release types and MB ones
if (bandcampAlbumData.current.type == "track") {
// map Bandcamp single tracks to singles
release.type = "single";
// 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
}
}
// Tracks
var disc = {
tracks: [],
format: release.format
};
release.discs.push(disc);
// attempt to detect multiple artists tracks
// bandcamp formats them as 'artist - tracktitle'
// only set to true if ALL tracks are formatted like this
// and if string doesn't start with a number (ie. 02 - title)
var various_artists = true;
for (var i=0; i < bandcampAlbumData.trackinfo.length; i++) {
if (!bandcampAlbumData.trackinfo[i].title.match(/ - /)
|| bandcampAlbumData.trackinfo[i].title.match(/^\d+ - /)) {
various_artists = false;
break;
}
}
// Release artist credit
if (bandcampAlbumData.artist.match(/^various(?: artists)?$/i)
&& various_artists) {
release.artist_credit = [ MBImport.specialArtist('various_artists') ];
} else {
release.artist_credit = MBImport.makeArtistCredits([bandcampAlbumData.artist]);
}
$.each(bandcampAlbumData.trackinfo, function (index, bctrack) {
var title = bctrack.title;
var artist = [];
if (various_artists) {
var m = bctrack.title.match(/^(.+) - (.+)$/);
if (m) {
title = m[2];
artist = [m[1]];
}
}
var track = {
'title': title,
'duration': Math.round(bctrack.duration * 1000),
'artist_credit': MBImport.makeArtistCredits(artist)
};
disc.tracks.push(track);
});
// 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) {
numtracks = parseInt(match, 10);
}
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;
}
// URLs
var link_type = MBImport.URL_TYPES;
// Download for free vs. for purchase
if (bandcampAlbumData.current.download_pref !== null) {
if (bandcampAlbumData.freeDownloadPage !== null || bandcampAlbumData.current.download_pref === 1 || (
bandcampAlbumData.current.download_pref === 2 && bandcampAlbumData.current.minimum_price === 0)) {
release.urls.push({
'url': release.url,
'link_type': link_type.download_for_free
});
}
if (bandcampAlbumData.current.download_pref === 2) {
release.urls.push({
'url': release.url,
'link_type': link_type.purchase_for_download
});
}
}
// Check if the release is streamable
if (bandcampAlbumData.hasAudio && !nostream) {
release.urls.push({
'url': release.url,
'link_type': link_type.stream_for_free
});
}
// Check if release is Creative Commons licensed
if ($("div#license a.cc-icons").length > 0) {
release.urls.push({
'url': $("div#license a.cc-icons").attr("href"),
'link_type': link_type.license
});
}
// Check if album has a back link to a label
var label = $("a.back-to-label-link span.back-to-label-name").text();
if (label) {
release.labels.push({
'name': label,
'mbid': '',
'catno': 'none'
});
}
return release;
},
// Insert links in page
insertLink: function (release) {
if (release.type == "track") {
// only import album or single, tracks belong to an album
return false;
}
// Form parameters
var edit_note = MBImport.makeEditNote(release.url, 'Bandcamp');
var parameters = MBImport.buildFormParameters(release, edit_note);
// Build form
var mbUI = $('<div id="mb_buttons">'
+ MBImport.buildFormHTML(parameters)
+ MBImport.buildSearchButton(release)
+ '</div>').hide();
// Append MB import link
$('#name-section').append(mbUI);
$('#mb_buttons').css({'margin-top': '6px'});
$('form.musicbrainz_import').css({display: 'inline-block'});
mbUI.slideDown();
},
// 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 () {
/* keep the following line as first, it is required to skip
* pages which aren't actually a bandcamp page, since we support
* bandcamp pages under third-party domains.
* see @include
*/
if (!unsafeWindow.TralbumData) return;
/***/
MBImportStyle();
var mblinks = new MBLinks('BCI_MBLINKS_CACHE');
var release = BandcampImport.retrieveReleaseInfo();
// add MB artist link
var root_url = release.url.match(/^(https?:\/\/[^\/]+)/)[1].split('?')[0];
var label_url = "";
mblinks.searchAndDisplayMbLink(root_url, 'artist', function (link) { $('div#name-section span[itemprop="byArtist"]').before(link); } );
mblinks.searchAndDisplayMbLink(root_url, 'label', function (link) { $('p#band-name-location span.title').append(link); }, 'label:' + root_url );
var labelback = $("a.back-to-label-link");
if (labelback) {
var labelbacklink = labelback.attr('href');
if (labelbacklink) {
label_url = labelbacklink.match(/^(https?:\/\/[^\/]+)/)[1].split('?')[0];
mblinks.searchAndDisplayMbLink(label_url, 'label', function (link) { $('a.back-to-label-link span.back-link-text').append(link); }, 'label:' + label_url );
}
}
if (release.artist_credit.length == 1) {
// try to get artist's mbid from cache
var artist_mbid = mblinks.resolveMBID(root_url);
if (artist_mbid) {
release.artist_credit[0].mbid = artist_mbid;
}
}
// try to get label mbid from cache
var label_mbid = "";
var label_name = "";
if (label_url) {
label_mbid = mblinks.resolveMBID('label:' + label_url);
label_name = $('a.back-to-label-link span.back-link-text ').contents().get(2).textContent;
} else {
label_mbid = mblinks.resolveMBID('label:' + root_url);
if (label_mbid) label_name = $('p#band-name-location span.title').text().trim();
}
if (label_mbid || label_name) {
if (release.labels.length == 0) {
release.labels.push({
'name': '',
'mbid': '',
'catno': 'none'
});
}
release.labels[0].name = label_name;
release.labels[0].mbid = label_mbid;
}
BandcampImport.insertLink(release);
LOGGER.info("Parsed release: ", release);
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); } );
} else {
// add MB release links to album or single
mblinks.searchAndDisplayMbLink(release.url, 'release', function (link) { $('div#name-section span[itemprop="byArtist"]').after(link); } );
}
// append a comma after each tag to ease cut'n'paste to MB
$("div.tralbum-tags a:not(:last-child).tag").after(", ");
// append a link to the full size image
var fullsizeimageurl = $("div#tralbumArt a").attr("href").replace('_10', '_0');
$("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' });
});