TJUPT Music JSON Uploady

import json from red/ops/dic

  1. // ==UserScript==
  2. // @name TJUPT Music JSON Uploady
  3. // @namespace http://tjupt.org/
  4. // @version 0.1.2
  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>: <strong>\u9690\u85cf\u5185\u5bb9<\/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>([\s\S]*?)<\/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;">([\s\S]*?)<\/div>/g, "[center]$1[/center]");
  35. wiki = wiki.replace(/<div style="text-align: (.*?);">([\s\S]*?)<\/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, '$2');
  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. //<li>
  49. wiki = wiki.replace(/<li>(.*?)<\/li>/g, '· $1\n');
  50. //<ul>
  51. wiki = wiki.replace(/<ul(.*?)>([\s\S]*?)<\/ul>/g,'$2');
  52. //<pre>
  53. wiki = wiki.replace(/<pre>([\s\S]*?)<\/pre>/g,'$1');
  54.  
  55. return wiki;
  56. }
  57.  
  58. function replaceUnsupportedBBCode(BB) {
  59. BB = BB.replace(/\[artist\](.*?)\[\/artist\]/g, '$1');
  60. BB = BB.replace(/\[plain\](.*?)\[\/plain\]/g, '$1');
  61. BB = BB.replace(/\[align=center\](.*?)\[\/align\]/g, '[center]$1[/center]');
  62. BB = BB.replace(/\[align(.*?)\]/g, '').replace(/\[\/align\]/g, '');
  63. BB = BB.replace(/\[pad(.*?)\]/g, '').replace(/\[\/pad\]/g, '');
  64.  
  65. return BB;
  66. }
  67.  
  68. function unicodeConvert(string) {
  69. string = string.replace(/\\u([\d\w]{4})/gi, (match, grp) => String.fromCharCode(parseInt(grp, 16)));
  70. string = string.replace(/&#(\d+);/g, (match, dec) => String.fromCharCode(dec));
  71. return string;
  72. }
  73.  
  74. function insertButtons() {
  75. const uploadButton = document.createElement('input');
  76. uploadButton.type = 'file';
  77. uploadButton.id = 'jsonFileUploader';
  78. uploadButton.accept = 'application/json';
  79. uploadButton.addEventListener('change', () => selectMusicCategory());
  80.  
  81. const goButton = document.createElement('button');
  82. goButton.textContent = 'Go';
  83. goButton.type = 'button';
  84. goButton.addEventListener('click', processJsonFile);
  85.  
  86. 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;
  87. if (targetLocation) {
  88. targetLocation.appendChild(uploadButton);
  89. targetLocation.appendChild(goButton);
  90. }
  91. }
  92.  
  93. function processJsonFile() {
  94. const fileInput = document.getElementById('jsonFileUploader');
  95. if (fileInput.files.length > 0) {
  96. const file = fileInput.files[0];
  97. const reader = new FileReader();
  98. reader.onload = (e) => {
  99. try {
  100. const data = JSON.parse(e.target.result);
  101. fillForm(data);
  102. } catch(error) {
  103. console.error('Error processing JSON file:', error);
  104. }
  105. };
  106. reader.readAsText(file);
  107. }
  108. }
  109.  
  110. function fillForm(data) {
  111. const albumTitle = unicodeConvert(String(data.response?.group?.name || ''));
  112. const remasterYear = String(data.response?.torrent?.remasterYear || '');
  113. let artistNames = '';
  114. if (data.response?.group?.musicInfo?.artists && data.response.group.musicInfo.artists.length >= 3) { artistNames = 'Various Artists';}
  115. else {
  116. artistNames = data.response?.group?.musicInfo?.artists?.map(artist => unicodeConvert(artist.name)).join(' & ') || '';
  117. }
  118. const media = unicodeConvert(data.response?.torrent?.media || '');
  119. const format = data.response?.torrent?.format || '';
  120. //encoding
  121. let encoding = unicodeConvert(data.response?.torrent?.encoding || '');
  122. if(encoding === "24bit Lossless") {
  123. encoding = "Hi-Res";
  124. } else if(encoding === "Lossless") {
  125. encoding = "无损";
  126. }
  127. //subHeading
  128. let subHeadingArray = [
  129. unicodeConvert(data.response?.torrent?.remasterRecordLabel),
  130. unicodeConvert(data.response?.torrent?.remasterCatalogueNumber),
  131. unicodeConvert(data.response?.torrent?.remasterTitle)
  132. ];
  133. if (data.response?.torrent?.hasLog && data.response?.torrent?.logScore) {
  134. subHeadingArray.push(`log (${data.response.torrent.logScore}%)`);
  135. }
  136. if (data.response?.torrent?.hasCue) {
  137. subHeadingArray.push("Cue");
  138. }
  139. if (data.response?.torrent?.scene) {
  140. subHeadingArray.push("Scene");
  141. }
  142. if (data.response?.torrent?.lossyWebApproved) {
  143. subHeadingArray.push("Lossy WEB");
  144. }
  145. if (data.response?.torrent?.lossyMasterApproved) {
  146. subHeadingArray.push("Lossy Master");
  147. }
  148. const subHeading = subHeadingArray.filter(info => info).join(' / ');
  149. //description
  150. let descr = String('[img]'+(data.response?.group?.wikiImage || '').replace(/\\\//g, '/')+'[/img]\n');
  151. //add torrent description
  152. const torrent_desc = replaceUnsupportedBBCode(he.decode(unicodeConvert(data.response?.torrent?.description || '').replace(/\\\//g, '/')));
  153. if(torrent_desc !== "") {descr = descr + "\n[quote]" +torrent_desc + "[/quote]\n";}
  154. //torrent group description
  155. const bbBody = replaceUnsupportedBBCode(he.decode(unicodeConvert(data.response?.group?.bbBody || '')));
  156. const wikiBBcode = replaceUnsupportedBBCode(he.decode(unicodeConvert(data.response?.group?.wikiBBcode || '')));
  157. if (bbBody) {descr += bbBody;}
  158. else if (wikiBBcode) {descr += wikiBBcode;}
  159. else { //parse wikiBody for DIC
  160. const bb4dic = wiki2bb(he.decode(unicodeConvert(data.response?.group?.wikiBody || '')));
  161. descr += bb4dic;
  162. }
  163.  
  164.  
  165. document.getElementById('hqname').value = albumTitle;
  166. document.getElementById('issuedate').value = remasterYear;
  167. document.getElementById('artist').value = artistNames;
  168. document.getElementById('specificcat').value = media;
  169. document.getElementById('format').value = format;
  170. document.getElementById('hqtone').value = encoding;
  171. const smallDescr = document.querySelector('input[type="text"][name="small_descr"]');
  172. if(smallDescr) {
  173. smallDescr.value = subHeading;
  174. }
  175. const currentDesc = document.getElementById('descr');
  176. if(currentDesc && currentDesc.value == ""){
  177. document.getElementById('descr').value = descr;
  178. }
  179. }
  180.  
  181. function selectMusicCategory() {
  182. const categorySelect = document.getElementById('browsecat');
  183. if (categorySelect && categorySelect.value !== "406") {
  184. categorySelect.value = "406";
  185. categorySelect.dispatchEvent(new Event('change'));
  186. }
  187. }
  188.  
  189. insertButtons();
  190. })();