Greasy Fork

来自缓存

Greasy Fork is available in English.

qB-WebUI 标记tracker异常

在 qBittorrent WebUI 中添加按钮,用于标记tracker状态出错的种子

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name            qB-WebUI 标记tracker异常
// @name:en         qB-WebUI tag trackerERR
// @namespace       localhost
// @version         0.2.1
// @author          ColderCoder
// @description     在 qBittorrent WebUI 中添加按钮,用于标记tracker状态出错的种子
// @description:en  add a button in qBittorrent WebUI to tag torrents with tracker error
// @license         MIT
// @run-at          document-end
// @match           http://127.0.0.1:8080/
// @require         https://lf6-cdn-tos.bytecdntp.com/cdn/jquery/3.6.0/jquery.min.js
// ==/UserScript==

//require qB API v2.3.0 +

const host = window.location.href;
const baseURL = host + 'api/v2/torrents/';

async function getFetch(route) {
    try {
        const response = await fetch(baseURL + route);
        if (!response.ok) {
            throw new Error('Error fetching info!');
        }
        const data = await response.json();
        return data;
    } catch (error) {
        console.error(error);
        return null;
    }
}

async function processTorrents() {
    try {
        //clear 'trackerErr' & 'Unregistered' tags first
        const url = `${baseURL}deleteTags`;
        const data = new URLSearchParams();
        data.append('tags', 'trackerErr,Unregistered');
        fetch(url, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            body: data
        })
            .then(response => {
            console.log(response);
        })
            .catch(error => {
            console.error('Error:', error);
        });

        const torrentList = await getFetch('info');
        let count = 0;
        const keywords = ['registered', 'invalid', 'deleted', 'banned', 'not found', 'exist', '删除']; // Keywords to search for

        for (const torrent of torrentList) {
            const trackers = await getFetch(`trackers?hash=${torrent.hash}`);
            let hasWorkingTracker = false;
            let hasUnregisteredTracker = false;
            let needUpdateTags = false;
            let torrentTags = [];

            for (let i = 0; i < trackers.length; i++) {
                const tracker = trackers[i];
                if (tracker.status === 4) { // tracker is in error state, check if unregistered
                    for (const msg of keywords) {
                        if (tracker.msg.toLowerCase().includes(msg)) {
                            hasUnregisteredTracker = true;
                            count++;
                            console.log(`${count}. ${torrent.name}: ${tracker.msg}`);
                            break;
                        }
                    }
                } else if (tracker.status !== 0) { // at least one tracker is not in error state nor disabled
                    hasWorkingTracker = true;
                }
            }

            if (hasUnregisteredTracker && !hasWorkingTracker) {
                torrentTags = ['Unregistered'];
                needUpdateTags = true;
            } else if (!hasWorkingTracker) {
                torrentTags = ['trackerErr'];
                needUpdateTags = true;
            }

            if (needUpdateTags) {
                const tags = torrentTags.join(",");

                //const response = await fetch(`${baseURL}addTags?hashes=${torrent.hash}&tags=${tags}`); //GET method. only for qb version under v4.5.0
                const url = `${baseURL}addTags`;
                const data = new URLSearchParams();
                data.append('hashes', torrent.hash);
                data.append('tags', tags);
                fetch(url, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                    body: data
                })
                    .then(response => {
                    console.log(response);
                })
                    .catch(error => {
                    console.error('Error:', error);
                });
            }
        }
        console.log('Done.');
    } catch (error) {
        console.error('Error:', error.message);
    }
}


jQuery("#desktopNavbar > ul").append(
    "<li><a class='js-modal'><b> 标记tracker异常 </b></a></li>",
);

jQuery(".js-modal").click(async function () {
    await processTorrents();
});