musicbrainz-userscripts/juno_download_importer.user.js

177 lines
5.2 KiB
JavaScript
Raw Normal View History

2015-06-13 09:38:19 +00:00
// ==UserScript==
// @name Import Juno Download releases to MusicBrainz
// @namespace https://github.com/murdos/musicbrainz-userscripts/
// @description One-click importing of releases from junodownload.com/products pages into MusicBrainz
2020-03-19 14:36:20 +00:00
// @version 2020.3.19.1
2015-06-13 09:38:19 +00:00
// @downloadURL https://raw.githubusercontent.com/murdos/musicbrainz-userscripts/master/juno_download_importer.user.js
// @updateURL https://raw.githubusercontent.com/murdos/musicbrainz-userscripts/master/juno_download_importer.user.js
// @include http*://www.junodownload.com/products/*
// @include http*://secure.junodownload.com/products/*
// @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js
// @require lib/mbimport.js
2015-06-13 09:38:19 +00:00
// @require lib/logger.js
// @require lib/mbimportstyle.js
2015-07-10 12:28:43 +00:00
// @icon https://raw.githubusercontent.com/murdos/musicbrainz-userscripts/master/assets/images/Musicbrainz_import_logo.png
2015-06-13 09:38:19 +00:00
// ==/UserScript==
// prevent JQuery conflicts, see http://wiki.greasespot.net/@grant
this.$ = this.jQuery = jQuery.noConflict(true);
$(document).ready(function() {
MBImportStyle();
2020-03-19 14:36:20 +00:00
let releaseUrl = window.location.href.replace('/?.*$/', '').replace(/#.*$/, '');
let release = retrieveReleaseInfo(releaseUrl);
insertLink(release, releaseUrl);
2015-06-13 09:38:19 +00:00
});
function parseReleaseDate(rdate) {
let months = {
January: 1,
February: 2,
March: 3,
April: 4,
May: 5,
June: 6,
July: 7,
August: 8,
September: 9,
October: 10,
November: 11,
December: 12
};
let m = rdate.match(/(\d{1,2}) ([a-z]+), (\d{4})/i);
if (m) {
return {
year: m[3],
month: months[m[2]],
day: m[1]
};
2015-06-13 09:38:19 +00:00
}
2020-03-19 14:36:20 +00:00
return false;
2015-06-13 09:38:19 +00:00
}
function retrieveReleaseInfo(release_url) {
let release = {
artist_credit: [],
2020-03-19 14:36:20 +00:00
title: $('meta[itemProp="name"]')
.attr('content')
.trim(),
year: 0,
month: 0,
day: 0,
format: 'Digital Media',
packaging: 'None',
country: 'XW',
status: 'official',
language: 'eng',
script: 'Latn',
type: '',
urls: [],
labels: [],
discs: []
};
2020-03-19 14:36:20 +00:00
let releaseDate = parseReleaseDate(
$('span[itemProp="datePublished"]')
.text()
.trim()
);
2020-03-19 14:36:20 +00:00
if (releaseDate) {
release.year = releaseDate.year;
release.month = releaseDate.month;
release.day = releaseDate.day;
2015-06-13 09:38:19 +00:00
}
release.urls.push({
url: release_url,
link_type: MBImport.URL_TYPES.purchase_for_download
});
release.labels.push({
2020-03-19 14:36:20 +00:00
name: $('meta[itemProp="author"]')
.attr('content')
.trim(),
2020-03-19 14:36:20 +00:00
catno: $('strong:contains("Cat:")')
.parent()
.contents()
.slice(2, 3)
.text()
.trim()
});
let tracks = [];
2020-03-19 14:36:20 +00:00
$('.product-tracklist-track[itemprop="track"]').each(function() {
// element only present if VA release or track has multiple artists
let artist = $(this)
.find('meta[itemprop="byArtist"]')
.attr('content');
if (artist !== undefined) {
artist = artist.trim();
}
let trackname = $(this)
2020-03-19 14:36:20 +00:00
.find('span[itemprop="name"]')
.text()
.trim();
let tracklength = $(this)
2020-03-19 14:36:20 +00:00
.find('meta[itemprop="duration"]')
.parent()
.contents()
.slice(0, 1)
.text()
.trim();
2020-03-19 14:36:20 +00:00
if (artist !== undefined && trackname.startsWith(`${artist} - `)) {
trackname = trackname.replace(`${artist} - `, '');
}
tracks.push({
2020-03-19 14:36:20 +00:00
artist_credit: MBImport.makeArtistCredits(artist === undefined ? [] : [artist]),
title: trackname,
duration: tracklength
});
});
2020-03-19 14:36:20 +00:00
let releaseArtists = $('.product-artist')
.contents()
.map(function() {
if (this.nodeType === Node.TEXT_NODE) {
return this.nodeValue === ' / ' ? null : this.nodeValue;
} else {
return $(this).text();
}
})
.get();
if (releaseArtists.length === 1 && releaseArtists[0] === 'VARIOUS') {
release.artist_credit = [MBImport.specialArtist('various_artists')];
} else {
2020-03-19 14:36:20 +00:00
release.artist_credit = MBImport.makeArtistCredits(releaseArtists);
2015-06-13 09:38:19 +00:00
}
2020-03-19 14:36:20 +00:00
release.discs.push({
tracks: tracks,
format: release.format
});
2015-06-13 09:38:19 +00:00
LOGGER.info('Parsed release: ', release);
return release;
2015-06-13 09:38:19 +00:00
}
2020-03-19 14:36:20 +00:00
function insertLink(release, releaseUrl) {
let editNote = MBImport.makeEditNote(releaseUrl, 'Juno Download');
let parameters = MBImport.buildFormParameters(release, editNote);
2015-06-13 09:38:19 +00:00
2020-03-19 14:36:20 +00:00
let mbUI = $(
`<div class="col-12 col-lg-9 mt-3"><div id="mb_buttons">${MBImport.buildFormHTML(parameters)}${MBImport.buildSearchButton(
release
)}</div></div>`
).hide();
2020-03-19 14:36:20 +00:00
$('.product-share')
.parent()
.after(mbUI);
$('#mb_buttons form').css({ display: 'inline', 'margin-right': '5px' });
mbUI.slideDown();
2015-06-13 09:38:19 +00:00
}