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