115Rename

115改名称(根据现有的文件名<番号>查询并修改文件名),需要翻墙(查询需要)

当前为 2020-02-15 提交的版本,查看 最新版本

// ==UserScript==
// @name                115Rename
// @namespace           http://tampermonkey.net/
// @version             0.2
// @description         115改名称(根据现有的文件名<番号>查询并修改文件名),需要翻墙(查询需要)
// @author              db117
// @include             https://115.com/home/userhome
// @include             https://115.com/?*mode=wangpan*
// @domain              javbus.com
// @grant               GM_notification
// @grant               GM_xmlhttpRequest
// ==/UserScript==

(function () {
    'use strict';

    if (window.location.href === "https://115.com/home/userhome")
        window.location = "https://115.com/?mode=wangpan";
    else {
        // 数据list
        var object_list;
        var object_ifr;
        object_ifr = $("iframe[style='position: absolute; top: 0px;']");
        object_ifr.load(
            function () {
                object_list = object_ifr.contents().find("body").find("div#js_data_list");
                object_list.mouseenter(
                    function () {
                        if ($("div.exph-loader").css("display") === "none" && !(object_list.find("div#isload").length)) {
                            // 添加按钮
                            addButton();
                        }
                    }
                );
            }
        );
    }

    // 添加按钮
    function addButton() {
        let rename_list = `
            <li id="rename_list">
                <a id="rename_a" class="mark" href="javascript:;">改名</a>
            </li>
        `;

        object_ifr.contents().find("body").mouseup(
            function (event) {
                if (event.button == 2) {
                    setTimeout(function () {
                        if ($("li#rename_list").length === 0) {
                            $("div#js_float_content").find("li[val='open_dir']").before(rename_list);
                            $("a#rename_a").click(
                                function () {
                                    rename();
                                }
                            );
                            object_ifr.contents().find("body").unbind("mouseup");
                            console.log("添加按钮")
                        }
                    }, 50);
                }
            }
        )
    }

    // 该名称
    function rename() {
        // 获取所有已选择的文件
        let list = object_list.find("li.selected");
        list.each(function (index, v) {
            let $item = $(v);
            // 原文件名称
            let file_name = $item.attr("title");
            // 文件类型
            let file_type = $item.attr("file_type");

            // 文件id
            let fid;
            // 后缀名
            let suffix;
            if (file_type === "0") {
                // 文件夹
                fid = $item.attr("cate_id");
            } else {
                // 文件
                fid = $item.attr("file_id");
                let lastIndexOf = file_name.lastIndexOf('.');
                if (lastIndexOf !== -1) {
                    suffix = file_name.substr(lastIndexOf, file_name.length);
                }
            }

            if (fid && file_name) {
                let fh = getVideoCode(file_name);
                if (fh) {
                    getVideoName(fid, fh, suffix);
                }
            }
        });
    }

    /**
     * 请求115接口
     * @param id 文件id
     * @param name 要修改的名称
     */
    function send_115(id, name) {
        let file_name = stringStandard(name);
        $.post("https://webapi.115.com/files/edit", {
                fid: id,
                file_name: file_name
            },
            function (data, status) {
                let result = JSON.parse(data);
                if (!result.state) {
                    GM_notification("改名失败,请查看控制台");
                    console.log("请求115接口异常: " + unescape(result.error
                        .replace(/\\(u[0-9a-fA-F]{4})/gm, '%$1')));
                } else {
                    GM_notification("改名成功,请刷新");
                    console.log("修改文件名称,id:" + id, "name:" + file_name);
                }
            }
        );
    }

    /**
     * 115名称不接受(\/:*?\"<>|)
     * @param name
     */
    function stringStandard(name) {
        return name.replace(/\\/g, "")
            .replace(/\//g, " ")
            .replace(/:/g, " ")
            .replace(/\?/g, " ")
            .replace(/"/g, " ")
            .replace(/</g, " ")
            .replace(/>/g, " ")
            .replace(/\|/g, "")
            .replace(/\*/g, " ");
    }

    /**
     * 获取番号
     * @param fid       文件id
     * @param fh        番号
     * @param suffix    后缀
     */
    function getVideoName(fid, fh, suffix) {
        GM_xmlhttpRequest({
            method: "GET",
            url: "https://www.javbus.com/search/" + fh,
            onload: xhr => {
                // 匹配标题
                let match = xhr.responseText.match("<span>(.*?)<br />");
                if (match) {
                    let title = match.pop();
                    if (title) {
                        let newName;
                        if (suffix) {
                            // 文件保存后缀名
                            newName = fh + " " + title + suffix;
                        } else {
                            newName = fh + " " + title;
                        }
                        send_115(fid, newName);
                    }
                } else {
                    getUncensored(fid, fh, suffix);
                }
            }
        })
    }

    /**
     * 获取无码番号
     * @param fid       文件id
     * @param fh        番号
     * @param suffix    后缀
     */
    function getUncensored(fid, fh, suffix) {
        GM_xmlhttpRequest({
            method: "GET",
            url: "https://www.javbus.com/uncensored/search/" + fh,
            synchronous: true,
            onload: xhr => {
                // 匹配标题
                let match = xhr.responseText.match("<span>(.*?)<br />");
                if (match) {
                    let title = match.pop();
                    if (title) {
                        let newName;
                        if (suffix) {
                            // 文件保存后缀名
                            newName = fh + " " + title + suffix;
                        } else {
                            newName = fh + " " + title;
                        }
                        send_115(fid, newName);
                    }
                }
            }
        })
    }

    // 获取番号
    function getVideoCode(title) {
        title = title.replace("SIS001", "").toUpperCase();

        let t = title.match(/T28[\-_]\d{3,4}/);
        // 一本道
        if (!t) {
            t = title.match(/1PONDO[\-_]\d{6}[\-_]\d{2,4}/);
            if (t) {
                t = t.toString().replace("1PONDO_", "")
                    .replace("1PONDO-","");
            }
        }
        if (!t) {
            t = title.match(/HEYZO[\-_]?\d{4}/);
        }
        if (!t) {
            // 加勒比
            t = title.match(/CARIB[\-_]\d{6}[\-_]\d{3}/);
            if (t) {
                t = t.toString().replace("CARIB-", "")
                    .replace("CARIB_","");
            }
        }
        if (!t) {
            // 东京热
            t = title.match(/N[-_]\d{4}/);
        }
        if (!t) {
            // Jukujo-Club | 熟女俱乐部
            t = title.match(/JUKUJO[-_]\d{4}/);
        }
        // 通用
        if (!t) {
            t = title.match(/[A-Z]{2,5}[-_]\d{3,5}/);
        }
        if (!t) {
            t = title.match(/\d{6}[\-_]\d{2,4}/);
        }
        if (!t) {
            t = title.match(/[A-Z]+\d{3,5}/);
        }
        if (t) {
            t = t.toString().replace("_","-");
            console.log("找到番号:" + t);
        }
        return t;
    }
})();