musicbrainz-userscripts/juno_download_importer.user.js

151 lines
4.9 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);
2020-04-05 14:01:21 +00:00
$(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,
2020-04-05 14:01:21 +00:00
December: 12,
};
let m = rdate.match(/(\d{1,2}) ([a-z]+), (\d{4})/i);
if (m) {
return {
year: m[3],
month: months[m[2]],
2020-04-05 14:01:21 +00:00
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-04-05 14:01:21 +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: [],
2020-04-05 14:01:21 +00:00
discs: [],
};
2020-04-05 14:01:21 +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,
2020-04-05 14:01:21 +00:00
link_type: MBImport.URL_TYPES.purchase_for_download,
});
release.labels.push({
2020-04-05 14:01:21 +00:00
name: $('meta[itemProp="author"]').attr('content').trim(),
catno: $('strong:contains("Cat:")').parent().contents().slice(2, 3).text().trim(),
});
let tracks = [];
2020-04-05 14:01:21 +00:00
$('.product-tracklist-track[itemprop="track"]').each(function () {
2020-03-19 14:36:20 +00:00
// element only present if VA release or track has multiple artists
2020-04-05 14:01:21 +00:00
let artist = $(this).find('meta[itemprop="byArtist"]').attr('content');
2020-03-19 14:36:20 +00:00
if (artist !== undefined) {
artist = artist.trim();
}
2020-04-05 14:01:21 +00:00
let trackname = $(this).find('span[itemprop="name"]').text().trim();
let tracklength = $(this).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,
2020-04-05 14:01:21 +00:00
duration: tracklength,
});
});
2020-03-19 14:36:20 +00:00
let releaseArtists = $('.product-artist')
.contents()
2020-04-05 14:01:21 +00:00
.map(function () {
2020-03-19 14:36:20 +00:00
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,
2020-04-05 14:01:21 +00:00
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-04-05 14:01:21 +00:00
$('.product-share').parent().after(mbUI);
2020-03-19 14:36:20 +00:00
$('#mb_buttons form').css({ display: 'inline', 'margin-right': '5px' });
mbUI.slideDown();
2015-06-13 09:38:19 +00:00
}