Gazelle extract featured artists from description

Tries to recognize and add featured artists from selected text in group description

当前为 2020-09-29 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Gazelle extract featured artists from description
  3. // @namespace https://greasyfork.org/cs/users/321857-anakunda
  4. // @version 1.39
  5. // @description Tries to recognize and add featured artists from selected text in group description
  6. // @author Anakunda
  7. // @copyright 2020, Anakunda (https://greasyfork.org/cs/users/321857-anakunda)
  8. // @license GPL-3.0-or-later
  9. // @match https://redacted.ch/torrents.php?*id=*
  10. // @match https://orpheus.network/torrents.php?*id=*
  11. // @match https://notwhat.cd/torrents.php?*id=*
  12. // @grant RegExp
  13. // @grant GM_xmlhttpRequest
  14. // @grant GM_getValue
  15. // @grant GM_setValue
  16. // @grant GM_deleteValue
  17. // @grant GM_log
  18. // @require https://greasyfork.org/scripts/388280-xpathlib/code/XPathLib.js
  19. // ==/UserScript==
  20.  
  21. const multiArtistParsers = [
  22. /\s*[,;\u3001](?!\s*(?:[JjSs]r)\b)(?:\s*[Aa]nd\s+)?\s*/,
  23. /\s+[\/\|\×]\s+/,
  24. ];
  25. const pseudoArtistParsers = [
  26. /* 0 */ /^(?:Various(?:\s+Artists)?|VA|\<various\s+artists\>|Různí(?:\s+interpreti)?)$/i,
  27. /* 1 */ /^(?:#??N[\/\-]?A|[JS]r\.?)$/i,
  28. /* 2 */ /^(?:auditorium|[Oo]becenstvo|[Pp]ublikum)$/,
  29. /* 3 */ /^(?:(Special\s+)??Guests?|Friends)$/i,
  30. /* 4 */ /^(?:Various\s+Composers)$/i,
  31. /* 5 */ /^(?:[Aa]nonym)/,
  32. /* 6 */ /^(?:traditional|trad\.|lidová)$/i,
  33. /* 7 */ /\b(?:traditional|trad\.|lidová)$/,
  34. /* 8 */ /^(?:tradiční|lidová)\s+/,
  35. /* 9 */ /^(?:[Ll]iturgical\b|[Ll]iturgick[áý])/,
  36. ];
  37. const ampersandParsers = [
  38. /\s+(?:vs\.?|X)\s+(?!\s*(?:[\&\/\+\,\;]|and))/i,
  39. /\s*[;\/\|\×]\s*(?!\s*(?:\s*[\&\/\+\,\;]|and))/i,
  40. /\s+(?:[\&\+]|and)\s+(?!his\b|her\b|Friends$|Strings$)/i, // /\s+(?:[\&\+]|and)\s+(?!(?:The|his|her|Friends)\b)/i,
  41. /\s*\+\s*(?!(?:his\b|her\b|Friends$|Strings$))/i,
  42. ];
  43. const otherArtistsParsers = [
  44. [/^(.*?)\s+(?:under|(?:conducted)\s+by)\s+(.*)$/, 4],
  45. [/^()(.*?)\s+\(conductor\)$/i, 4],
  46. //[/^()(.*?)\s+\(.*\)$/i, 1],
  47. ];
  48.  
  49. const siteApiTimeframeStorageKey = document.location.hostname + ' API time frame';
  50. const gazelleApiFrame = 10500;
  51. var artistIndex, siteArtistsCache = {}, notSiteArtistsCache = [], xhr = new XMLHttpRequest;
  52. var modal = null, btnAdd = null, btnCustom = null, customCtrls = [], sel = null, ajaxRejects = 0;
  53. var prefs = {
  54. set: function(prop, def) { this[prop] = GM_getValue(prop, def) }
  55. };
  56.  
  57. Array.prototype.includesCaseless = function(str) {
  58. if (typeof str != 'string') return false;
  59. str = str.toLowerCase();
  60. return this.find(elem => typeof elem == 'string' && elem.toLowerCase() == str) != undefined;
  61. };
  62. Array.prototype.pushUnique = function(...items) {
  63. if (Array.isArray(items) && items.length > 0) items.forEach(it => { if (!this.includes(it)) this.push(it) });
  64. return this.length;
  65. };
  66. Array.prototype.pushUniqueCaseless = function(...items) {
  67. if (Array.isArray(items) && items.length > 0) items.forEach(it => { if (!this.includesCaseless(it)) this.push(it) });
  68. return this.length;
  69. };
  70.  
  71. (function() {
  72. 'use strict';
  73.  
  74. const styleSheet = `
  75. .modal {
  76. position: fixed;
  77. left: 0;
  78. top: 0;
  79. width: 100%;
  80. height: 100%;
  81. background-color: rgba(0, 0, 0, 0.5);
  82. opacity: 0;
  83. visibility: hidden;
  84. transform: scale(1.1);
  85. transition: visibility 0s linear 0.25s, opacity 0.25s 0s, transform 0.25s;
  86. }
  87. .modal-content {
  88. position: absolute;
  89. top: 50%;
  90. left: 50%;
  91. font-size: 17px;
  92. transform: translate(-50%, -50%);
  93. background-color: FloralWhite;
  94. color: black;
  95. width: 31rem;
  96. border-radius: 0.5rem;
  97. padding: 2rem 2rem 2rem 2rem;
  98. font-family: monospace;
  99. }
  100. .show-modal {
  101. opacity: 1;
  102. visibility: visible;
  103. transform: scale(1.0);
  104. transition: visibility 0s linear 0s, opacity 0.25s 0s, transform 0.25s;
  105. }
  106. input[type="text"] { cursor: text; }
  107. input[type="radio"] { cursor: pointer; }
  108. .lbl { cursor: pointer; }
  109.  
  110. .tooltip {
  111. position: relative;
  112. }
  113.  
  114. .tooltip .tooltiptext {
  115. visibility: hidden;
  116. width: 120px;
  117. background-color: #555;
  118. color: #fff;
  119. text-align: center;
  120. border-radius: 6px;
  121. padding: 5px 0;
  122. position: absolute;
  123. z-index: 1;
  124. bottom: 125%;
  125. left: 50%;
  126. margin-left: -60px;
  127. opacity: 0;
  128. transition: opacity 0.3s;
  129. }
  130.  
  131. .tooltip .tooltiptext::after {
  132. position: absolute;
  133. top: 100%;
  134. left: 50%;
  135. margin-left: -5px;
  136. border-width: 5px;
  137. border-style: solid;
  138. border-color: #555 transparent transparent transparent;
  139. }
  140.  
  141. .tooltip:hover .tooltiptext {
  142. visibility: visible;
  143. opacity: 1;
  144. }
  145. `;
  146.  
  147. var addBox = document.querySelector('form.add_form[name="artists"]');
  148. if (addBox == null) return;
  149. btnAdd = document.createElement('input');
  150. btnAdd.id = 'add-artists-from-selection';
  151. btnAdd.value = 'Extract from selection';
  152. btnAdd.onclick = add_from_selection;
  153. btnAdd.type = 'button';
  154. btnAdd.style.marginLeft = '5px';
  155. btnAdd.style.visibility = 'hidden';
  156. addBox.appendChild(btnAdd);
  157.  
  158. var style = document.createElement('style');
  159. document.head.appendChild(style);
  160. style.id = 'artist-parser-form';
  161. style.type = 'text/css';
  162. style.innerHTML = styleSheet;
  163. var el, elem = [];
  164. elem.push(document.createElement('div'));
  165. elem[elem.length - 1].className = 'modal';
  166. elem[elem.length - 1].id = 'add-from-selection-form';
  167. modal = elem[0];
  168. elem.push(document.createElement('div'));
  169. elem[elem.length - 1].className = 'modal-content';
  170. elem.push(document.createElement('input'));
  171. elem[elem.length - 1].id = 'btnFill';
  172. elem[elem.length - 1].type = 'submit';
  173. elem[elem.length - 1].value = 'Capture';
  174. elem[elem.length - 1].style = "position: fixed; right: 30px; width: 80px; top: 30px;";
  175. elem[elem.length - 1].onclick = doParse;
  176. elem.push(document.createElement('input'));
  177. elem[elem.length - 1].id = 'btnCancel';
  178. elem[elem.length - 1].type = 'button';
  179. elem[elem.length - 1].value = 'Cancel';
  180. elem[elem.length - 1].style = "position: fixed; right: 30px; width: 80px; top: 65px;";
  181. elem[elem.length - 1].onclick = closeModal;
  182. var presetIndex = 0;
  183. function addPreset(val, label = 'Custom', rx = null, order = [1, 2]) {
  184. elem.push(document.createElement('div'));
  185. el = document.createElement('input');
  186. elem[elem.length - 1].style.paddingBottom = '10px';
  187. el.id = 'parse-preset-' + val;
  188. el.name = 'parse-preset';
  189. el.value = val;
  190. if (val == 1) el.checked = true;
  191. el.type = 'radio';
  192. el.onchange = update_custom_ctrls;
  193. if (rx) {
  194. el.rx = rx;
  195. el.order = order;
  196. }
  197. if (val == 999) btnCustom = el;
  198. elem[elem.length - 1].appendChild(el);
  199. el = document.createElement('label');
  200. el.style.marginLeft = '10px';
  201. el.style.marginRight = '10px';
  202. el.htmlFor = 'parse-preset-' + val;
  203. el.className = 'lbl';
  204. el.innerHTML = label;
  205. elem[elem.length - 1].appendChild(el);
  206. if (val != 999) return;
  207. el = document.createElement('input');
  208. el.type = 'text';
  209. el.id = 'custom-pattern';
  210. el.style.width = '20rem';
  211. el.style.fontFamily = 'monospace';
  212. el.autoComplete = "on";
  213. addTooltip(el, 'RegExp to parse lines, first two captured groups are used');
  214. customCtrls.push(elem[elem.length - 1].appendChild(el));
  215. el = document.createElement('input');
  216. el.type = 'radio';
  217. el.name = 'parse-order';
  218. el.id = 'parse-order-1';
  219. el.value = 1;
  220. el.checked = true;
  221. el.style.marginLeft = '1rem';
  222. addTooltip(el, 'Captured regex groups assigned in order $1: artist(s), $2: assignment');
  223. customCtrls.push(elem[elem.length - 1].appendChild(el));
  224. el = document.createElement('label');
  225. el.htmlFor = 'parse-order-1';
  226. el.textContent = '→';
  227. el.style.marginLeft = '5px';
  228. elem[elem.length - 1].appendChild(el);
  229. el = document.createElement('input');
  230. el.type = 'radio';
  231. el.name = 'parse-order';
  232. el.id = 'parse-order-2';
  233. el.value = 2;
  234. el.style.marginLeft = '10px';
  235. addTooltip(el, 'Captured regex groups assigned in order $1: assignment, $2: artist(s)');
  236. customCtrls.push(elem[elem.length - 1].appendChild(el));
  237. el = document.createElement('label');
  238. el.htmlFor = 'parse-order-2';
  239. el.textContent = '←';
  240. el.style.marginLeft = '5px';
  241. elem[elem.length - 1].appendChild(el);
  242. }
  243. addPreset(++presetIndex, escapeHTML('<artist(s)> - <assignment>]'), /^\s*(.+?)(?:\:|\s+[\-\−\—\~\–]+\s+(.*?))?\s*$/);
  244. addPreset(++presetIndex, escapeHTML('<artist>[, <assignment>]') +
  245. '<span style="font-family: initial;">&nbsp;&nbsp;<i>(HRA style)</i></span>', /^\s*(.+?)(?:\:|\s*,\s*(.*?))?\s*$/);
  246. addPreset(++presetIndex, escapeHTML('<artist(s)>[: <assignment>]'), /^\s*(.+?)(?:\:|\s*:+\s*(.*?))?(?:\s*,)?\s*$/);
  247. addPreset(++presetIndex, escapeHTML('<artist(s)>[ (<assignment>)]'), /^\s*(.+?)(?:\:|\s+(?:\([^\(\)]+\)|\[[^\[\]]+\]|\{[^\{\}]+\}))?(?:\s*,)?\s*$/);
  248. addPreset(++presetIndex, escapeHTML('[<assignment> - ]<artist(s)>'), /^\s*(?:(.*?)\s+[\-\−\—\~\–]+\s+)?(.+?)\:?(?:\s*,)?\s*$/, [2, 1]);
  249. addPreset(++presetIndex, escapeHTML('[<assignment>: ]<artist(s)>'), /^\s*(?:(.*?)\s*:+\s*)?(.+?)\:?(?:\s*,)?\s*$/, [2, 1]);
  250. addPreset(++presetIndex, escapeHTML('<artist>[ / <assignment>]'), /^\s*(.+?)(?:\:|\s*\/+\s*(.*?))?(?:\s*,)?\s*$/);
  251. addPreset(++presetIndex, escapeHTML('<artist>[; <assignment>]'), /^\s*(.+?)(?:\:|\s*;\s*(.*?))?(?:\s*,)?\s*$/);
  252. addPreset(++presetIndex, escapeHTML('[<assignment> / ]<artist(s)>'), /^\s*(?:(.*?)\s*\/+\s*)?(.+?)\:?(?:\s*,)?\s*$/, [2, 1]);
  253. addPreset(++presetIndex, '<span style="font-family: initial;">From tracklist</span>', /^\s*\d+(?:\s*[\-\−\—\~\–\.\:]\s*|\s+)(.+?)\s*$/, []);
  254. addPreset(999);
  255. elem.slice(2).forEach(k => { elem[1].appendChild(k) });
  256. elem[0].appendChild(elem[1]);
  257. document.body.appendChild(elem[0]);
  258. window.addEventListener("click", windowOnClick);
  259. document.addEventListener('selectionchange', () => {
  260. var cs = window.getComputedStyle(modal);
  261. if (!btnAdd || window.getComputedStyle(modal).visibility != 'hidden') return;
  262. var sel = document.getSelection();
  263. ShowHideAddbutton();
  264. });
  265. })();
  266.  
  267. function add_from_selection() {
  268. sel = document.getSelection();
  269. if (sel.isCollapsed || modal == null) return;
  270. prefs.set('preset', 1);
  271. prefs.set('custom_pattern', '^\\s*(.+?)(?:\\s*:+\\s*(.*?)|\\:)?\\s*$');
  272. prefs.set('custom_pattern_order', 1);
  273. setRadiosValue('parse-preset', prefs.preset);
  274. customCtrls[0].value = prefs.custom_pattern;
  275. setRadiosValue('parse-order', prefs.custom_pattern_order);
  276. sel = sel.toString();
  277. update_custom_ctrls();
  278. modal.classList.add("show-modal");
  279. }
  280.  
  281. function doParse(expr, flags = '') {
  282. closeModal();
  283. if (!sel) return;
  284. let preset = getSelectedRadio('parse-preset');
  285. if (preset == null) return;
  286. prefs.preset = preset.value;
  287. let order = preset.order;
  288. let custom_parse_order = getSelectedRadio('parse-order');
  289. let rx = preset.rx;
  290. if (!rx && preset.value == 999 && custom_parse_order != null) {
  291. rx = new RegExp(customCtrls[0].value);
  292. order = custom_parse_order != null ?
  293. custom_parse_order.value == 1 ? [1, 2] : custom_parse_order.value == 2 ? [2, 1] : null : [1, 2];
  294. }
  295. let artists = [
  296. 'artist_main', 'artist_guest', 'artists_remix', 'artists_composers',
  297. 'artists_conductors', 'artists_dj', 'artists_producer',
  298. ].map(category => Array.from(document.querySelectorAll(`ul#artist_list > li.${category} > a`)).map(a => a.textContent.trim()));
  299. cleanupArtistsForm();
  300. artistIndex = 0;
  301. sel.split(/(?:\r?\n)+/).forEach(function(line) {
  302. if (!line || !line.trim()) return;
  303. if (/^\s*(?:Recorded|Mastered)\b/i.test(line)) return;
  304. line = line.replace(/\s+\(tracks?\b[^\(\)]+\)/, '').replace(/\s+\[tracks?\b[^\[\]]+\]/, '')
  305. let matches = /^\s*(?:Produced)[ \-\−\—\~\–]by (.+?)\s*$/.exec(line);
  306. if (matches != null) splitAmpersands(matches[1]).forEach(producer => { addArtist(producer, 7) });
  307. else if (rx instanceof RegExp && (matches = rx.exec(line)) != null) {
  308. if (!Array.isArray(order) || order.length < 2) {
  309. let title = matches[1];
  310. const featArtistParsers = [
  311. ///\s+(?:meets)\s+(.+?)\s*$/i,
  312. /* 0 */ /\s+(?:[Ww]ith|[Aa]vec)\s+(?!his\b|her\b|Friends$|Strings$)(.+?)\s*$/,
  313. /* 1 */ /(?:\s+[\-\−\—\–\_])?\s+(?:[Ff]eaturing\s+|(?:[Ff]eat|[Ff]t|FT)\.\s*|[Ff]\.\/\s*)([^\(\)\[\]\{\}]+?)(?=\s*(?:[\(\[\{].*)?$)/,
  314. /* 2 */ /\s+\[\s*f(?:eat(?:\.|uring)|t\.|\.\/)\s+([^\[\]]+?)\s*\]/i,
  315. /* 3 */ /\s+\(\s*f(?:eat(?:\.|uring)|t\.|\.\/)\s+([^\(\)]+?)\s*\)/i,
  316. /* 4 */ /\s+\[\s*(?:(?:en\s+)?duo\s+)?avec\s+([^\[\]]+?)\s*\]/i,
  317. /* 5 */ /\s+\(\s*(?:(?:en\s+)?duo\s+)?avec\s+([^\(\)]+?)\s*\)/i,
  318. /* 6 */ /\s+\[\s*(?:with|w\/)\s+(?![Hh]is\b|[Hh]er\b|Friends$|Strings$)([^\[\]]+?)\s*\]/,
  319. /* 7 */ /\s+\(\s*(?:with|w\/)\s+(?![Hh]is\b|[Hh]er\b|Friends$|Strings$)([^\(\)]+?)\s*\)/,
  320. ];
  321. if (/^(.+?) - /.test(title)) {
  322. let artist = RegExp.$1;
  323. if ((matches = featArtistParsers.slice(0, 3).reduce((m, rx) => m || rx.exec(artist), null)) != null) {
  324. splitAmpersands(artist.slice(0, matches.index)).forEach(artist => { addArtist(artist, 1) });
  325. splitAmpersands(matches[1]).forEach(artist => { addArtist(artist, 2) });
  326. } else splitAmpersands(artist).forEach(artist => { addArtist(artist, 1) });
  327. }
  328. if ((matches = featArtistParsers.slice(1).reduce((m, rx) => m || rx.exec(title), null)) != null)
  329. splitAmpersands(matches[1]).forEach(artist => { addArtist(artist, 2) });
  330. const remixParsers = [
  331. /\s+\(([^\(\)]+?)[\'\’\`]s[^\(\)]*\s(?:(?:Re)?Mix|Reworx)\)/i,
  332. /\s+\[([^\[\]]+?)[\'\’\`]s[^\[\]]*\s(?:(?:Re)?Mix|Reworx)\]/i,
  333. /\s+\(([^\(\)]+?)\s+(?:(?:Extended|Enhanced)\s+)?(?:Remix|Reworx)\)/i,
  334. /\s+\[([^\[\]]+?)\s+(?:(?:Extended|Enhanced)\s+)?(?:Remix|Reworx)\]/i,
  335. /\s+\(Remix(?:ed)?\s+by\s+([^\(\)]+)\)/i,
  336. /\s+\[Remix(?:ed)?\s+by\s+([^\[\]]+)\]/i,
  337. ];
  338. if ((matches = remixParsers.reduce((m, rx) => m || rx.exec(title), null)) != null)
  339. splitAmpersands(matches[1]).forEach(remixer => { addArtist(remixer, 3) });
  340. } else if (matches[order[0]]) {
  341. let role = deduceArtist(matches[order[1]]);
  342. splitAmpersands(matches[order[0]]).forEach(artist => { addArtist(artist, role) });
  343. } else splitAmpersands(matches[order[1]]).forEach(artist => { addArtist(artist, 2) });
  344. }
  345. });
  346. prefs.custom_pattern = customCtrls[0].value;
  347. prefs.custom_pattern_order = custom_parse_order != null ? custom_parse_order.value : 1;
  348. for (let i in prefs) { if (typeof prefs[i] != 'function') GM_setValue(i, prefs[i]) }
  349. return;
  350.  
  351. function deduceArtist(str) {
  352. if (/\b(?:remix)/i.test(str)) return 3; // remixer
  353. if (/\b(?:composer|libretto|lyric\w*|written[ \-\−\—\~\–]by)\b/i.test(str)) return 4; // composer
  354. if (/\b(?:conduct|director\b|direction\b)/i.test(str)) return 5; // conductor
  355. if (/\b(?:compiler\b)/i.test(str)) return 6; // compiler
  356. if (/\b(?:producer\b|produced[ \-\−\—\~\–]by\b)/i.test(str)) return 7; // producer
  357. return 2;
  358. }
  359.  
  360. function addArtist(name, type = 1) {
  361. if (!name || !(type > 0)) return false;
  362. if (pseudoArtistParsers.some(rx => rx.test(name))) return false;
  363. // avoid dupes
  364. if (artists[type - 1].includesCaseless(name)) return false;
  365. if (type == 1 && [4, 5].some(cat => artists[cat].includesCaseless(name))) return false;
  366. if (type == 2 && [0, 4, 5].some(cat => artists[cat].includesCaseless(name))) return false;
  367. let input = getArtistField(artistIndex);
  368. if (input == null) {
  369. AddArtistField();
  370. if ((input = getArtistField(artistIndex)) == null) return false;
  371. }
  372. input.value = name;
  373. input.nextElementSibling.value = type;
  374. artists[type - 1].pushUniqueCaseless(name);
  375. ++artistIndex;
  376. return true;
  377. }
  378. }
  379.  
  380. function getArtistField(index) {
  381. if (!(index >= 0)) return null;
  382. let inputs = document.querySelectorAll('input[name="aliasname[]"]');
  383. return index < inputs.length ? inputs[index] : null;
  384. }
  385.  
  386. function closeModal() {
  387. if (modal == null) return;
  388. ShowHideAddbutton();
  389. modal.classList.remove("show-modal");
  390. }
  391.  
  392. function windowOnClick(event) {
  393. if (modal != null && event.target === modal) closeModal();
  394. }
  395.  
  396. function update_custom_ctrls() {
  397. function en(elem) {
  398. if (elem == null || btnCustom == null) return;
  399. elem.disabled = !btnCustom.checked;
  400. elem.style.opacity = btnCustom.checked ? 1 : 0.5;
  401. }
  402. customCtrls.forEach(k => { en(k) });
  403. }
  404.  
  405. function getSelectedRadio(name) {
  406. for (var i of document.getElementsByName(name)) { if (i.checked) return i }
  407. return null;
  408. }
  409.  
  410. function setRadiosValue(name, val) {
  411. for (var i of document.getElementsByName(name)) { if (i.value == val) i.checked = true }
  412. }
  413.  
  414. function ShowHideAddbutton() {
  415. //btnAdd.style.visibility = document.getSelection().type == 'Range' ? 'visible' : 'hidden';
  416. btnAdd.style.visibility = document.getSelection().isCollapsed ? 'hidden' : 'visible';
  417. }
  418.  
  419. function escapeHTML(string) {
  420. var pre = document.createElement('pre');
  421. var text = document.createTextNode(string);
  422. pre.appendChild(text);
  423. return pre.innerHTML;
  424. }
  425.  
  426. function cleanupArtistsForm() {
  427. document.querySelectorAll('input[name="aliasname[]"]').forEach(function(input) {
  428. input.value = '';
  429. input.nextElementSibling.value = 1;
  430. });
  431. }
  432.  
  433. function addTooltip(elem, text) {
  434. if (elem == null) return;
  435. elem.classList.add('tooltip');
  436. var tt = document.createElement('span');
  437. tt.className = 'tooltiptext';
  438. tt.textContent = text;
  439. elem.appendChild(tt);
  440. }
  441.  
  442. function twoOrMore(artist) { return artist.length >= 2 && !pseudoArtistParsers.some(rx => rx.test(artist)) };
  443. function looksLikeTrueName(artist, index = 0) {
  444. return twoOrMore(artist)
  445. && (index == 0 || !/^(?:his\b|her\b|Friends$|Strings$)/i.test(artist))
  446. && artist.split(/\s+/).length >= 2
  447. && !pseudoArtistParsers.some(rx => rx.test(artist)) || getSiteArtist(artist);
  448. }
  449.  
  450. function strip(art, level = 0) {
  451. return typeof art == 'string' ? [
  452. /\s+(?:aka|AKA)\.?\s+(.*)$/g,
  453. /\s*\([^\(\)]+\)/g,
  454. /\s*\[[^\[\]]+\]/g,
  455. /\s*\{[^\{\}]+\}/g,
  456. ].slice(level).reduce((acc, rx) => acc.replace(rx, ''), art) : undefined;
  457. }
  458.  
  459. function getSiteArtist(artist) {
  460. //if (isOPS) return undefined;
  461. if (!artist || notSiteArtistsCache.includesCaseless(artist)) return null;
  462. var key = Object.keys(siteArtistsCache).find(it => it.toLowerCase() == artist.toLowerCase());
  463. if (key) return siteArtistsCache[key];
  464. var now = Date.now();
  465. try { var apiTimeFrame = JSON.parse(window.localStorage[siteApiTimeframeStorageKey]) } catch(e) { apiTimeFrame = {} }
  466. if (!apiTimeFrame.timeStamp || now > apiTimeFrame.timeStamp + gazelleApiFrame) {
  467. apiTimeFrame.timeStamp = now;
  468. apiTimeFrame.requestCounter = 1;
  469. } else ++apiTimeFrame.requestCounter;
  470. window.localStorage[siteApiTimeframeStorageKey] = JSON.stringify(apiTimeFrame);
  471. if (apiTimeFrame.requestCounter > 5) {
  472. console.debug('getSiteArtist() request exceeding AJAX API time frame: /ajax.php?action=artist&artistname="' +
  473. artist + '" (' + apiTimeFrame.requestCounter + ')');
  474. ++ajaxRejects;
  475. return undefined;
  476. }
  477. try {
  478. var requestUrl = '/ajax.php?action=artist&artistname=' + encodeURIComponent(artist);
  479. xhr.open('GET', requestUrl, false);
  480. //if (isRED && prefs.redacted_api_key) xhr.setRequestHeader('Authorization', prefs.redacted_api_key);
  481. xhr.send();
  482. if (xhr.status == 404) {
  483. notSiteArtistsCache.pushUniqueCaseless(artist);
  484. return null;
  485. }
  486. if (xhr.readyState != XMLHttpRequest.DONE || xhr.status < 200 || xhr.status >= 400) {
  487. console.warn('getSiteArtist("' + artist + '") error:', xhr, 'url:', document.location.origin + requestUrl);
  488. return undefined; // error
  489. }
  490. let response = JSON.parse(xhr.responseText);
  491. if (response.status != 'success') {
  492. notSiteArtistsCache.pushUniqueCaseless(artist);
  493. return null;
  494. }
  495. siteArtistsCache[artist] = response.response;
  496. if (prefs.diag_mode) console.log('getSiteArtist("' + artist + '") success:', siteArtistsCache[artist]);
  497. return (siteArtistsCache[artist]);
  498. } catch(e) {
  499. console.error('UA::getSiteArtist("' + artist + '"):', e, xhr);
  500. return undefined;
  501. }
  502. }
  503.  
  504. function splitArtists(str, parsers = multiArtistParsers) {
  505. var result = [str];
  506. parsers.forEach(function(parser) {
  507. for (var i = result.length; i > 0; --i) {
  508. var j = result[i - 1].split(parser).map(strip);
  509. if (j.length > 1 && j.every(twoOrMore) && !j.some(artist => pseudoArtistParsers.some(rx => rx.test(artist)))
  510. && !getSiteArtist(result[i - 1])) result.splice(i - 1, 1, ...j);
  511. }
  512. });
  513. return result;
  514. }
  515.  
  516. function splitAmpersands(artists) {
  517. if (typeof artists == 'string') var result = splitArtists(strip(artists, 1));
  518. else if (Array.isArray(artists)) result = Array.from(artists); else return [];
  519. ampersandParsers.forEach(function(ampersandParser) {
  520. for (let i = result.length; i > 0; --i) {
  521. let j = result[i - 1].split(ampersandParser).map(strip);
  522. if (j.length <= 1 || !j.every(looksLikeTrueName) || getSiteArtist(result[i - 1])) continue;
  523. result.splice(i - 1, 1, ...j.filter(function(artist) {
  524. return !result.includesCaseless(artist) && !pseudoArtistParsers.some(rx => rx.test(artist));
  525. }));
  526. }
  527. });
  528. return result;
  529. }