Supraphonline foobar2000 tagging support

Kopíruje metadata alba ve formátu pro aplikaci tagů ve foobar2000

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

  1. // ==UserScript==
  2. // @name Supraphonline foobar2000 tagging support
  3. // @name:cs Supraphonline podpora tagování ve foobar2000
  4. // @name:en Supraphonline foobar2000 tagging support
  5. // @namespace https://greasyfork.org/cs/users/321857-anakunda
  6. // @version 1.3
  7. // @description Kopíruje metadata alba ve formátu pro aplikaci tagů ve foobar2000
  8. // @description:cs Kopíruje metadata alba ve formátu pro aplikaci tagů ve foobar2000
  9. // @description:en Copies album metadata to clipboard in machine parseable format
  10. // @author Já, Osobně
  11. // @copyright 2020, Anakunda (https://greasyfork.org/cs/users/321857-anakunda)
  12. // @license GPL-3.0-or-later
  13. // @iconURL https://www.supraphonline.cz/favicon.ico
  14. // @match http*://*.supraphonline.cz/album/*
  15. // @grant GM_xmlhttpRequest
  16. // @grant GM_setClipboard
  17. // @grant GM_getValue
  18. // @grant GM_getValue
  19. // @grant GM_deleteValue
  20. // @require https://greasyfork.org/scripts/408084-xhrlib/code/xhrLib.js
  21.  
  22. // ==/UserScript==
  23.  
  24. // Výraz pro 'Automatically Fill Values' funkci ve foobaru2000:
  25. // %album artist%%album%%date%%releasedate%%genre%%label%%catalog%%discnumber%%totaldiscs%%discsubtitle%%tracknumber%%totaltracks%%artist%%title%%performer%%composer%%media%%comment%%url%
  26.  
  27. 'use strict';
  28.  
  29. Array.prototype.pushUnique = function(...items) {
  30. if (Array.isArray(items) && items.length > 0) items.forEach(it => { if (!this.includes(it)) this.push(it) });
  31. return this.length;
  32. };
  33. Array.prototype.pushUniqueCaseless = function(...items) {
  34. if (Array.isArray(items) && items.length > 0) items.forEach(it => { if (!this.includesCaseless(it)) this.push(it) });
  35. return this.length;
  36. };
  37. Array.prototype.equalTo = function(arr) {
  38. return Array.isArray(arr) && arr.length == this.length
  39. && Array.from(arr).sort().toString() == Array.from(this).sort().toString();
  40. };
  41. Array.prototype.equalCaselessTo = function(arr) {
  42. function adjust(elem) { return typeof elem == 'string' ? elem.toLowerCase() : elem }
  43. return Array.isArray(arr) && arr.length == this.length
  44. && arr.map(adjust).sort().toString() == this.map(adjust).sort().toString();
  45. };
  46.  
  47. var hTimer = setInterval(function() {
  48. var ref = document.querySelector('form.table-action');
  49. if (ref == null) return;
  50. clearInterval(hTimer);
  51. var child = document.createElement('button');
  52. child.id = 'copy-info-to-clipboard';
  53. child.textContent = 'Kopírovat do schránky';
  54. child.type = 'button';
  55. child.name = 'copy-info-to-clipboard';
  56. child.className = 'btn btn-danger topframe_login';
  57. child.style.marginRight = '10px';
  58. child.onclick = fetchAlbum;
  59. ref.prepend(child);
  60. }, 1000);
  61.  
  62. function fetchAlbum(evt) {
  63. var original_text = evt.target.textContent;
  64. evt.target.disabled = true;
  65. evt.target.textContent = 'Pracuji...';
  66.  
  67. let tracks = [], discNumber, discSubtitle, domParser = new DOMParser(), ref, media, encoding, format, bitdepth,
  68. trackIdentifiers, releaseDate, totalDiscs, samplerate, catalogue, imgUrl, album, totalTracks, albumYear,
  69. label, identifiers = {};
  70. const vaParser = /^(?:Various(?:\s+Artists)?|VA|\<various\s+artists\>|Různí(?:\s+interpreti)?)$/i;
  71. const pseudoArtistParsers = [
  72. /^(?:#??N[\/\-]?A|[JS]r\.?)$/i,
  73. /^(?:traditional|trad\.|lidová)$/i,
  74. /\b(?:traditional|trad\.|lidová)$/,
  75. /^(?:tradiční|lidová)\s+/,
  76. /^(?:[Aa]nonym)/,
  77. /^(?:[Ll]iturgical\b|[Ll]iturgick[áý])/,
  78. /^(?:auditorium|[Oo]becenstvo|[Pp]ublikum)$/,
  79. /^(?:Various\s+Composers)$/i,
  80. /^(?:Guests|Friends)$/i,
  81. ];
  82. const VA = 'Various Artists';
  83.  
  84. if (/\/album\/(\d+)\b/i.test(document.URL)) identifiers.SUPRAPHONLINE_ID = parseInt(RegExp.$1);
  85. let artist = Array.from(document.querySelectorAll('div.visible-lg-block > h2.album-artist > a'))
  86. .map(a => a.title || a.textContent.trim());
  87. let isVA = (ref = document.querySelector('span[itemprop="byArtist"] > meta[itemprop="name"]')) != null ?
  88. vaParser.test(ref.content) : artist.length <= 0;
  89. if ((ref = document.querySelector('h1[itemprop="name"]')) != null) album = ref.firstChild.data.trim();
  90. if ((ref = document.querySelector('meta[itemprop="numTracks"]')) != null) totalTracks = parseInt(ref.content);
  91. let genres = (ref = document.querySelector('meta[itemprop="genre"]')) != null ? ref.content : undefined;
  92. if ((ref = document.querySelector('li.album-version > div.selected > div')) != null) {
  93. if (/\b(?:MP3)\b/.test(ref.textContent)) {
  94. media = 'WEB'; encoding = 'lossy'; format = 'MP3';
  95. }
  96. if (/\b(?:FLAC)\b/.test(ref.textContent)) {
  97. media = 'WEB'; encoding = 'lossless'; format = 'FLAC'; bitdepth = 16;
  98. }
  99. if (/\b(?:Hi[\s\-]*Res)\b/.test(ref.textContent)) {
  100. media = 'WEB'; encoding = 'lossless'; format = 'FLAC'; bitdepth = 24;
  101. }
  102. if (/\b(?:CD)\b/.test(ref.textContent)) media = 'CD';
  103. if (/\b(?:LP)\b/.test(ref.textContent)) media = 'Vinyl';
  104. }
  105. const copyrightParser = /^(?:\([PC]\)|℗|©)$/i;
  106. document.querySelectorAll('ul.summary > li').forEach(function(li) {
  107. if (li.childElementCount <= 0) return;
  108. let key = li.firstElementChild.textContent, value = li.lastChild.textContent.trim();
  109. if (key.includes('Nosič')) media = value;
  110. if (key.includes('Datum vydání')) releaseDate = normalizeDate(value, 'cs');
  111. if (key.includes('První vydání')) albumYear = extractYear(value);
  112. if (key.includes('Žánr')) genres = translateGenre(value);
  113. if (key.includes('Vydavatel')) label = value;
  114. if (key.includes('Katalogové číslo')) catalogue = value;
  115. if (key.includes('Formát')) {
  116. if (/\b(?:FLAC|WAV|AIFF?)\b/.test(value)) { encoding = 'lossless'; format = 'FLAC' }
  117. if (/\b(\d+)[\-\s]?bits?\b/i.test(value)) bitdepth = parseInt(RegExp.$1);
  118. if (/\b([\d\.\,]+)[\-\s]?kHz\b/.test(value)) samplerate = parseFloat(RegExp.$1.replace(',', '.')) * 1000;
  119. }
  120. //if (key.includes('Celková stopáž')) totalTime = timeStringToTime(value);
  121. if (copyrightParser.test(key) && !albumYear) albumYear = extractYear(value);
  122. });
  123. const creators = ['autoři', 'interpreti', 'tělesa', 'digitalizace'];
  124. let artists = [], ndx;
  125. for (let i = 0; i < creators.length; ++i) artists[i] = {};
  126. document.querySelectorAll('ul.sidebar-artist > li').forEach(function(it) {
  127. if ((ref = it.querySelector('h3')) != null) {
  128. ndx = undefined;
  129. creators.forEach((it, _ndx) => { if (ref.textContent.includes(it)) ndx = _ndx });
  130. } else {
  131. if (typeof ndx != 'number') return;
  132. if (ndx == 2) var role = 'ensemble';
  133. else if ((ref = it.querySelector('span')) != null) role = translateRole(ref);
  134. if ((ref = it.querySelector('a')) != null) {
  135. if (!Array.isArray(artists[ndx][role])) artists[ndx][role] = [];
  136. artists[ndx][role].pushUnique([ref.textContent.trim(), origin + ref.pathname]);
  137. }
  138. }
  139. });
  140. let description = Array.from(document.querySelectorAll('div[itemprop="description"] p'))
  141. .map(p => p.textContent.trim()).join('\n\n').replace(/\s+/g, ' ');
  142. let performers = [], composer = [], conductor = [], DJs = [], volMedia;
  143. for (let i = 1; i < 3; ++i) Object.keys(artists[i]).forEach(function(role) { // performers
  144. var a = artists[i][role].map(a => a[0]);
  145. artist.pushUnique(...a);
  146. ([
  147. 'conductor',
  148. 'choirmaster',
  149. 'director',
  150. ].includes(role) ? conductor : role == 'DJ' ? DJs : performers).pushUnique(...a);
  151. });
  152. Object.keys(artists[0]).forEach(function(role) { // composers
  153. composer.pushUnique(...artists[0][role].map(it => it[0])
  154. .filter(it => !pseudoArtistParsers.some(rx => rx.test(it))));
  155. });
  156. if ((ref = document.querySelector('meta[itemprop="image"]')) != null) imgUrl = ref.content.replace(/\?.*$/, '');
  157. document.querySelectorAll('table.table-tracklist > tbody > tr').forEach(function(tr, index) {
  158. if (tr.classList.contains('cd-header') && (ref = tr.querySelector('td > h3')) != null
  159. && /\b(?:(\S*?)\s*)?(\d+)\b/.test(ref.textContent)) {
  160. volMedia = RegExp.$1 ? RegExp.lastMatch : undefined;
  161. discNumber = parseInt(RegExp.$2) || undefined;
  162. }
  163. if (tr.classList.contains('song-header') && (ref = tr.querySelector('td')) != null)
  164. discSubtitle = ref.title || ref.textContent.trim();
  165. if (tr.classList.contains('track') && tr.id) {
  166. trackIdentifiers = {
  167. TRACK_ID: /^(?:track)-(\d+)$/i.test(tr.id) ? parseInt(RegExp.$1) : undefined,
  168. };
  169. if (volMedia) trackIdentifiers.VOL_MEDIA = volMedia;
  170. let track = {
  171. artist: isVA ? VA : undefined,
  172. artists: !isVA ? artist : undefined,
  173. album: album,
  174. album_year: /*trackYear || */albumYear || undefined,
  175. release_date: releaseDate,
  176. label: label,
  177. catalog: catalogue,
  178. encoding: encoding,
  179. codec: format,
  180. bitdepth: bitdepth,
  181. samplerate: samplerate || undefined,
  182. media: media,
  183. genre: genres,
  184. disc_number: discNumber,
  185. total_discs: totalDiscs,
  186. disc_subtitle: discSubtitle,
  187. track_number: /^\s*(\d+)\.?\s*$/.test(tr.children[0].firstChild.textContent) ?
  188. parseInt(RegExp.$1) || RegExp.$1 : undefined,
  189. total_tracks: totalTracks,
  190. title: (ref = tr.querySelector('meta[itemprop="name"][content]')) != null ? ref.content
  191. : (ref = tr.querySelector('td > a.trackdetail')) != null ? ref.textContent.trim() : undefined,
  192. performers: performers.length > 0 ? performers : undefined,
  193. composers: composer.length > 0 ? composer : undefined,
  194. conductors: conductor.length > 0 ? conductor : undefined,
  195. compilers: DJs.length > 0 ? DJs : undefined,
  196. duration: durationFromMeta(tr),
  197. url: document.location.origin + document.location.pathname,
  198. description: description,
  199. identifiers: mergeIds(),
  200. cover_url: imgUrl,
  201. };
  202. tracks.push((function() {
  203. if ((ref = tr.querySelector('td > a.trackdetail')) == null) return Promise.reject('link not found');
  204. return globalFetch(origin + ref.pathname + ref.search).then(function(response) {
  205. var detail = response.document.querySelector('div[data-swap="trackdetail-' +
  206. track.identifiers.TRACK_ID + '"] > div > div.row');
  207. if (detail == null) return Promise.reject('element not found');
  208. detail.querySelectorAll('div[class]:nth-of-type(1) > ul > li').forEach(function(li) {
  209. var key = li.querySelector('span'), value = li.lastChild.textContent.trim();
  210. if (key != null && value) key = key.textContent.trim(); else return;
  211. if (key.startsWith('Žánr')) track.genre = value;
  212. if (key.startsWith('Nahrávka dokončena')) track.rec_year = extractYear(value);
  213. if (key.startsWith('Místo nahrání')) track.venue = value;
  214. if (key.startsWith('Rok prvního vydání')) track.pub_year = extractYear(value);
  215. if (copyrightParser.test(key)) track.copyright = value;
  216. });
  217. let trackArtists = [];
  218. for (let i = 0; i < 8; ++i) trackArtists[i] = [];
  219. detail.querySelectorAll('div[class]:nth-of-type(2) > ul > li').forEach(function(li) {
  220. var key = li.querySelector('span');
  221. var artists = Array.from(li.getElementsByTagName('a')).map(a => a.textContent.trim())
  222. .filter(artist => !pseudoArtistParsers.some(rx => rx.test(artist)));
  223. if (key != null && artists.length > 0) key = translateRole(key); else return;
  224. function oneOf(...arr) { return arr.some(role => key == role) }
  225. if (key.startsWith('remix'))
  226. trackArtists[2].pushUnique(...artists);
  227. else if (oneOf('music', 'lyrics', 'music+lyrics', 'original lyrics', 'czech lyrics', 'libreto', 'music improvisation', 'author'))
  228. trackArtists[3].pushUnique(...artists);
  229. else if (key == 'DJ')
  230. trackArtists[5].pushUnique(...artists);
  231. else if (key == 'produced by')
  232. trackArtists[6].pushUnique(...artists);
  233. else if (key == 'recorded by') {
  234. } else {
  235. if (oneOf('ensemble') || /\b(?:vocals)\b/.test(key))
  236. trackArtists[0].pushUnique(...artists);
  237. else if (oneOf('conductor', 'choirmaster', 'director'))
  238. trackArtists[4].pushUnique(...artists);
  239. trackArtists[7].pushUnique(...artists.map(artist => `${artist} (${key})`));
  240. }
  241. });
  242. if (trackArtists[0].length > 0 && (isVA || !trackArtists[0].equalCaselessTo(artist)))
  243. track.track_artists = trackArtists[0];
  244. [
  245. [3, 'composer'],
  246. [4, 'conductor'],
  247. [2, 'remixer'],
  248. [5, 'compiler'],
  249. [6, 'producer'],
  250. [7, 'performer'],
  251. ].forEach(def => { if (trackArtists[def[0]].length > 0) track[def[1] + 's'] = trackArtists[def[0]] })
  252. return track;
  253. });
  254. })().catch(function(reason) {
  255. console.error('Supraphonline parser failed to get track', index + 1, 'detail:', reason);
  256. return Promise.resolve(track);
  257. }));
  258. } // track
  259. });
  260. Promise.all(tracks).then(tracks => tracks.map(track => [
  261. isVA ? VA : joinArtists(track.artists) || '',
  262. track.album || '',
  263. track.album_year || track.pub_year || '',
  264. track.release_date || '',
  265. track.genre || '',
  266. track.label || '',
  267. track.catalog || '',
  268. track.disc_number || '',
  269. track.total_discs > 1 ? track.total_discs : '',
  270. track.disc_subtitle || '',
  271. track.track_number || '',
  272. track.total_tracks || '',
  273. (Array.isArray(track.track_artists) && track.track_artists.length > 0 ? joinArtists(track.track_artists)
  274. : Array.isArray(track.artists) && track.artists.length > 0 ? joinArtists(track.artists) : track.artist) || '',
  275. track.title || '',
  276. (track.performers || []).join(', '),
  277. (track.composers || []).join(', '),
  278. track.media,
  279. track.description,
  280. track.url,
  281. ].join('\x1E')).join('\n')).then(clipBoard => { GM_setClipboard(clipBoard, 'text') }).catch(e => { alert(e) }).then(function() {
  282. evt.target.disabled = false;
  283. evt.target.textContent = original_text;
  284. });
  285.  
  286. function translateGenre(genre) {
  287. if (!genre || typeof genre != 'string') return undefined;
  288. [
  289. ['Orchestrální hudba', 'Orchestral Music'],
  290. ['Komorní hudba', 'Chamber Music'],
  291. ['Vokální', 'Classical, Vocal'],
  292. ['Klasická hudba', 'Classical'],
  293. ['Melodram', 'Classical, Melodram'],
  294. ['Symfonie', 'Symphony'],
  295. ['Vánoční hudba', 'Christmas Music'],
  296. [/^(?:Alternativ(?:ní|a))$/i, 'Alternative'],
  297. ['Dechová hudba', 'Brass Music'],
  298. ['Elektronika', 'Electronic'],
  299. ['Folklor', 'Folclore, World Music'],
  300. ['Instrumentální hudba', 'Instrumental'],
  301. ['Latinské rytmy', 'Latin'],
  302. ['Meditační hudba', 'Meditative'],
  303. ['Vojenská hudba', 'Military Music'],
  304. ['Pro děti', 'Children'],
  305. ['Pro dospělé', 'Adult'],
  306. ['Mluvené slovo', 'Spoken Word'],
  307. ['Audiokniha', 'audiobook'],
  308. ['Humor', 'humour'],
  309. ['Pohádka', 'Fairy-Tale'],
  310. ].forEach(function(subst) {
  311. if (typeof subst[0] == 'string' && genre.toLowerCase() == subst[0].toLowerCase()
  312. || subst[0] instanceof RegExp && subst[0].test(genre)) genre = subst[1];
  313. });
  314. return genre;
  315. }
  316.  
  317. function translateRole(elem) {
  318. return elem instanceof HTMLElement ? [
  319. [/\b(?:klavír)\b/, 'piano'],
  320. [/\b(?:housle)\b/, 'violin'],
  321. [/\b(?:varhany)\b/, 'organ'],
  322. [/\b(?:cembalo)\b/, 'harpsichord'],
  323. [/\b(?:trubka)\b/, 'trumpet'],
  324. [/\b(?:soprán)\b/, 'soprano'],
  325. [/\b(?:alt)\b/, 'alto'],
  326. [/\b(?:baryton)\b/, 'baritone'],
  327. [/\b(?:bas)\b/, 'basso'],
  328. [/\b(?:syntezátor)\b/, 'synthesizer'],
  329. [/\b(?:zpěv)\b/, 'vocals'],
  330. [/\b(?:čte|četba)\b/, 'narration'],
  331. [/\b(?:akustická kytara)\b/, 'acoustic guitar'],
  332. [/\b(?:kytara)\b/, 'guitar'],
  333. ['hudební těleso', 'ensemble'],
  334. ['vypravuje', 'narration'],
  335. ['komentář', 'commentary'],
  336. ['hovoří a zpívá', 'speaks and sings'],
  337. ['hovoří', 'spoken by'],
  338. ['improvizace', 'improvisation'],
  339. ['původní text', 'original lyrics'],
  340. ['hudba+text', 'music+lyrics'],
  341. ['český text', 'czech lyrics'],
  342. ['text', 'lyrics'],
  343. ['hudba', 'music'],
  344. ['hudební improvizace', 'music improvisation'],
  345. ['autor', 'author'],
  346. ['účinkuje', 'participating'],
  347. ['dirigent', 'conductor'],
  348. ['sbormistr', 'choirmaster'],
  349. ['řídí', 'director'],
  350. ['produkce', 'produced by'],
  351. ['nahrál', 'recorded by'],
  352. ['digitální přepis', 'A/D transfer'],
  353. ].reduce((r, def) => r.replace(...def), elem.textContent.trim().toLowerCase().replace(/\s*:.*$/, '')) : undefined;
  354. }
  355.  
  356. function mergeIds() {
  357. var r = Object.assign({}, identifiers, trackIdentifiers);
  358. trackIdentifiers = {};
  359. return r;
  360. }
  361. }
  362.  
  363. function joinArtists(arr, decorator = artist => artist) {
  364. if (!Array.isArray(arr)) return null;
  365. if (arr.some(artist => artist.includes('&'))) return arr.map(decorator).join(', ');
  366. if (arr.length < 3) return arr.map(decorator).join(' & ');
  367. return arr.slice(0, -1).map(decorator).join(', ') + ' & ' + decorator(arr.slice(-1).pop());
  368. }
  369.  
  370. function timeStringToTime(str) {
  371. if (!/(-\s*)?\b(\d+(?::\d{2})*(?:\.\d+)?)\b/.test(str)) return null;
  372. var t = 0, a = RegExp.$2.split(':');
  373. while (a.length > 0) t = t * 60 + parseFloat(a.shift());
  374. return RegExp.$1 ? -t : t;
  375. }
  376.  
  377. function normalizeDate(str, countryCode = undefined) {
  378. if (typeof str != 'string') return null;
  379. var match;
  380. function formatOutput(yearIndex, montHindex, dayIndex) {
  381. var year = parseInt(match[yearIndex]), month = parseInt(match[montHindex]), day = parseInt(match[dayIndex]);
  382. if (year < 30) year += 2000; else if (year < 100) year += 1900;
  383. if (year < 1000 || year > 9999 || month < 1 || month > 12 || day < 0 || day > 31) return null;
  384. return year.toString() + '-' + month.toString().padStart(2, '0') + '-' + day.toString().padStart(2, '0');
  385. }
  386. if ((match = /\b(\d{4})-(\d{1,2})-(\d{1,2})\b/.exec(str)) != null) return formatOutput(1, 2, 3); // US
  387. if ((match = /\b(\d{4})\/(\d{1,2})\/(\d{1,2})\b/.exec(str)) != null) return formatOutput(1, 2, 3);
  388. if ((match = /\b(\d{1,2})\/(\d{1,2})\/(\d{2})\b/.exec(str)) != null
  389. && (parseInt(match[1]) > 12 || /\b(?:be|it)/.test(countryCode))) return formatOutput(3, 2, 1); // BE, IT
  390. if ((match = /\b(\d{1,2})\/(\d{1,2})\/(\d{2})\b/.exec(str)) != null) return formatOutput(3, 1, 2); // US
  391. if ((match = /\b(\d{1,2})\/(\d{1,2})\/(\d{4})\b/.exec(str)) != null) return formatOutput(3, 2, 1); // UK, IE, FR, ES
  392. if ((match = /\b(\d{1,2})-(\d{1,2})-((?:\d{2}|\d{4}))\b/.exec(str)) != null) return formatOutput(3, 2, 1); // NL
  393. if ((match = /\b(\d{1,2})\. *(\d{1,2})\. *(\d{4})\b/.exec(str)) != null) return formatOutput(3, 2, 1); // CZ, DE
  394. if ((match = /\b(\d{1,2})\. *(\d{1,2})\. *(\d{2})\b/.exec(str)) != null) return formatOutput(3, 2, 1); // AT, CH, DE, LU
  395. if ((match = /\b(\d{4})\. *(\d{1,2})\. *(\d{1,2})\b/.exec(str)) != null) return formatOutput(1, 2, 3); // JP
  396. return extractYear(str);
  397. }
  398.  
  399. function extractYear(expr) {
  400. if (typeof expr == 'number') return Math.round(expr);
  401. if (typeof expr != 'string') return null;
  402. if (/\b(\d{4})\b/.test(expr)) return parseInt(RegExp.$1);
  403. var d = new Date(expr);
  404. return parseInt(isNaN(d) ? expr : d.getFullYear());
  405. }
  406.  
  407. function durationFromMeta(elem) {
  408. if (!(elem instanceof HTMLElement)) return undefined;
  409. let meta = elem.querySelector('meta[itemprop="duration"][content]');
  410. if (meta == null) return undefined;
  411. let m = /^PT?(?:(?:(\d+)H)?(\d+)M)?(\d+)S$/i.exec(meta.content);
  412. if (m != null)
  413. return (parseInt(RegExp.$1) || 0) * 60**2 + (parseInt(RegExp.$2) || 0) * 60 + (parseInt(RegExp.$3) || 0);
  414. m = timeStringToTime(meta.content);
  415. return m != null ? m : undefined;
  416. }