TJUPT Music JSON Uploady

import json from red/ops/dic

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

  1. // ==UserScript==
  2. // @name TJUPT Music JSON Uploady
  3. // @namespace http://tjupt.org/
  4. // @version 0.0.12
  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. function replaceUnsupportedBBCode(BB) {
  17. BB = BB.replace(/\[artist\](.*?)\[\/artist\]/g, '$1');
  18. BB = BB.replace(/\[plain\](.*?)\[\/plain\]/g, '$1');
  19. BB = BB.replace(/\[align=center\](.*?)\[\/align\]/g, '[center]$1[/center]');
  20. BB = BB.replace(/\[align(.*?)\]/g, '').replace(/\[\/align\]/g, '');
  21. BB = BB.replace(/\[pad(.*?)\]/g, '').replace(/\[\/pad\]/g, '');
  22.  
  23. return BB;
  24. }
  25.  
  26. function unicodeConvert(string) {
  27. string = string.replace(/\\u([\d\w]{4})/gi, (match, grp) => String.fromCharCode(parseInt(grp, 16)));
  28. string = string.replace(/&#(\d+);/g, (match, dec) => String.fromCharCode(dec));
  29. return string;
  30. }
  31.  
  32. function insertButtons() {
  33. const uploadButton = document.createElement('input');
  34. uploadButton.type = 'file';
  35. uploadButton.id = 'jsonFileUploader';
  36. uploadButton.accept = 'application/json';
  37. uploadButton.addEventListener('change', () => selectMusicCategory());
  38.  
  39. const goButton = document.createElement('button');
  40. goButton.textContent = 'Go';
  41. goButton.type = 'button';
  42. goButton.addEventListener('click', processJsonFile);
  43.  
  44. 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;
  45. if (targetLocation) {
  46. targetLocation.appendChild(uploadButton);
  47. targetLocation.appendChild(goButton);
  48. }
  49. }
  50.  
  51. function processJsonFile() {
  52. const fileInput = document.getElementById('jsonFileUploader');
  53. if (fileInput.files.length > 0) {
  54. const file = fileInput.files[0];
  55. const reader = new FileReader();
  56. reader.onload = (e) => {
  57. try {
  58. const data = JSON.parse(e.target.result);
  59. fillForm(data);
  60. } catch(error) {
  61. console.error('Error processing JSON file:', error);
  62. }
  63. };
  64. reader.readAsText(file);
  65. }
  66. }
  67.  
  68. function fillForm(data) {
  69. const albumTitle = unicodeConvert(String(data.response?.group?.name || ''));
  70. const remasterYear = String(data.response?.torrent?.remasterYear || '');
  71. const artistNames = data.response?.group?.musicInfo?.artists?.map(artist => unicodeConvert(artist.name)).join(' & ') || '';
  72. const media = unicodeConvert(data.response?.torrent?.media || '');
  73. const format = data.response?.torrent?.format || '';
  74. //encoding
  75. let encoding = unicodeConvert(data.response?.torrent?.encoding || '');
  76. if(encoding === "24bit Lossless") {
  77. encoding = "Hi-Res";
  78. } else if(encoding === "Lossless") {
  79. encoding = "无损";
  80. }
  81. //subHeading
  82. let subHeadingArray = [
  83. unicodeConvert(data.response?.torrent?.remasterRecordLabel),
  84. unicodeConvert(data.response?.torrent?.remasterCatalogueNumber),
  85. unicodeConvert(data.response?.torrent?.remasterTitle)
  86. ];
  87. if (data.response?.torrent?.hasLog && data.response?.torrent?.logScore) {
  88. subHeadingArray.push(`log (${data.response.torrent.logScore}%)`);
  89. }
  90. if (data.response?.torrent?.hasCue) {
  91. subHeadingArray.push("Cue");
  92. }
  93. if (data.response?.torrent?.scene) {
  94. subHeadingArray.push("Scene");
  95. }
  96. const subHeading = subHeadingArray.filter(info => info).join(' / ');
  97. //description
  98. let descr = String('[img]'+(data.response?.group?.wikiImage || '').replace(/\\\//g, '/')+'[/img]\n');
  99. //TODO: parse wikiBody. bbBody is RED-only; wikiBBcode is OPS-only; currently not support DIC
  100. const bbBody = replaceUnsupportedBBCode(he.decode(unicodeConvert(data.response?.group?.bbBody || '')));
  101. const wikiBBcode = replaceUnsupportedBBCode(he.decode(unicodeConvert(data.response?.group?.wikiBBcode || '')));
  102. if (bbBody) {descr += bbBody;}
  103. else if (wikiBBcode) {descr += wikiBBcode;}
  104. //else descr += wikiBody;
  105.  
  106. document.getElementById('hqname').value = albumTitle;
  107. document.getElementById('issuedate').value = remasterYear;
  108. document.getElementById('artist').value = artistNames;
  109. document.getElementById('specificcat').value = media;
  110. document.getElementById('format').value = format;
  111. document.getElementById('hqtone').value = encoding;
  112. const smallDescr = document.querySelector('input[type="text"][name="small_descr"]');
  113. if(smallDescr) {
  114. smallDescr.value = subHeading;
  115. }
  116. const currentDesc = document.getElementById('descr');
  117. if(currentDesc && currentDesc.value == ""){
  118. document.getElementById('descr').value = descr;
  119. }
  120. }
  121.  
  122. function selectMusicCategory() {
  123. const categorySelect = document.getElementById('browsecat');
  124. if (categorySelect && categorySelect.value !== "406") {
  125. categorySelect.value = "406";
  126. categorySelect.dispatchEvent(new Event('change'));
  127. }
  128. }
  129.  
  130. insertButtons();
  131. })();