Greasy Fork

moviepilotNameTest

moviepilots名称测试

目前为 2023-08-17 提交的版本。查看 最新版本

// ==UserScript==
// @name         moviepilotNameTest
// @namespace    http://tampermonkey.net/
// @version      1.3.1
// @description  moviepilots名称测试
// @author       yubanmeiqin9048
// @match        https://*/details.php?id=*
// @match        https://*/details_movie.php?id=*
// @match        https://*/details_tv.php?id=*
// @match        https://*/details_animate.php?id=*
// @match        https://bangumi.moe/*
// @match        https://*.acgnx.se/*
// @match        https://*.dmhy.org/*
// @match        https://nyaa.si/*
// @match        https://mikanani.me/*
// @match        https://*.skyey2.com/*
// @grant        GM_log
// @grant        GM_xmlhttpRequest
// @connect      *
// @license MIT
// ==/UserScript==

const moviepilotUrl = 'http://localhost:3000';
const moviepilotUser = 'admin';
const moviepilotPassword = 'password';

function renderTag(string, background_color) {
    return `<span style=\"background-color:${background_color};color:#ffffff;border-radius:0;font-size:12px;margin:0 4px 0 0;padding:1px 2px\">${string}</span>`
}
 
function renderMoviepilotTag(tag) {
    return `<td class="rowhead nowrap" valign="top" align="right">MoviePilot</td><td class="rowfollow" valign="top" align="left">${tag}</td>`;
}

function login () {
    return new Promise(function(resolve, reject){
        GM_xmlhttpRequest({
            method: 'POST',
            responseType: 'json',
            url: moviepilotUrl + '/api/v1/login/access-token',
            data: `username=${moviepilotUser}&password=${moviepilotPassword}`,
            headers: {
                "accept": "application/json",
                "content-type": "application/x-www-form-urlencoded"
            },
            onload: (res) => {
                resolve(res.response.access_token);
            },
            onerror: (err) => {
                reject(err);
            }
        });
    });
}

function recognize (token, title, subtitle) {
    return new Promise(function(resolve, reject){
        GM_xmlhttpRequest({
            url: moviepilotUrl + `/api/v1/media/recognize?title=${title}&subtitle=${subtitle}`,
            method: "GET",
            headers: {
                "user-agent": navigator.userAgent,
                "content-type": "application/json",
                "Authorization": `bearer ${token}`
            },
            responseType: "json",
            onload: (res) => {
                resolve(res.response);
            },
            onerror: (err) => {
                reject(err);
            }
        });
    });
}

function creatRecognizeRow (row, title, subtitle) {
    row.innerHTML = renderMoviepilotTag("识别中");
    login().then(token => {
        recognize(token,title,subtitle).then(data => {
            GM_log(data.status)
            if (data.media_info) {
                let html = '';
                html += data.media_info.type ? renderTag(data.media_info.type, '#2775b6') : '';
                html += data.media_info.category ? renderTag(data.media_info.category, '#2775b6') : '';
                html += data.media_info.title ? renderTag(data.media_info.title, '#c54640') : '';
                html += data.meta_info.season_episode ? renderTag(data.meta_info.season_episode, '#e6702e') : '';
                html += data.meta_info.year ? renderTag(data.meta_info.year, '#e6702e') : '';
                html += data.media_info.tmdb_id ? '<a href="'+data.media_info.detail_link + '" target="_blank">' + renderTag(data.media_info.tmdb_id, '#5bb053') + '</a>': '';
                html += data.meta_info.resource_type ? renderTag(data.meta_info.resource_type, '#677489') : '';
                html += data.meta_info.resource_pix ? renderTag(data.meta_info.resource_pix, '#677489') : '';
                html += data.meta_info.video_encode ? renderTag(data.meta_info.video_encode, '#677489') : '';
                html += data.meta_info.audio_encode ? renderTag(data.meta_info.audio_encode, '#677489') : '';
                html += data.meta_info.resource_team ? renderTag(data.meta_info.resource_team, '#701eeb') : '';
                row.innerHTML = renderMoviepilotTag(html);
            } else {
                row.innerHTML = renderMoviepilotTag(`识别失败`);
            }
        })
    }).catch(error => {
        row.innerHTML = renderMoviepilotTag(`登录${moviepilotUrl}失败`);
    });
}

function creatRecognizeTip(tip, text) {
    tip.showText(`识别中`);
    login().then(token => {
        GM_log(text);
        let subtitle = ''
        recognize(token,text,subtitle).then(data => {
            GM_log(data.status)
            if (data.status === 200) {
                let html = '';
                html += data.media_info.type ? `类型:${data.media_info.type}<br>` : '';
                html += data.media_info.category ? `分类:${data.media_info.category}<br>` : '';
                html += data.media_info.title ? `标题:${data.media_info.title}<br>` : '';
                html += data.meta_info.season_episode ? `季集:${data.media_info.season_episode}<br>` : '';
                html += data.meta_info.year ? `年份:${data.media_info.year}<br>` : '';
                html += data.meta_info.resource_team ? `制作:${data.meta_info.resource_team}<br>` : '';
                html += data.media_info.tmdb_id ? 'tmdb:<a href="' + data.media_info.detail_link + '" target="_blank">' + data.media_info.tmdb_id + '</a>' : 'tmdb:未识别';
                tip.showText(html);
            } else {
                tip.showText(`识别失败`);
            }
        })
    }).catch(error => {
        tip.showText(`登录${moviepilotUrl}失败`);
    });
}

(function () {
    'use strict';
    // 结果面板
    class RecognizeTip {
        constructor() {
            const div = document.createElement('div');
            div.hidden = true;
            div.setAttribute('style',
                `position:absolute!important;
font-size:13px!important;
overflow:auto!important;
background:#fff!important;
font-family:sans-serif,Arial!important;
font-weight:normal!important;
text-align:left!important;
color:#000!important;
padding:0.5em 1em!important;
line-height:1.5em!important;
border-radius:5px!important;
border:1px solid #ccc!important;
box-shadow:4px 4px 8px #888!important;
max-width:350px!important;
max-height:216px!important;
z-index:2147483647!important;`
            );
            document.documentElement.appendChild(div);
            //点击了内容面板,不再创建图标
            div.addEventListener('mouseup', e => e.stopPropagation());
            this._tip = div;
        }
        showText(text) { //显示测试结果
            this._tip.innerHTML = text;
            this._tip.hidden = !1;
        }
        hide() {
            this._tip.innerHTML = '';
            this._tip.hidden = true;
        }
        pop(ev) {
            this._tip.style.top = ev.pageY + 'px';
            //面板最大宽度为350px
            this._tip.style.left = (ev.pageX + 350 <= document.body.clientWidth ?
                ev.pageX : document.body.clientWidth - 350) + 'px';
        }
    }
    const tip = new RecognizeTip();

    class Icon {
        constructor() {
            const icon = document.createElement('span');
            icon.hidden = true;
            icon.innerHTML = `<svg style="margin:4px !important;" width="16" height="16" viewBox="0 0 24 24">
            <path d="M12 2L22 12L12 22L2 12Z" style="fill:none;stroke:#3e84f4;stroke-width:2;"></path></svg>`;
            icon.setAttribute('style',
                `width:24px!important;
		height:24px!important;
		background:#fff!important;
		border-radius:50%!important;
		box-shadow:4px 4px 8px #888!important;
		position:absolute!important;
		z-index:2147483647!important;`
            );
            document.documentElement.appendChild(icon);
            //拦截二个鼠标事件,以防止选中的文本消失
            icon.addEventListener('mousedown', e => e.preventDefault(), true);
            icon.addEventListener('mouseup', ev => ev.preventDefault(), true);
            icon.addEventListener('click', ev => {
                if (ev.ctrlKey) navigator.clipboard.readText()
                    .then(text => {
                        this.queryText(text.trim(), ev);
                    })
                    .catch(err => {
                        console.error('Failed to read contents: ', err);
                    });
                else {
                    const text = window.getSelection().toString().trim().replace(/\s{2,}/g, ' ');
                    this.queryText(text, ev);
                }
            });
            this._icon = icon;
        }
        pop(ev) {
            const icon = this._icon;
            icon.style.top = ev.pageY + 9 + 'px';
            icon.style.left = ev.pageX + -18 + 'px';
            icon.hidden = !1;
            setTimeout(this.hide.bind(this), 2e3);
        }
        hide() {
            this._icon.hidden = true;
        }
        queryText(text, ev) {
            if (text) {
                this._icon.hidden = true;
                tip.pop(ev);
                creatRecognizeTip(tip,text);
            }
        }
    }
    const icon = new Icon();

    document.addEventListener('mouseup', function (e) {
        var text = window.getSelection().toString().trim();
        GM_log(text);
        if (!text) {
            icon.hide();
            tip.hide();
        }
        else icon.pop(e);
    });

    let rows = document.getElementsByClassName('rowhead');
    if (rows.length) {
        let first_row = rows[0];
        let second_row = rows[1]
        let torrent_name = first_row.nextElementSibling.firstElementChild.text;
        let torrent_subtitle = second_row.nextElementSibling.innerText
        // let download_link = first_row.nextElementSibling.firstElementChild.href;
        // GM_log(download_link);
        GM_log(torrent_name);
        GM_log(torrent_subtitle);
        let table = first_row.parentNode.parentNode.parentNode;
        let row = table.insertRow(2);
        if (torrent_name) {
            creatRecognizeRow(row,torrent_name,torrent_subtitle)
        } else {
            row.innerHTML = renderMoviepilotTag("未获取到种子名");
        }
    }
})();