TJUPT Music JSON Uploady

import json from red/ops/dic

当前为 2024-04-11 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name TJUPT Music JSON Uploady
  3. // @namespace http://tjupt.org/
  4. // @version 0.1.0
  5. // @description import json from red/ops/dic
  6. // @author Colder
  7. // @match https://*.tjupt.org/upload.php
  8. // @grant none
  9. // @require https://cdn.jsdelivr.net/npm/he@1.2.0/he.js
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. //this is not robust: 'wikiBody to BBCode' for DIC
  17. function wiki2bb(wiki) {
  18. //br
  19. wiki = wiki.replace(/<br \/>/g, '');
  20. //quote
  21. wiki = wiki.replace(/<blockquote>([\s\S]*)<\/blockquote>/g,'[quote]$1[/quote]');
  22. //hide
  23. wiki = wiki.replace(/<strong>(.*?)<\/strong>: <a href="javascript:void\(0\);" onclick="BBCode\.spoiler\(this\);">(.*?)<\/a><blockquote class="hidden spoiler">([\s\S]*)<\/blockquote>/g, '[hide=$1]$3[/hide]');
  24. //b
  25. wiki = wiki.replace(/<strong>(.*?)<\/strong>/g, '[b]$1[/b]');
  26. //quote=xxx (?)
  27. wiki = wiki.replace(/<strong class=\\"quoteheader\\">(.*?)<\/strong> wrote: <blockquote>(.*?)<\/blockquote>/g, '[quote=$1]$2[/quote]');
  28. //url=xxx
  29. wiki = wiki.replace(/<a rel="noreferrer" target="_blank" href="(.*?)">(.*?)<\/a>/g, function(match, p1, p2) {
  30. const url = p1.replace(/\\\//g, '/');
  31. return "[url=" + url + "]" + p2 + "[/url]";
  32. });
  33. //center, then clear unsupported align
  34. wiki = wiki.replace(/<div style=\\"text-align: center;">(.*?)<\/div>/g, "[center]$1[/center]");
  35. wiki = wiki.replace(/<div style=\\"text-align: (.*?);">(.*?)<\/div>/g, "$2");
  36. //img
  37. wiki = wiki.replace(/<img class=\\"scale_image\\" onclick=\\"lightbox\.init\(this, \$(this).width\(\)\);\\" alt=\\"([^"]*)\\" src=\\"([^"]*)\\" \/>/g, function(match, p1, p2) {
  38. const url = p2.replace(/\\\//g, '/');
  39. return '[img]' + url + '[/img]';
  40. });
  41. //artist
  42. wiki = wiki.replace(/<a href=\\"artist\.php\?artistname=(.*?)\\">(.*?)<\/a>/g, '[artist]$2[/artist]');
  43. //size
  44. wiki = wiki.replace(/<span class="size(.?)">([\s\S]*?)<\/span>/g, '[size=$1]$2[/size]');
  45. //italic, then clear other span style
  46. wiki = wiki.replace(/<span style="font-style: italic;">([\s\S]*?)<\/span>/g, '[i]$1[/i]');
  47. wiki = wiki.replace(/<span style=(.*?)>/g, '').replace(/<\/span>/g, '');
  48.  
  49. return wiki;
  50. }
  51.  
  52.  
  53. function replaceUnsupportedBBCode(BB) {
  54. BB = BB.replace(/\[artist\](.*?)\[\/artist\]/g, '$1');
  55. BB = BB.replace(/\[plain\](.*?)\[\/plain\]/g, '$1');
  56. BB = BB.replace(/\[align=center\](.*?)\[\/align\]/g, '[center]$1[/center]');
  57. BB = BB.replace(/\[align(.*?)\]/g, '').replace(/\[\/align\]/g, '');
  58. BB = BB.replace(/\[pad(.*?)\]/g, '').replace(/\[\/pad\]/g, '');
  59.  
  60. return BB;
  61. }
  62.  
  63. function unicodeConvert(string) {
  64. string = string.replace(/\\u([\d\w]{4})/gi, (match, grp) => String.fromCharCode(parseInt(grp, 16)));
  65. string = string.replace(/&#(\d+);/g, (match, dec) => String.fromCharCode(dec));
  66. return string;
  67. }
  68.  
  69. function insertButtons() {
  70. const uploadButton = document.createElement('input');
  71. uploadButton.type = 'file';
  72. uploadButton.id = 'jsonFileUploader';
  73. uploadButton.accept = 'application/json';
  74. uploadButton.addEventListener('change', () => selectMusicCategory());
  75.  
  76. const goButton = document.createElement('button');
  77. goButton.textContent = 'Go';
  78. goButton.type = 'button';
  79. goButton.addEventListener('click', processJsonFile);
  80.  
  81. const targetLocation = document.evaluate('/html/body/table[2]/tbody/tr[2]/td/form/table/tbody/tr[1]/td', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  82. if (targetLocation) {
  83. targetLocation.appendChild(uploadButton);
  84. targetLocation.appendChild(goButton);
  85. }
  86. }
  87.  
  88. function processJsonFile() {
  89. const fileInput = document.getElementById('jsonFileUploader');
  90. if (fileInput.files.length > 0) {
  91. const file = fileInput.files[0];
  92. const reader = new FileReader();
  93. reader.onload = (e) => {
  94. try {
  95. const data = JSON.parse(e.target.result);
  96. fillForm(data);
  97. } catch(error) {
  98. console.error('Error processing JSON file:', error);
  99. }
  100. };
  101. reader.readAsText(file);
  102. }
  103. }
  104.  
  105. function fillForm(data) {
  106. const albumTitle = unicodeConvert(String(data.response?.group?.name || ''));
  107. const remasterYear = String(data.response?.torrent?.remasterYear || '');
  108. const artistNames = data.response?.group?.musicInfo?.artists?.map(artist => unicodeConvert(artist.name)).join(' & ') || '';
  109. const media = unicodeConvert(data.response?.torrent?.media || '');
  110. const format = data.response?.torrent?.format || '';
  111. //encoding
  112. let encoding = unicodeConvert(data.response?.torrent?.encoding || '');
  113. if(encoding === "24bit Lossless") {
  114. encoding = "Hi-Res";
  115. } else if(encoding === "Lossless") {
  116. encoding = "无损";
  117. }
  118. //subHeading
  119. let subHeadingArray = [
  120. unicodeConvert(data.response?.torrent?.remasterRecordLabel),
  121. unicodeConvert(data.response?.torrent?.remasterCatalogueNumber),
  122. unicodeConvert(data.response?.torrent?.remasterTitle)
  123. ];
  124. if (data.response?.torrent?.hasLog && data.response?.torrent?.logScore) {
  125. subHeadingArray.push(`log (${data.response.torrent.logScore}%)`);
  126. }
  127. if (data.response?.torrent?.hasCue) {
  128. subHeadingArray.push("Cue");
  129. }
  130. if (data.response?.torrent?.scene) {
  131. subHeadingArray.push("Scene");
  132. }
  133. const subHeading = subHeadingArray.filter(info => info).join(' / ');
  134. //description
  135. let descr = String('[img]'+(data.response?.group?.wikiImage || '').replace(/\\\//g, '/')+'[/img]\n');
  136. //add torrent description
  137. const torrent_desc = he.decode(unicodeConvert(data.response?.torrent?.description || '').replace(/\\\//g, '/'));
  138. if(torrent_desc !== "") {descr = descr + "\n[quote]" +torrent_desc + "[/quote]\n";}
  139. //torrent group description
  140. const bbBody = replaceUnsupportedBBCode(he.decode(unicodeConvert(data.response?.group?.bbBody || '')));
  141. const wikiBBcode = replaceUnsupportedBBCode(he.decode(unicodeConvert(data.response?.group?.wikiBBcode || '')));
  142. if (bbBody) {descr += bbBody;}
  143. else if (wikiBBcode) {descr += wikiBBcode;}
  144. else { //parse wikiBody for DIC
  145. const bb4dic = wiki2bb(he.decode(unicodeConvert(data.response?.group?.wikiBody || '')));
  146. descr += bb4dic;
  147. }
  148.  
  149.  
  150. document.getElementById('hqname').value = albumTitle;
  151. document.getElementById('issuedate').value = remasterYear;
  152. document.getElementById('artist').value = artistNames;
  153. document.getElementById('specificcat').value = media;
  154. document.getElementById('format').value = format;
  155. document.getElementById('hqtone').value = encoding;
  156. const smallDescr = document.querySelector('input[type="text"][name="small_descr"]');
  157. if(smallDescr) {
  158. smallDescr.value = subHeading;
  159. }
  160. const currentDesc = document.getElementById('descr');
  161. if(currentDesc && currentDesc.value == ""){
  162. document.getElementById('descr').value = descr;
  163. }
  164. }
  165.  
  166. function selectMusicCategory() {
  167. const categorySelect = document.getElementById('browsecat');
  168. if (categorySelect && categorySelect.value !== "406") {
  169. categorySelect.value = "406";
  170. categorySelect.dispatchEvent(new Event('change'));
  171. }
  172. }
  173.  
  174. insertButtons();
  175. })();