2016-05-29 12:21:25 +00:00
// ==UserScript==
// @name Import FMA releases to MusicBrainz
// @description Add a button to import https://freemusicarchive.org/ releases to MusicBrainz via API
2018-02-18 17:16:45 +00:00
// @version 2018.2.18.1
2016-05-29 12:21:25 +00:00
// @namespace https://github.com/murdos/musicbrainz-userscripts
// @downloadURL https://raw.github.com/murdos/musicbrainz-userscripts/master/fma_importer.user.js
// @updateURL https://raw.github.com/murdos/musicbrainz-userscripts/master/fma_importer.user.js
// @include http*://freemusicarchive.org/music/*
// @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
// @grant none
// ==/UserScript==
/ *
* Test cases :
* http : //freemusicarchive.org/music/Various_Artists/Of_Men_And_Machines/
* http : //freemusicarchive.org/music/cloud_mouth/songs_from_the_sewer/
* http : //freemusicarchive.org/music/Podington_Bear/Springtime/
* http : //freemusicarchive.org/music/Broke_For_Free/Directionless_EP/
* http : //freemusicarchive.org/music/Various_Artists_Evergreens_n_Odditunes/Evergreens_n_Odditunes/
2016-09-23 13:54:26 +00:00
* Radio program : http : //freemusicarchive.org/music/Kyle_Eyre_Clyd/Live_on_WFMUs_Strength_Through_Failure_with_Fabio_Roberti_8132015/
2016-05-29 12:21:25 +00:00
* /
// prevent JQuery conflicts, see http://wiki.greasespot.net/@grant
this . $ = this . jQuery = jQuery . noConflict ( true ) ;
// API Key assigned to registered user on FMA
2018-11-20 22:18:49 +00:00
var FMA _API = 'FMA API KEY Missing' ;
2016-05-29 12:21:25 +00:00
var DEBUG = false ; // true | false
if ( DEBUG ) {
2018-11-20 22:18:49 +00:00
LOGGER . setLevel ( 'debug' ) ;
2016-05-29 12:21:25 +00:00
}
// promise to ensure all api calls are done before we parse the release
var tracks _deferred = $ . Deferred ( ) ;
var retrieve _tracks _promise = tracks _deferred . promise ( ) ;
// object to store all global attributes collected for the release
var release _attributes = { } ; // albumid, total_pages, artist_name, label
// arrays to store the data retrieved from API to parse for MB release
var album _api _array = [ ] ; // album information [0]
var tracks _api _array = [ ] ; // track information [0,1,2,..] one element for each pagination in FMA tracks API
2020-04-05 14:01:21 +00:00
$ ( document ) . ready ( function ( ) {
2018-11-20 22:18:49 +00:00
// if we have something on local storage place that
if ( localStorage . getItem ( 'FMA_API_KEY' ) ) {
FMA _API = localStorage . getItem ( 'FMA_API_KEY' ) ; // -> YOURAPIKEY
} else {
insertAPIKEYSection ( ) ;
2020-04-05 14:01:21 +00:00
$ ( '#api_key_submit' ) . click ( function ( ) {
2018-11-20 22:18:49 +00:00
let myval = $ ( '#apikey_input' ) . val ( ) ;
localStorage . setItem ( 'FMA_API_KEY' , myval ) ;
$ ( '#musicbrainz_apikey' ) . hide ( ) ;
FMA _API = localStorage . getItem ( 'FMA_API_KEY' ) ; // -> YOURAPIKEY
LOGGER . debug ( ` FMA API Key set: ${ FMA _API } ` ) ;
location . reload ( true ) ; //as document loaded and FMA_API was set out of scope
} ) ;
}
// window.localStorage.clear() hint: to clear the localStorage if needed
LOGGER . info ( 'Document Ready & FMA Userscript Executing' ) ;
let fmaPage = parseFMApage ( ) ;
let mblinks = new MBLinks ( 'FMA_CACHE' , 7 * 24 * 60 ) ;
if ( DEBUG ) {
insertAPISection ( ) ;
updateAPISection . AlbumId ( release _attributes . albumid ) ;
updateAPISection . ApiKey ( FMA _API ) ;
}
if ( $ ( '.minitag-album' ) . length && FMA _API !== 'FMA API KEY Missing' ) {
// To make sure API and release only build on Album page.
// Track parameters: total number of pages / api calls limit hardcoded to max of 20
let retrieve _track _info = new track _api _parameters ( ) ;
// Album detail
let retrieve _album _detail = new album _api ( ) ;
// Track detail
$ . when ( retrieve _track _info ) // ensure the track info is retrieved first (total_pages counter)
2020-04-05 14:01:21 +00:00
. then ( function ( ) {
2018-11-20 22:18:49 +00:00
// loop and deferred promise for multiple ajax calls
updateAPISection . TrackAjaxStatus ( 'busy' ) ;
let track _api _calls = [ ] ;
for ( let i = 1 ; i <= release _attributes . total _pages ; i ++ ) {
track _api _calls . push ( track _api ( i ) ) ;
}
2020-04-05 14:01:21 +00:00
$ . when . apply ( this , track _api _calls ) . done ( function ( ) {
2018-11-20 22:18:49 +00:00
LOGGER . debug ( 'Tracks loaded and done in DONE lets use it' ) ;
//console.log("total_pages " + release_attributes.total_pages);
tracks _deferred . resolve ( ) ;
} ) ;
} )
2020-04-05 14:01:21 +00:00
. done ( function ( ) {
2018-11-20 22:18:49 +00:00
LOGGER . debug ( 'Deferred for: Track info > track detail > resolved' ) ;
} ) ;
$ . when ( retrieve _tracks _promise )
2020-04-05 14:01:21 +00:00
. done ( function ( ) {
2018-11-20 22:18:49 +00:00
updateAPISection . TrackAjaxStatus ( 'completed' ) ;
} )
2020-04-05 14:01:21 +00:00
. fail ( function ( ) {
2018-11-20 22:18:49 +00:00
updateAPISection . TrackAjaxStatus ( 'fail' ) ;
} ) ;
2020-04-05 14:01:21 +00:00
$ . when ( retrieve _track _info , retrieve _tracks _promise , retrieve _album _detail ) . done ( function ( ) {
2018-11-20 22:18:49 +00:00
LOGGER . info ( 'All the AJAX API calls are done continue to build the release object ...' ) ;
// LOGGER.debug("ALBUM Object > " + album_api_array[0]);
// LOGGER.debug("TRACK Object > " + tracks_api_array);
let FreeMusicArchiveRelease = new Parsefmarelease ( album _api _array [ 0 ] , tracks _api _array ) ;
insertMBSection ( FreeMusicArchiveRelease ) ;
let album _link = window . location . href ;
2020-04-05 14:01:21 +00:00
let url = $ ( location ) . attr ( 'href' ) . split ( '/' ) ;
2018-11-20 22:18:49 +00:00
let artist _url = url [ url . length - 3 ] ;
let base _url = 'http://freemusicarchive.org/music/' ;
let artist _link = ` ${ base _url + artist _url } / ` ;
2020-04-05 14:01:21 +00:00
mblinks . searchAndDisplayMbLink ( album _link , 'release' , function ( link ) {
2018-11-20 22:18:49 +00:00
$ ( '.subh1' ) . before ( link ) ;
} ) ;
2020-04-05 14:01:21 +00:00
mblinks . searchAndDisplayMbLink ( artist _link , 'artist' , function ( link ) {
2018-11-20 22:18:49 +00:00
$ ( '.subh1' ) . after ( link ) ;
} ) ;
} ) ;
}
2016-05-29 12:21:25 +00:00
} ) ;
// Determine the location on page to add MusicBrainz Section
function insertMbUI ( mbUI ) {
2018-11-20 22:18:49 +00:00
let e ;
if ( ( e = $ ( '#header' ) ) && e . length ) {
e . after ( mbUI ) ;
} else if ( ( e = $ ( '#content' ) ) && e . length ) {
e . before ( mbUI ) ;
} else if ( ( e = $ ( '.brumbs' ) ) && e . length ) {
e . append ( mbUI ) ;
}
2016-05-29 12:21:25 +00:00
}
// Insert link to high resolution image on FMA page
function insertIMGlinks ( ) {
2018-11-20 22:18:49 +00:00
//LOGGER.debug("FMA insertIMGlinks Function Executing");
let imgsrc = $ ( '#image-1 img.sbar-fullimg' ) . attr ( 'src' ) ;
imgsrc = imgsrc . substring ( 0 , imgsrc . indexOf ( '?' ) ) ;
//LOGGER.debug(" insertIMGlinks > imgsrc:", imgsrc);
$ ( '#album-images' ) . append ( ` <p><img src="http://musicbrainz.org/favicon.ico" /><a href=" ${ imgsrc } ">MB High Res Image</a></p> ` ) ;
2016-05-29 12:21:25 +00:00
}
// Insert FreeMusicArchive API Status section on FMA page
function insertAPISection ( ) {
2018-11-20 22:18:49 +00:00
//LOGGER.debug("FMA insertAPISection Function Executing");
2016-05-29 12:21:25 +00:00
2018-11-20 22:18:49 +00:00
let fmaUI = $ ( '<div id="fmaapistatus" class="sbar-stat"><h4 class="wlinepad"><span class="hd">FMA API</span></h4></div>' ) . hide ( ) ;
2016-05-29 12:21:25 +00:00
2018-11-20 22:18:49 +00:00
if ( DEBUG )
fmaUI . css ( {
2020-04-05 14:01:21 +00:00
border : '1px dotted red' ,
2018-11-20 22:18:49 +00:00
} ) ;
2016-05-29 12:21:25 +00:00
2018-11-20 22:18:49 +00:00
let fmaStatusBlock = $ (
'<a class="lbut-lt" id="lbut-lt-fma-api-album-id">»</a> <a class="lbut-lt" id="lbut-lt-fma-api-key-id">»</a> <a id="lbut-lt-fma-api-album" class="lbut-lt">Album info retrieved</a><a class="lbut-lt" id="lbut-lt-fma-api-tracks">Track info retrieved</a>'
) ;
fmaUI . append ( fmaStatusBlock ) ;
2016-05-29 12:21:25 +00:00
2018-11-20 22:18:49 +00:00
insertMbUI ( fmaUI ) ; // Insert the FMA API Status UI
2016-05-29 12:21:25 +00:00
2018-11-20 22:18:49 +00:00
$ ( '#fmaapistatus' ) . css ( {
display : 'inline-block' ,
float : 'left' ,
height : '120px' ,
2020-04-05 14:01:21 +00:00
width : '49%' ,
2018-11-20 22:18:49 +00:00
} ) ;
2016-05-29 12:21:25 +00:00
2018-11-20 22:18:49 +00:00
fmaUI . slideDown ( ) ;
2016-05-29 12:21:25 +00:00
}
// Update FreeMusicArchive API Status section on FMA page
var updateAPISection = {
2020-04-05 14:01:21 +00:00
AlbumId : function ( albumid ) {
2018-11-20 22:18:49 +00:00
this . albumid = albumid ;
$ ( '#lbut-lt-fma-api-album-id' ) . text ( this . albumid ) ;
return 'complete' ;
} ,
2020-04-05 14:01:21 +00:00
ApiKey : function ( apikey ) {
2018-11-20 22:18:49 +00:00
this . apikey = apikey ;
$ ( '#lbut-lt-fma-api-key-id' ) . text ( FMA _API ) ;
return 'complete' ;
} ,
2020-04-05 14:01:21 +00:00
AlbumAjaxStatus : function ( ajaxstatus ) {
2018-11-20 22:18:49 +00:00
if ( ajaxstatus === null ) {
this . ajaxstatus = 'notcalled' ;
} else {
this . ajaxstatus = ajaxstatus ;
}
switch ( this . ajaxstatus ) {
case 'completed' : // Definition is that api call was successfull hence busy retrieving data
//test chaging status of album api to error retrieving data after 2 seconds
$ ( '#lbut-lt-fma-api-album' ) . css ( {
2020-04-05 14:01:21 +00:00
'background-color' : 'green' ,
2018-11-20 22:18:49 +00:00
} ) ;
break ;
case 'busy' : // Definition is that api call was successfull hence busy retrieving data
//test chaging status of album api to error retrieving data after 2 seconds
$ ( '#lbut-lt-fma-api-album' ) . css ( {
2020-04-05 14:01:21 +00:00
'background-color' : 'orange' ,
2018-11-20 22:18:49 +00:00
} ) ;
break ;
case 'fail' : // Definition is that api call was successfull hence busy retrieving data
//test chaging status of album api to error retrieving data after 2 seconds
$ ( '#lbut-lt-fma-api-album' ) . css ( {
2020-04-05 14:01:21 +00:00
'background-color' : 'red' ,
2018-11-20 22:18:49 +00:00
} ) ;
break ;
}
} ,
2020-04-05 14:01:21 +00:00
TrackAjaxStatus : function ( ajaxstatus ) {
2018-11-20 22:18:49 +00:00
if ( ajaxstatus === null ) {
this . ajaxstatus = 'notcalled' ;
} else {
this . ajaxstatus = ajaxstatus ;
}
switch ( this . ajaxstatus ) {
case 'completed' : // Definition is that api call was successfull hence busy retrieving data
//test chaging status of album api to error retrieving data after 2 seconds
$ ( '#lbut-lt-fma-api-tracks' ) . css ( {
2020-04-05 14:01:21 +00:00
'background-color' : 'green' ,
2018-11-20 22:18:49 +00:00
} ) ;
break ;
case 'busy' : // Definition is that api call was successfull hence busy retrieving data
//test chaging status of album api to error retrieving data after 2 seconds
$ ( '#lbut-lt-fma-api-tracks' ) . css ( {
2020-04-05 14:01:21 +00:00
'background-color' : 'orange' ,
2018-11-20 22:18:49 +00:00
} ) ;
break ;
case 'fail' : // Definition is that api call was successfull hence busy retrieving data
//test chaging status of album api to error retrieving data after 2 seconds
$ ( '#lbut-lt-fma-api-tracks' ) . css ( {
2020-04-05 14:01:21 +00:00
'background-color' : 'red' ,
2018-11-20 22:18:49 +00:00
} ) ;
break ;
}
2020-04-05 14:01:21 +00:00
} ,
2016-05-29 12:21:25 +00:00
} ;
// Insert MusicBrainz section on FMA page
function insertMBSection ( release ) {
2018-11-20 22:18:49 +00:00
//LOGGER.debug(release);
let mbUI = $ (
'<div id="musicbrainz" class="section musicbrainz"><h4 class="wlinepad"><span class="hd">MusicBrainz</span></h4></div>'
) . hide ( ) ;
if ( DEBUG )
mbUI . css ( {
2020-04-05 14:01:21 +00:00
border : '1px dotted red' ,
2018-11-20 22:18:49 +00:00
} ) ;
let mbContentBlock = $ ( '<div class="section_content"></div>' ) ;
mbUI . append ( mbContentBlock ) ;
if ( release . maybe _buggy ) {
let warning _buggy = $ ( '<p><small><b>Warning</b>: this release is buggy, please check twice the data you import.</small><p' ) . css ( {
color : 'red' ,
float : 'left' ,
'margin-top' : '4px' ,
2020-04-05 14:01:21 +00:00
'margin-bottom' : '4px' ,
2018-11-20 22:18:49 +00:00
} ) ;
mbContentBlock . prepend ( warning _buggy ) ;
}
// Form parameters
let edit _note = ` FMA_Album_Id: ${ release _attributes . albumid } ` ; // temp add album id here untill we can add easy way to schema
edit _note = edit _note + MBImport . makeEditNote ( window . location . href , 'FreeMusicArchive' ) ;
let parameters = MBImport . buildFormParameters ( release , edit _note ) ;
// Build form + search button
let innerHTML = ` <div id="mb_buttons"> ${ MBImport . buildFormHTML ( parameters ) } ${ MBImport . buildSearchButton ( release ) } </div> ` ;
mbContentBlock . append ( innerHTML ) ;
insertMbUI ( mbUI ) ; // Insert the MusicBrainzUI
insertIMGlinks ( ) ; // Insert the link to high res image
$ ( '#musicbrainz' ) . css ( {
display : 'block' ,
float : 'right' ,
height : '120px' ,
2020-04-05 14:01:21 +00:00
width : '49%' ,
2018-11-20 22:18:49 +00:00
} ) ;
$ ( '#mb_buttons' ) . css ( {
display : 'inline-block' ,
float : 'right' ,
2020-04-05 14:01:21 +00:00
height : '80px' ,
2018-11-20 22:18:49 +00:00
} ) ;
$ ( 'form.musicbrainz_import' ) . css ( {
width : '49%' ,
2020-04-05 14:01:21 +00:00
display : 'inline-block' ,
2018-11-20 22:18:49 +00:00
} ) ;
$ ( 'form.musicbrainz_import_search' ) . css ( {
2020-04-05 14:01:21 +00:00
float : 'right' ,
2018-11-20 22:18:49 +00:00
} ) ;
$ ( 'form.musicbrainz_import > button' ) . css ( {
width : '63px' ,
height : '80px' ,
2020-04-05 14:01:21 +00:00
'box-sizing' : 'border-box' ,
2018-11-20 22:18:49 +00:00
} ) ;
mbUI . slideDown ( ) ;
2016-05-29 12:21:25 +00:00
}
// Insert MusicBrainz API section on FMA page to enter API Key
function insertAPIKEYSection ( ) {
2018-11-20 22:18:49 +00:00
LOGGER . debug ( 'FMA insertAPIKEYSection Function Executing' ) ;
let mbUI = $ (
'<div id="musicbrainz_apikey" class="section musicbrainz"><h4 class="wlinepad"><span class="hd">Import FMA API KEY for MusicBrainz</span></h4></div>'
) . hide ( ) ;
if ( DEBUG )
mbUI . css ( {
2020-04-05 14:01:21 +00:00
border : '1px dotted red' ,
2018-11-20 22:18:49 +00:00
} ) ;
let mbContentBlock = $ ( '<div class="section_content"></div>' ) ;
mbUI . append ( mbContentBlock ) ;
// Build section
let innerHTML =
'<span class="mhd-nosep">Please enter API Key found <a class="donate" href="https://freemusicarchive.org/member/api_key" target="_blank">here</a></span>' ;
innerHTML = ` ${ innerHTML } <div id="mb_buttons"><input id="apikey_input" type="text" name="apikey_input" value=""><br><input id="api_key_submit" type="submit" value="Import API KEY"></div> ` ;
mbContentBlock . append ( innerHTML ) ;
insertMbUI ( mbUI ) ; // Insert the MusicBrainzUI
$ ( '#musicbrainz_apikey' ) . css ( {
display : 'block' ,
float : 'right' ,
height : '120px' ,
2020-04-05 14:01:21 +00:00
width : '49%' ,
2018-11-20 22:18:49 +00:00
} ) ;
$ ( '#mb_buttons' ) . css ( {
display : 'inline-block' ,
float : 'right' ,
2020-04-05 14:01:21 +00:00
height : '80px' ,
2018-11-20 22:18:49 +00:00
} ) ;
mbUI . slideDown ( ) ;
2016-05-29 12:21:25 +00:00
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Retrieve data from FMA API //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Retrieve Album JSON from API and push into array
function album _api ( ) {
2018-11-20 22:18:49 +00:00
let fmaWsUrl = ` https://freemusicarchive.org/api/get/albums.json?api_key= ${ FMA _API } &album_id= ${ release _attributes . albumid } ` ;
2020-04-05 14:01:21 +00:00
var promise _variable = $ . getJSON ( fmaWsUrl , function ( ) {
2018-11-20 22:18:49 +00:00
updateAPISection . AlbumAjaxStatus ( 'busy' ) ;
LOGGER . debug ( ` promise_variable [state] in [getJSON] ${ promise _variable . state ( ) } ` ) ;
2020-04-05 14:01:21 +00:00
} ) . done ( function ( albumjson ) {
2018-11-20 22:18:49 +00:00
LOGGER . debug ( ' >> Album > DONE' ) ;
updateAPISection . AlbumAjaxStatus ( 'completed' ) ;
//LOGGER.debug(albumjson);
release _attributes . artist _name = albumjson . dataset [ 0 ] . artist _name ;
album _api _array . push ( albumjson . dataset [ 0 ] ) ;
} ) ;
return promise _variable . promise ( ) ;
2016-05-29 12:21:25 +00:00
}
// Retrieve Album JSON from API and assign values to release object
function track _api _parameters ( ) {
2018-11-20 22:18:49 +00:00
let fmaWsUrl = ` https://freemusicarchive.org/api/get/tracks.json?api_key= ${ FMA _API } &album_id= ${ release _attributes . albumid } &limit=20 ` ;
2016-05-29 12:21:25 +00:00
2020-04-05 14:01:21 +00:00
var promise _track _api _params = $ . getJSON ( fmaWsUrl , function ( ) {
2018-11-20 22:18:49 +00:00
LOGGER . debug ( ` promise_track_api_params [state] in [getJSON] ${ promise _track _api _params . state ( ) } ` ) ;
2020-04-05 14:01:21 +00:00
} ) . done ( function ( trackinfojson ) {
2018-11-20 22:18:49 +00:00
LOGGER . debug ( ' >> Track INFO > DONE' ) ;
release _attributes . total _pages = trackinfojson . total _pages ;
//LOGGER.debug(trackinfojson);
} ) ;
2016-05-29 12:21:25 +00:00
2018-11-20 22:18:49 +00:00
return promise _track _api _params . promise ( ) ;
2016-05-29 12:21:25 +00:00
}
// Retrieve Track JSON from API and push into array, can handle page itteration
function track _api ( page ) {
2018-11-20 22:18:49 +00:00
let fmaWsUrl = ` https://freemusicarchive.org/api/get/tracks.json?api_key= ${ FMA _API } &album_id= ${
release _attributes . albumid
} & limit = 20 & page = $ { parseInt ( page ) } ` ;
2020-04-05 14:01:21 +00:00
var promise _track _api = $ . getJSON ( fmaWsUrl , function ( ) {
2018-11-20 22:18:49 +00:00
LOGGER . debug ( ` promise_track_api_params [state] in [getJSON] ${ promise _track _api . state ( ) } ` ) ;
2020-04-05 14:01:21 +00:00
} ) . done ( function ( tracksjson ) {
2018-11-20 22:18:49 +00:00
LOGGER . debug ( ` >> Track page ${ page } > DONE ` ) ;
LOGGER . debug ( tracksjson ) ;
tracks _api _array . push ( tracksjson . dataset ) ;
} ) ;
return promise _track _api . promise ( ) ;
2016-05-29 12:21:25 +00:00
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Parse information from FMA Page //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function parseFMApage ( ) {
2018-11-20 22:18:49 +00:00
// Check to see if it is an album class is minitag-album div#content div.bcrumb h1 span.minitag-album
let FMAtype = '' ;
// class inp-embed-code contain the album id
if ( $ ( '.minitag-album' ) . length ) {
FMAtype = 'album' ;
} else if ( $ ( '.minitag-song' ) . length ) {
FMAtype = 'track' ;
} else if ( $ ( '.minitag-artist' ) . length ) {
FMAtype = 'artist' ;
}
if ( FMAtype == 'album' ) {
//LOGGER.debug("FMA parseFMApage Function Executing on ", FMAtype);
let FMAEmbedCode = $ ( '.inp-embed-code input' ) . attr ( 'value' ) ;
FMAEmbedCodeRegex = /(\/embed\/album\/)(.+?(?=.xml))/ ; // regex to find the album id from the input object
let FMAAlbumIdMatch = FMAEmbedCode . match ( FMAEmbedCodeRegex ) ; // match the Id
release _attributes . albumid = FMAAlbumIdMatch [ 2 ] . trim ( ) ; // assign the ID to a variable
LOGGER . info ( 'FreeMusicArchive Album identified as: ' , release _attributes . albumid ) ;
} else {
LOGGER . error ( 'No unique album identified on page' , window . location . href ) ;
release _attributes . albumid = '' ;
}
// Label parsed from webpage as it is not in API
2020-04-05 14:01:21 +00:00
$ ( 'div.sbar-stat span.lf105.stathd' ) . each ( function ( ) {
2018-11-20 22:18:49 +00:00
//var tester = $(this).eq(0).text().trim().toLowerCase(); // working
2020-04-05 14:01:21 +00:00
let taglist = $ ( this ) . eq ( 0 ) . text ( ) . trim ( ) . toLowerCase ( ) ;
2018-11-20 22:18:49 +00:00
if ( taglist == 'label:' ) {
2020-04-05 14:01:21 +00:00
release _attributes . label = $ ( this ) . next ( ) . text ( ) ;
2018-11-20 22:18:49 +00:00
// fmarelease.labels.push({
// name: FMAAlbumLabel
// });
} else {
release _attributes . label = '' ;
}
} ) ;
2016-05-29 12:21:25 +00:00
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Analyze FMA data and return a release object //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Parse the date string and set object properties day, month, year
function parse _MM _DD _YYYY ( date , obj ) {
2018-11-20 22:18:49 +00:00
if ( ! date ) return ;
2020-04-05 14:01:21 +00:00
let m = date . split ( /\D+/ , 3 ) . map ( function ( e ) {
2018-11-20 22:18:49 +00:00
return parseInt ( e , 10 ) ;
} ) ;
if ( m [ 0 ] !== undefined ) {
obj . month = m [ 0 ] ;
if ( m [ 1 ] !== undefined ) {
obj . day = m [ 1 ] ;
if ( m [ 2 ] !== undefined ) {
obj . year = m [ 2 ] ;
}
}
}
2016-05-29 12:21:25 +00:00
}
// parse the release from the album and track objects
function Parsefmarelease ( albumobject , trackobject ) {
2018-11-20 22:18:49 +00:00
if ( albumobject === undefined ) {
albumobject = [ ] ;
} else {
albumobject = albumobject ;
}
if ( trackobject === undefined ) {
trackobject = [ ] ;
} else {
trackobject = trackobject ;
}
let fmarelease = { } ;
// Create an empty object required for MBImport
fmarelease . title = '' ;
fmarelease . artist _credit = [ ] ;
fmarelease . type = '' ;
fmarelease . status = '' ;
fmarelease . language = '' ;
fmarelease . script = '' ;
fmarelease . packaging = '' ;
fmarelease . country = '' ;
fmarelease . year = '' ;
fmarelease . month = '' ;
fmarelease . day = '' ;
fmarelease . labels = [ ] ;
fmarelease . barcode = '' ;
fmarelease . urls = [ ] ;
fmarelease . discs = [ ] ;
// LOGGER.debug("Album object for parsing", albumobject);
// LOGGER.debug("Track object for parsing", trackobject);
// Title
fmarelease . title = albumobject . album _title ;
LOGGER . debug ( 'Title: ' , fmarelease . title ) ;
// Artist Credit
let VariousArtistsRegex = /(Various Artists)/ ; //found "Various Artists || Various Artists [album name]"
let various _artists = VariousArtistsRegex . test ( albumobject . artist _name ) ;
if ( various _artists ) {
fmarelease . artist _credit = [ MBImport . specialArtist ( 'various_artists' ) ] ;
} else {
fmarelease . artist _credit = MBImport . makeArtistCredits ( [ albumobject . artist _name ] ) ;
}
// Type
// TODO: match all FMA types to MB types
if ( albumobject . album _type == 'Radio Program' ) {
fmarelease . type = 'broadcast' ;
} else {
fmarelease . type = albumobject . album _type . toLowerCase ( ) ;
}
// Default status is official
fmarelease . status = 'official' ;
// Script
fmarelease . script = 'Latn' ;
// Check to see if a download button is available
if ( $ ( '.sqbtn-downloadalbum' ) . length ) {
fmarelease . packaging = 'none' ; // Default packaging for download is none
// Release URL
fmarelease . urls . push ( {
url : albumobject . album _url ,
2020-04-05 14:01:21 +00:00
link _type : MBImport . URL _TYPES . download _for _free ,
2018-11-20 22:18:49 +00:00
} ) ;
} else {
// Release URL
fmarelease . urls . push ( {
url : albumobject . album _url ,
2020-04-05 14:01:21 +00:00
link _type : MBImport . URL _TYPES . other _databases ,
2018-11-20 22:18:49 +00:00
} ) ;
}
// Check to see if a play button is available
if ( $ ( '.sqbtn-playpage' ) . length ) {
// Release URL
fmarelease . urls . push ( {
url : albumobject . album _url ,
2020-04-05 14:01:21 +00:00
link _type : MBImport . URL _TYPES . stream _for _free ,
2018-11-20 22:18:49 +00:00
} ) ;
}
// Release date
if ( albumobject . album _date _released ) {
parse _MM _DD _YYYY ( albumobject . album _date _released , fmarelease ) ;
}
// Label parsed from webpage as it is not in API
fmarelease . labels . push ( {
2020-04-05 14:01:21 +00:00
name : release _attributes . label ,
2018-11-20 22:18:49 +00:00
} ) ;
let discarray = [ ] ;
let trackarray = [ ] ;
// release_attributes.total_pages
for ( let track _page _in _array = 0 ; track _page _in _array < trackobject . length ; track _page _in _array ++ ) {
//LOGGER.debug(" ** Looping through array set for page " + track_page_in_array);
let track _count _in _array _page = trackobject [ track _page _in _array ] . length ;
//LOGGER.debug(" ** Track count in: trackobject[" + track_page_in_array + "] = " + track_count_in_array_page);
for ( let tracknumber = 0 ; tracknumber < track _count _in _array _page ; tracknumber ++ ) {
//LOGGER.debug(" **** Track number in: trackobject[" + track_page_in_array + "][" + tracknumber + "] = " + tracknumber);
let track = { } ;
track . disc _number = trackobject [ track _page _in _array ] [ tracknumber ] . track _disc _number ;
track . number = trackobject [ track _page _in _array ] [ tracknumber ] . track _number ;
track . title = trackobject [ track _page _in _array ] [ tracknumber ] . track _title ;
track . duration = trackobject [ track _page _in _array ] [ tracknumber ] . track _duration ;
track . artist _credit = MBImport . makeArtistCredits ( [ trackobject [ track _page _in _array ] [ tracknumber ] . artist _name ] ) ;
trackarray . push ( track ) ;
}
}
// Could not find a example where disc_number != 1 yet but started teh check so long
let largest _disc = Math . max . apply (
Math ,
2020-04-05 14:01:21 +00:00
trackarray . map ( function ( o ) {
2018-11-20 22:18:49 +00:00
return o . disc _number ;
} )
) ;
//LOGGER.debug("Highest number disc:" + largest_disc);
for ( var disccount = 1 ; disccount <= largest _disc ; disccount ++ ) {
// use this to map all the objects from trackarray with disc_number value of disccount to a new object
2020-04-05 14:01:21 +00:00
let tracklist _per _disc = $ . map ( trackarray , function ( obj , index ) {
2018-11-20 22:18:49 +00:00
if ( obj . disc _number == disccount ) {
return obj ;
}
} ) ;
// use this to sort the tracks per disc from low to high
2020-04-05 14:01:21 +00:00
tracklist _per _disc = tracklist _per _disc . sort ( function ( a , b ) {
2018-11-20 22:18:49 +00:00
return parseInt ( a . number ) - parseInt ( b . number ) ;
} ) ;
// remove the disc_number from the tracklist - not working
// tracklist_per_disc = tracklist_per_disc.filter(function( obj ) {
// return obj.field !== 'disc_number';
// });
// current solution to remove disc_number
for ( i = tracklist _per _disc . length - 1 ; i >= 0 ; i -- ) {
delete tracklist _per _disc [ i ] . disc _number ;
}
//LOGGER.debug("Disc # " + disccount + " > " + JSON.stringify(tracklist_per_disc));
let disc = {
position : disccount ,
format : 'Digital Media' ,
2020-04-05 14:01:21 +00:00
tracks : tracklist _per _disc ,
2018-11-20 22:18:49 +00:00
} ;
fmarelease . discs . push ( disc ) ;
}
LOGGER . info ( 'Release:' , fmarelease ) ;
return fmarelease ;
2016-05-29 12:21:25 +00:00
}