Gazelle Autofiller

Automatically fills a Gazelle form, to be used with torrent files that have a very specific filename structure

  1. // ==UserScript==
  2. // @name Gazelle Autofiller
  3. // @description Automatically fills a Gazelle form, to be used with torrent files that have a very specific filename structure
  4. // @namespace shurelia
  5. // @include http*://passtheheadphones.me/upload.php*
  6. // @include http*://apollo.rip/upload.php*
  7. // @include http*://notwhat.cd/upload.php*
  8. // @include http*://hydra.zone/upload.php*
  9. // @version 0.2.3
  10. // @require https://greasyfork.org/scripts/13016-bencode-encoder-decoder/code/Bencode%20encoderdecoder.js?version=79776
  11. // @require https://greasyfork.org/scripts/13017-rusha/code/Rusha.js?version=79803
  12. // ==/UserScript==
  13.  
  14. if (!Array.prototype.last){
  15. Array.prototype.last = function(){
  16. return this[this.length - 1];
  17. };
  18. };
  19.  
  20. String.prototype.trimChar = function(chr) {
  21. var str = this;
  22.  
  23. while(str.charAt(0) == chr) {
  24. str = str.substring(1);
  25. }
  26.  
  27. while(str.charAt(str.length-1) == chr) {
  28. str = str.substring(0, str.length-1);
  29. }
  30.  
  31. return str;
  32. };
  33.  
  34. var Torrent = function(binaryString)
  35. {
  36. var self = this,
  37. uniqueStringChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
  38. mimeType = "application/x-bittorrent",
  39. data = bencode.decode(binaryString),
  40. originalAnnounceUrl = "announce" in data ? data.announce : "",
  41.  
  42. calculateHash = function() {
  43. return new Rusha().digestFromBuffer(bencode.encode(data.info)).toUpperCase();
  44. },
  45.  
  46. originalHash = calculateHash(),
  47. hash = originalHash;
  48.  
  49. self.getBinary = function() {
  50. return bencode.encode(data);
  51. };
  52.  
  53. self.getName = function() {
  54. return decodeURIComponent(escape(data.info.name));
  55. };
  56.  
  57. self.setAnnounceUrl = function(value) {
  58. data.announce = value;
  59. return this;
  60. };
  61.  
  62. self.getAnnounceUrl = function() {
  63. return data.announce;
  64. };
  65.  
  66. self.getOriginalAnnounceUrl = function() {
  67. return originalAnnounceUrl;
  68. };
  69.  
  70. self.getHash = function() {
  71. return hash;
  72. };
  73.  
  74. self.getOriginalHash = function() {
  75. return originalHash;
  76. };
  77.  
  78. var randomString = function (length) {
  79. var text = "";
  80.  
  81. for (var i = 0; i < length; i++) {
  82. text += uniqueStringChars.charAt(Math.floor(Math.random() * uniqueStringChars.length));
  83. }
  84.  
  85. return text;
  86. };
  87.  
  88. self.changeHash = function() {
  89. data.info.private = 1;
  90. data.info.unique = randomString(30);
  91. hash = calculateHash();
  92. return hash;
  93. };
  94.  
  95. self.getDownloadLink = function(text) {
  96. var a = document.createElement("a");
  97. a.setAttribute("href", "javascript:void(0);");
  98. a.textContent = text ? text : hash;
  99. a.style.cursor = "pointer";
  100. a.onclick = self.downloadTorrent;
  101.  
  102. return a;
  103. };
  104.  
  105. self.downloadTorrent = function(){
  106. var uri = "data:application/x-bittorrent;base64," + btoa(bencode.encode(data));
  107.  
  108. var link = document.createElement("a");
  109. link.href = uri;
  110. link.style = "visibility:hidden";
  111. link.download = self.getName() + ".torrent";
  112.  
  113. document.body.appendChild(link);
  114. link.click();
  115. document.body.removeChild(link);
  116.  
  117. return false;
  118. };
  119.  
  120. self.countFiles = function(){
  121. return data.info.files ? data.info.files.length : 0;
  122. };
  123.  
  124. self.getFiles = function() {
  125. return data.info.files ? data.info.files : [];
  126. };
  127.  
  128. self.getTrackerId = function() {
  129. return originalAnnounceUrl.split('://')[1].split('/')[0].split(':')[0].replace(/^tracker\./, "").replace(/^please\./, "");
  130. };
  131.  
  132. self.getTotalSize = function() {
  133. var files = data.info.files;
  134. var size = 0;
  135.  
  136. if (files && files instanceof Array) {
  137. for (var i = 0, file; file = files[i]; i++) {
  138. if (file.length) {
  139. size += file.length;
  140. }
  141. }
  142. }
  143.  
  144. return size;
  145. };
  146.  
  147. self.getBlob = function() {
  148. var i, l, array,
  149. binary = self.getBinary();
  150.  
  151. l = binary.length;
  152. array = new Uint8Array(l);
  153.  
  154. for (i = 0; i < l; i++){
  155. array[i] = binary.charCodeAt(i);
  156. }
  157.  
  158. return new Blob([array], {type: mimeType});
  159. }
  160. };
  161.  
  162.  
  163. (function()
  164. {
  165. var self = this;
  166. var fileInput = document.getElementById("file");
  167.  
  168. if (!fileInput) {
  169. console.log("File input not found.");
  170. return;
  171. }
  172.  
  173. self.input = fileInput;
  174. self.form = self.input.closest("form");
  175. self.host = document.location.href.split('://')[1].split('/')[0].split(':')[0].replace(/^(www|tls|ssl)\./, "");
  176. self.formats = {
  177. flac: 'FLAC',
  178. mp3: 'MP3',
  179. ogg: 'Ogg Vorbis',
  180. m4a: 'AAC'
  181. };
  182.  
  183. fileInput.addEventListener("change", function(ev) {
  184. if (ev.target.files[0].name.toLowerCase().indexOf(".torrent") === -1) {
  185. return;
  186. }
  187. var reader = new FileReader();
  188. reader.onload = function(e) {
  189. self.addTorrent(e.target.result, ev.target.files[0].name);
  190. };
  191. reader.readAsBinaryString(ev.target.files[0]);
  192. });
  193.  
  194. self.addTorrent = function(binaryString, filename)
  195. {
  196. try {
  197. self.analyze(new Torrent(binaryString), filename);
  198. } catch (e) {
  199. console.log(e);
  200. }
  201. };
  202.  
  203. self.analyze = function(torrent, filename) {
  204. console.log(filename)
  205. var i, l, ext, format, media, bitrate, year, artist, album,
  206. torrentName = torrent.getName(),
  207. files = torrent.getFiles();
  208.  
  209. var seg = filename.replace(/\).*?\.torrent/gi, "");
  210. seg = filename.split(/\s\(|\s-\s/g);
  211. artist = seg[0];
  212. bitrate = "Lossless";
  213. format = seg[seg.length-2];
  214. media = seg[seg.length-3];
  215. year = seg[seg.length-4];
  216. console.log(year);
  217. album = ""
  218. for (var i = 1; i<seg.length-4; i++) {
  219. if (i > 1) { album += " - "; }
  220. album += seg[i];
  221. }
  222. if (format === 'FLAC') {
  223. document.getElementById('upload_logs').className = "";
  224. }
  225. self.generateReleaseDesc(torrent);
  226. if (artist) {
  227. self.setArtist(artist);
  228. }
  229.  
  230. if (album) {
  231. self.setAlbum(album);
  232. }
  233.  
  234. if (format) {
  235. self.setFormat(format);
  236. }
  237.  
  238. if (media) {
  239. self.setMedia(media);
  240. }
  241.  
  242. if (bitrate) {
  243. self.setBitrate(bitrate);
  244. }
  245. if (year) {
  246. self.setYear(year);
  247. }
  248. };
  249.  
  250. self.setArtist = function (artist) {
  251. document.getElementById('artist').value = artist;
  252. };
  253.  
  254. self.setAlbum = function (album) {
  255. document.getElementById('title').value = album;
  256. };
  257.  
  258. self.setYear = function (year) {
  259. document.getElementById('year').value = year;
  260. };
  261.  
  262. self.setFormat = function (format) {
  263. document.getElementById('format').value = format;
  264. };
  265.  
  266. self.setMedia = function (media) {
  267. document.getElementById('media').value = media;
  268. };
  269.  
  270. self.setBitrate = function (bitrate) {
  271. document.getElementById('bitrate').value = bitrate;
  272. };
  273.  
  274. self.generateReleaseDesc = function (torrent) {
  275. var tracks = [], files = torrent.getFiles();
  276.  
  277. for (var i = 0, l = files.length; i < l; i++) {
  278. ext = files[i].path[0].split('.').last().toLowerCase();
  279. if (!self.formats.hasOwnProperty(ext)) continue;
  280. tracks.push('[#]' + files[i].path[0].replace(/^[\d\(\)\[\]]+\.?\s*\-?/, '').replace(/\.([a-zA-Z0-9]+)$/, '').trim());
  281. }
  282.  
  283. document.getElementById('album_desc').value = "[size=2][b]Tracklist:[/b][/size]\n" + tracks.join("\n");
  284. };
  285. })();