Gazelle extract featured artists from description

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

当前为 2020-10-03 提交的版本,查看 最新版本

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