Skip to content

Instantly share code, notes, and snippets.

@ewasion
Last active April 18, 2018 14:43
Show Gist options
  • Save ewasion/9328e9b0feea1413bbdd5d4744858c27 to your computer and use it in GitHub Desktop.
Save ewasion/9328e9b0feea1413bbdd5d4744858c27 to your computer and use it in GitHub Desktop.
AB AutoReport
// ==UserScript==
// @name AB AutoReport
// @namespace https://github.com/ewasion
// @version 0.1.2
// @description Tool to find and report trumped torrents.
// @author Eva
// @homepage https://gist.github.com/ewasion/9328e9b0feea1413bbdd5d4744858c27
// @icon https://animebytes.tv/favicon.ico
// @updateURL https://gist.github.com/ewasion/9328e9b0feea1413bbdd5d4744858c27/raw/ab-autoreport.user.js
// @downloadURL https://gist.github.com/ewasion/9328e9b0feea1413bbdd5d4744858c27/raw/ab-autoreport.user.js
// @grant none
// @match https://animebytes.tv/torrents*.php*
// @exclude https://animebytes.tv/torrents*.php?action=*
// @license GPL-3.0
// @run-at document-end
// ==/UserScript==
"use strict";
if(document.getElementById("content") != null) {
const search = document.getElementById("browse_nav_right"),
group = document.querySelector('.linkbox > a[href^="/upload.php"]'),
reportLink = document.createElement("a");
reportLink.id = "userscript-autoreport";
reportLink.href = "#userscript-autoreport";
if(group != null) {
reportLink.addEventListener('click', autoReport, false);
reportLink.innerText = "[AutoReport]";
group.prepend(" ");
group.parentNode.insertBefore(reportLink, group);
} else if(search != null) {
reportLink.addEventListener('click', gatherGroups, false);
reportLink.innerText = "AutoReport";
search.append(" | ");
search.appendChild(reportLink);
}
}
if(localStorage.getItem("userscript-autoreport-reported") == null) localStorage.setItem("userscript-autoreport-reported", "");
Array.prototype.forEach.call(torrents, function(element, index) {
element.classList.add("userscript-autoreport");
});
function autoReport(group, prompt = null) {
if(group.target) {
group = document.getElementsByClassName("torrent_table")[0];
var title = document.querySelector(".thin > h2").innerText;
} else {
var title = group.querySelector(".group_title > strong").innerText;
}
const torrents = group.querySelectorAll("a[href*='torrents'][href*='.php?id='][href*='&torrentid=']:not([href*='#']):not([title])"),
media = [];
Array.prototype.forEach.call(torrents, function(torrent) {
media.push(torrent.getAttribute("data-media"));
});
if(prompt == null) prompt = confirm("Prompt for reports (OK) or just highlight torrents (Cancel)");
Array.prototype.forEach.call([...new Set(media)].filter(n => n), function(currentMedia) {
const raws = group.querySelectorAll('a[data-media="' + currentMedia + '"][data-subbing="RAW"]:not([data-region]):not([data-exclusive])'),
softsubs = group.querySelectorAll('a[data-media="' + currentMedia + '"][data-subbing="Softsubs"]:not([data-region]):not([data-exclusive])');
if(raws.length > 0 && softsubs.length > 0) {
console.log(title + " - " + raws.length + " " + currentMedia + " RAWs can be reported.");
Array.prototype.forEach.call(raws, function(raw) {
raw.parentNode.style = "background: #c23b22;";
if(prompt && confirm(title + "\n\n" + raw.innerText + "\ncan be replaced by\n" + softsubs[0].innerText + "\n\nReason: Softsubs trump RAWs of the same source.\n\nReport?")) {
raw.parentNode.style = "background: #760000;";
sendReport(raw.href.replace(/https:\/\/animebytes\.tv\/torrents2?\.php\?id=\d+&torrentid=/, ""), "Softsubs trump RAWs of the same source. " + softsubs[0].href);
}
});
}
});
}
function gatherGroups() {
const groups = document.getElementsByClassName("group_cont");
const prompt = confirm("Prompt for reports (OK) or just highlight torrents (Cancel)");
Array.prototype.forEach.call(groups, function(group) {
autoReport(group, prompt);
});
}
function sendReport(id, reason) {
if(localStorage.getItem("userscript-autoreport-reported").split(",").includes(id)) {
if(!confirm("You already reported torrent #" + id + ", are you sure you want to continue?")) return;
}
const url = "https://animebytes.tv/reports.php?action=report&id=" + id;
var reportPage = new XMLHttpRequest();
reportPage.open("GET", url, true);
reportPage.onreadystatechange = function() {
if(reportPage.readyState == 4 && reportPage.status == 200) {
const token = reportPage.responseText.match(/<input type="hidden" name="auth" value="(.+)"/)[1];
var report = new XMLHttpRequest();
var params = "action=takereport&auth=" + token + "&id=" + id + "&type=Torrent&reason=" + encodeURIComponent(reason).replace(/%20/g, "+");
report.open("POST", url, true);
report.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
report.onreadystatechange = function() {
if(report.readyState == 4 && report.status == 200) {
if(report.responseText.indexOf("<h2>Error</h2>") !== -1) {
alert("Couldn't report torrent #" + id);
} else {
localStorage.setItem("userscript-autoreport-reported", (localStorage.getItem("userscript-autoreport-reported") + "," + id).replace(/^,/, ""));
}
}
}
report.send(params);
}
}
reportPage.send();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment