forked from murdos/musicbrainz-userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bandcamp_importer.user.js
180 lines (151 loc) · 5.52 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
// ==UserScript==
// @name Import Bandcamp releases into MB
// @version 2014.10.18.1
// @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 http*://*.bandcamp.com/album/*
// @include http*://*.bandcamp.com/track/*
// @require https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js
// @require https://raw.github.com/murdos/musicbrainz-userscripts/master/lib/import_functions.js
// ==/UserScript==
if (!unsafeWindow) unsafeWindow = window;
$(document).ready(function(){
var release = retrieveReleaseInfo();
insertLink(release);
});
// Analyze Bandcamp data and return a release object
function retrieveReleaseInfo() {
var release = new Object();
release.discs = [];
var bandcampAlbumData = unsafeWindow.TralbumData;
var bandcampEmbedData = unsafeWindow.EmbedData;
// Release artist credit
release.artist_credit = [ { artist_name: bandcampAlbumData.artist } ];
// Grab release title
release.title = bandcampAlbumData.current.title;
// Grab release event information
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
}
function convdate(date) {
if (typeof date != "undefined" && date != "") {
var d = new Date(date);
return {
"year": d.getUTCFullYear(),
"month": d.getUTCMonth() + 1,
"day": d.getUTCDate()
}
}
return false;
}
if(bandcampEmbedData.album_title) {
release.parent_album = bandcampEmbedData.album_title;
}
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';
release.packaging = 'none';
release.language = 'eng';
release.script = 'Latn';
// map Bandcamp single tracks to singles
if(release.type == "track")
{ release.type = "single"; }
// Tracks
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,
'duration': Math.round(bctrack.duration*1000),
'artist_credit': []
}
disc.tracks.push(track);
});
// URLs
// link_type mapping:
// - 74: purchase for download
// - 75: download for free
// - 85: stream {video} for free
// - 301: license
LINK_PURCHASE_FOR_DOWNLOAD = 74
LINK_DOWNLOAD_FOR_FREE = 75
LINK_STREAM_FOR_FREE = 85
LINK_LICENSE = 301
release.urls = new Array();
// 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': window.location.href,
'link_type': LINK_DOWNLOAD_FOR_FREE
});
}
if (bandcampAlbumData.current.download_pref === 2) {
// - 74: purchase for download
release.urls.push({
'url': window.location.href,
'link_type': LINK_PURCHASE_FOR_DOWNLOAD
});
}
}
// Check if the release is streamable
if (bandcampAlbumData.hasAudio) {
release.urls.push({
'url': window.location.href,
'link_type': LINK_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_LICENSE
});
}
mylog(release);
return release;
}
// Insert links in page
function insertLink(release) {
if(release.type == "single" && typeof release.parent_album != "undefined") {
mylog("This is part of an album, not continuing.");
return false;
}
/*
var mbUI = document.createElement('div');
mbUI.innerHTML = "<h3>MusicBrainz</h3>";
mbUI.className = "section";
var mbContentBlock = document.createElement('div');
mbContentBlock.className = "section_content";
mbUI.appendChild(mbContentBlock);
*/
// Form parameters
var edit_note = 'Imported from ' + window.location.href;
var parameters = MBReleaseImportHelper.buildFormParameters(release, edit_note);
// Build form
var innerHTML = MBReleaseImportHelper.buildFormHTML(parameters);
// Append search link
//innerHTML += ' <small>(' + MBReleaseImportHelper.buildSearchLink(release) + ')</small>';
$('h2.trackTitle').append(innerHTML);
}
function mylog(obj) {
var DEBUG = true;
if (DEBUG && unsafeWindow.console) {
unsafeWindow.console.log(obj);
}
}