diff --git a/discogs_importer.user.js b/discogs_importer.user.js
index 81ad84a..10cba88 100644
--- a/discogs_importer.user.js
+++ b/discogs_importer.user.js
@@ -1,593 +1,962 @@
// ==UserScript==
+
// @name Import Discogs releases to MusicBrainz
-// @version 2011-05-24_02
+
+// @version 2011-05-25_01
+
// @namespace http://userscripts.org/users/22504
+
// @include http://*musicbrainz.org/release/add
+
// @include http://*musicbrainz.org/release/*/add
+
// @include http://*musicbrainz.org/release/*/edit
+
// @include http://*.discogs.com/*release/*
+
// @exclude http://*.discogs.com/*release/*?f=xml*
+
// @exclude http://www.discogs.com/release/add
+
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js
+
// ==/UserScript==
+
+
// Script Update Checker
+
// -- http://userscripts.org/scripts/show/20145
+
var version_scriptNum = 36376; // Change this to the number given to the script by userscripts.org (check the address bar)
-var version_timestamp = 1306238657649; // Used to differentiate one version of the script from an older one. Use the (new Date()).getTime() function to get a value for this.
+
+var version_timestamp = 1306279616598; // Used to differentiate one version of the script from an older one. Use the (new Date()).getTime() function to get a value for this.
+
try {
+
function updateCheck(forced) {if((forced)||(parseInt(GM_getValue("lastUpdate", "0")) + 86400000 <= (new Date().getTime()))) {try {GM_xmlhttpRequest({method: "GET",url: "http://userscripts.org/scripts/review/" + version_scriptNum + "?" + new Date().getTime(),headers: {'Cache-Control': 'no-cache'},onload: function(xhrResponse) {GM_setValue("lastUpdate", new Date().getTime() + ""); var rt = xhrResponse.responseText.replace(/ ?/gm, " ").replace(/
';
+
innerHTML += '
Use the following fields to search for a Discogs release.
';
+
innerHTML += '';
+
innerHTML += '
Searching...
';
+
innerHTML += '
';
+
innerHTML += '
';
- $add_disc_dialog.find('div.add-disc-tab:last').after(innerHTML);
+
+ //$add_disc_dialog.find('div.add-disc-tab:last').after(innerHTML);
+
+
// On Discogs website
+
} else {
+
// Discogs Webservice URL
+
var discogsWsUrl = window.location.href.replace(/http:\/\/(www\.|)discogs\.com\/(.*\/|)release\//, 'http://discogs.com/release/') + "?f=xml&api_key=" + discogsApiKey;
+
mylog(discogsWsUrl);
+
+
/* Main function */
+
+
GM_xmlhttpRequest({
+
method: "GET",
+
url: discogsWsUrl,
+
headers: {
+
"User-Agent":"monkeyagent",
+
"Accept":"text/monkey,text/xml",
+
},
+
onload: function(response) {
+
var xmldoc = new DOMParser().parseFromString(response.responseText,"text/xml");
+
var release = parseDiscogsRelease(xmldoc);
+
insertLink(release);
+
}
+
});
+
}
+
+
});
+
+
+
+
// Analyze Discogs data and return a release object
+
function parseDiscogsRelease(xmldoc) {
+
var release = new Object();
+
release.discs = [];
+
+
// Compute artist(s) name(s)
- release.artist = cookArtistName(getXPathVal(xmldoc, "//release/artists/artist/*[name()='name' or name()='join']", false));
- release.artist = release.artist.replace(/ \(\d+\)$/, "");
+
+ release.artist_credits = new Array();
+ $(xmldoc).find("release artists artist").each(function() {
+ var $artist = $(this);
+ var ac = { name: $artist.find("name").text(), anv: $artist.find("anv").text(), join: " " + $artist.find("join").text() + " "};
+ //ac.name = ac.name.replace(/ \(\d+\)$/, "");
+ release.artist_credits.push(ac);
+ });
+
+
// Grab release title
+
release.title = getXPathVal(xmldoc, "//release/title", true);
+
+
// Grab release event information
+
var releasedate = getXPathVal(xmldoc, "//release/released", true);
+
if (typeof releasedate != "undefined" && releasedate != "") {
+
var tmp = releasedate.split('-'); if (tmp[0] != "undefined" && tmp[0] != "") {
+
+
release.year = parseInt(tmp[0], 10);
-
+
+
if (tmp[1] != "undefined" && tmp[1] != "") {
+
release.month = parseInt(tmp[1], 10);
+
if (tmp[2] != "undefined" && tmp[2] != "") {
+
release.day = parseInt(tmp[2], 10);
+
}
+
}
+
}
- }
- release.label = getXPathVal(xmldoc, "//release/labels/label/@name", true);
- release.catno = getXPathVal(xmldoc, "//release/labels/label/@catno", true);
+ }
+
+ release.labels = new Array();
+ $(xmldoc).find("release labels label").each(function() {
+ release.labels.push( { name: $(this).attr('name'), catno: $(this).attr('catno') } );
+ });
+
release.format = MediaTypes[getXPathVal(xmldoc, "//release/formats/format/@name", true)];
+ // Special handle of vinyl 7", 10" and 12"
+ $(xmldoc).find("release formats descriptions description").each(function() {
+ var desc = $(this).text();
+ if (desc.match(/7"|10"|12"/)) release.format = MediaTypes[desc];
+ });
+
+
release.country = Countries[ getXPathVal(xmldoc, "//release/country", true) ];
+
+
// Grab tracks
+
var tracks = [];
+
var trackNodes = getXPathVal(xmldoc, "//tracklist/track", false);
+
+
for (var i = 0; i < trackNodes.snapshotLength; i++) {
+
var track = new Object();
+
var trackNode = trackNodes.snapshotItem(i);
+
+
track.title = trackNode.getElementsByTagName("title").item(0).textContent;
+
track.duration = trackNode.getElementsByTagName("duration").item(0).textContent;
+
+
// Track artist
+
var trackArtist = cookArtistName(getXPathVal(xmldoc, ".//artists//*[name()='name' or name()='join']", false, trackNode));
+
trackArtist = trackArtist.replace(/ \(\d+\)$/, "");
+
+
if (trackArtist != "")
+
track.artist = trackArtist;
+
+
// Track position and release number
+
var trackPosition = trackNode.getElementsByTagName("position").item(0).textContent;
+
var releaseNumber = 1;
+
+
// Skip special tracks
+
if (trackPosition.toLowerCase().match("^(video|mp3)")) {
+
trackPosition = "";
+
}
+
+
// Remove "CD" prefix
+
trackPosition = trackPosition.replace(/^CD/i, "");
+
// Multi discs e.g. 1.1 or 1-1
+
var tmp = trackPosition.match(/^(\d+)(?=(-|\.)\d*)/);
+
if (tmp && tmp[0]) {
+
releaseNumber = tmp[0];
+
} else {
+
// Vinyls disc numbering: A1, B3, ...
+
tmp = trackPosition.match(/^([A-Za-z])\d*/);
+
if (tmp && tmp[0] && tmp[0] != "V") {
+
var code = tmp[0].charCodeAt(0);
+
// A-Z
+
if (65 <= code && code <= 90) {
+
code = code - 65;
+
} else if (97 <= code && code <= 122) {
+
// a-z
+
code = code - (65 + 32);
+
}
+
releaseNumber = (code-code%2)/2+1;
}
+
}
+
+
// Create release if needed
+
if( !release.discs[releaseNumber-1] ) {
+
release.discs.push(new Object());
+
release.discs[releaseNumber-1].tracks = [];
+
}
+
+
// Trackposition is empty e.g. for release title
+
if (trackPosition != "" && trackPosition != null)
+
release.discs[releaseNumber-1].tracks.push(track);
+
}
+
mylog(release);
+
return release;
+
}
+
+
// Insert links in Discogs page
+
function insertLink(release) {
+
+
var mbUI = document.createElement('div');
+
mbUI.innerHTML = "