MusicBrainz Import Helper Functions

Functions that enable the import of album and track info into the MusicBrainz database.

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/4987/32527/MusicBrainz%20Import%20Helper%20Functions.js

  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. // MusicBrainz Import helper functions
  3. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4.  
  5. /*
  6. * How to use this module?
  7. *
  8. * - First build a release object (see expected format below) that you'll fill in from source of data
  9. * - Call as follows, e.g.:
  10. * var parameters = MBReleaseImportHelper.buildFormParameters(parsedRelease, optionalEditNote);
  11. * - Then build the HTML that you'll inject into source site page:
  12. * var formHtml = MBReleaseImportHelper.buildFormHTML(parameters);
  13. * - Addinionally, you can inject a search link to verify that the release is not already known by MusicBrainz:
  14. * var linkHtml = MBReleaseImportHelper.buildSearchLink(parsedRelease);
  15. *
  16. * Expected format of release object:
  17. *
  18. * release = {
  19. * title,
  20. * artist_credit,
  21. * type,
  22. * status,
  23. * language,
  24. * script,
  25. * packaging,
  26. * country,
  27. * year,
  28. * month,
  29. * day,
  30. * labels = [ { name, mbid, catno }, ... ],
  31. * barcode,
  32. * urls = [ {url, link_type }, ... ],
  33. * discs = [
  34. * {
  35. * title,
  36. * format,
  37. * tracks = [
  38. * { number, title, duration, artist_credit },
  39. * ...
  40. * ]
  41. * },
  42. * ...
  43. * ],
  44. * }
  45. *
  46. * where 'artist_credit' has the following format:
  47. *
  48. * artist_credit = [
  49. * {
  50. * credited_name,
  51. * artist_name,
  52. * artist_mbid,
  53. * joinphrase
  54. * },
  55. * ...
  56. * ]
  57. *
  58. */
  59.  
  60. var MBReleaseImportHelper = (function() {
  61.  
  62. // --------------------------------------- publics ----------------------------------------- //
  63.  
  64. // compute HTML of search link
  65. function fnBuildSearchLink(release) {
  66.  
  67. var totaltracks = 0;
  68. for (var i=0; i < release.discs.length; i++) {
  69. totaltracks += release.discs[i].tracks.length;
  70. }
  71. var release_artist = "";
  72. for (var i=0; i < release.artist_credit.length; i++) {
  73. var ac = release.artist_credit[i];
  74. release_artist += ac.artist_name;
  75. if (typeof ac.joinphrase != 'undefined' && ac.joinphrase != "") {
  76. release_artist += ac.joinphrase;
  77. } else {
  78. if (i != release.artist_credit.length-1) release_artist += ", ";
  79. }
  80. }
  81.  
  82. var innerHTML = '<a href="http://musicbrainz.org/search?query=artist%3A(' + luceneEscape(release_artist) + ')%20release%3A(' + luceneEscape(release.title) + ')%20tracks%3A(' + totaltracks + ')';
  83. if(release.country) innerHTML += '%20country:'+release.country;
  84. innerHTML += '&type=release&advanced=1">Search in MusicBrainz</a>';
  85.  
  86. return innerHTML;
  87. }
  88.  
  89. // compute HTML of import form
  90. function fnBuildFormHTML(parameters) {
  91.  
  92. // Build form
  93. var innerHTML = '<form action="http://musicbrainz.org/release/add" method="post" target="_blank" accept-charset="UTF-8" charset="' + document.characterSet + '">';
  94. parameters.forEach(function(parameter) {
  95. var value = parameter.value + "";
  96. innerHTML += "<input type='hidden' value='" + value.replace(/'/g,"&apos;") + "' name='" + parameter.name + "'/>";
  97. });
  98.  
  99. innerHTML += '<input type="submit" value="Import into MB">';
  100. innerHTML += '</form>';
  101.  
  102. return innerHTML;
  103. }
  104.  
  105. // build form POST parameters that MB is waiting
  106. function fnBuildFormParameters(release, edit_note) {
  107. // Form parameters
  108. var parameters = new Array();
  109. appendParameter(parameters, 'name', release.title);
  110.  
  111. // Release Artist credits
  112. buildArtistCreditsFormParameters(parameters, "", release.artist_credit);
  113.  
  114. if (release["secondary_types"]) {
  115. for (var i=0; i < release.secondary_types.length; i++) {
  116. appendParameter(parameters, 'type', release.secondary_types[i]);
  117. }
  118. }
  119. appendParameter(parameters, 'type', release.type);
  120. appendParameter(parameters, 'status', release.status);
  121. appendParameter(parameters, 'language', release.language);
  122. appendParameter(parameters, 'script', release.script);
  123. appendParameter(parameters, 'packaging', release.packaging);
  124.  
  125. // Date + country
  126. appendParameter(parameters, 'country', release.country);
  127. if (!isNaN(release.year) && release.year != 0) { appendParameter(parameters, 'date.year', release.year); };
  128. if (!isNaN(release.month) && release.month != 0) { appendParameter(parameters, 'date.month', release.month); };
  129. if (!isNaN(release.day) && release.day != 0) { appendParameter(parameters, 'date.day', release.day); };
  130.  
  131. // Barcode
  132. appendParameter(parameters, 'barcode', release.barcode);
  133.  
  134. // Label + catnos
  135. for (var i=0; i < release.labels.length; i++) {
  136. var label = release.labels[i];
  137. appendParameter(parameters, 'labels.'+i+'.name', label.name);
  138. appendParameter(parameters, 'labels.'+i+'.mbid', label.mbid);
  139. if (label.catno != "none") {
  140. appendParameter(parameters, 'labels.'+i+'.catalog_number', label.catno);
  141. }
  142. }
  143.  
  144. // URLs
  145. for (var i=0; i < release.urls.length; i++) {
  146. var url = release.urls[i];
  147. appendParameter(parameters, 'urls.'+i+'.url', url.url);
  148. appendParameter(parameters, 'urls.'+i+'.link_type', url.link_type);
  149. }
  150.  
  151. // Mediums
  152. for (var i=0; i < release.discs.length; i++) {
  153. var disc = release.discs[i];
  154. appendParameter(parameters, 'mediums.'+i+'.format', disc.format);
  155. appendParameter(parameters, 'mediums.'+i+'.name', disc.title);
  156.  
  157. // Tracks
  158. for (var j=0; j < disc.tracks.length; j++) {
  159. var track = disc.tracks[j];
  160. appendParameter(parameters, 'mediums.'+i+'.track.'+j+'.number', track.number);
  161. appendParameter(parameters, 'mediums.'+i+'.track.'+j+'.name', track.title);
  162. var tracklength = (typeof track.duration != 'undefined' && track.duration != '') ? track.duration : "?:??";
  163. appendParameter(parameters, 'mediums.'+i+'.track.'+j+'.length', tracklength);
  164.  
  165. buildArtistCreditsFormParameters(parameters, 'mediums.'+i+'.track.'+j+'.', track.artist_credit);
  166. }
  167. }
  168.  
  169. // Add Edit note parameter
  170. appendParameter(parameters, 'edit_note', edit_note);
  171.  
  172. return parameters;
  173. }
  174.  
  175. // --------------------------------------- privates ----------------------------------------- //
  176.  
  177. function appendParameter(parameters, paramName, paramValue) {
  178. if(!paramValue) return;
  179. parameters.push( { name: paramName, value: paramValue } );
  180. }
  181.  
  182. function luceneEscape(text) {
  183. var newtext = text.replace(/[-[\]{}()*+?~:\\^!"]/g, "\\$&");
  184. return newtext.replace("&&", "\&&").replace("||", "\||");
  185. }
  186.  
  187. function buildArtistCreditsFormParameters(parameters, paramPrefix, artist_credit) {
  188. if(!artist_credit) return;
  189. for (var i=0; i < artist_credit.length; i++) {
  190. var ac = artist_credit[i];
  191. appendParameter(parameters, paramPrefix+'artist_credit.names.'+i+'.name', ac.credited_name);
  192. appendParameter(parameters, paramPrefix+'artist_credit.names.'+i+'.artist.name', ac.artist_name);
  193. appendParameter(parameters, paramPrefix+'artist_credit.names.'+i+'.artist.mbid', ac.artist_mbid);
  194. if (typeof ac.joinphrase != 'undefined' && ac.joinphrase != "") {
  195. appendParameter(parameters, paramPrefix+'artist_credit.names.'+i+'.join_phrase', ac.joinphrase);
  196. }
  197. }
  198. }
  199.  
  200. // ---------------------------------- expose publics here ------------------------------------ //
  201.  
  202. return {
  203. buildSearchLink: fnBuildSearchLink,
  204. buildFormHTML: fnBuildFormHTML,
  205. buildFormParameters: fnBuildFormParameters
  206. };
  207. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址