From 8f0a60b7feb7309d6b7e4b0de77e9bcfcb8427b0 Mon Sep 17 00:00:00 2001 From: IITII Date: Wed, 27 Mar 2024 16:05:05 +0800 Subject: [PATCH] =?UTF-8?q?fix(mt):=20=E6=89=B9=E9=87=8F=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?,=20=E8=AF=A6=E6=83=85=E9=A1=B5=E4=B8=8B=E8=BD=BD=20#1762?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 批量下载解析进度提醒 --- resource/i18n/en.json | 3 +- resource/i18n/zh-CN.json | 1 + resource/sites/xp.m-team.io/common.js | 147 +++++++++++++---------- resource/sites/xp.m-team.io/config.json | 5 - resource/sites/xp.m-team.io/details.js | 28 +---- resource/sites/xp.m-team.io/torrents.js | 153 +++++++++++++++++++----- 6 files changed, 211 insertions(+), 126 deletions(-) diff --git a/resource/i18n/en.json b/resource/i18n/en.json index ad64de96c..68ed7dfeb 100644 --- a/resource/i18n/en.json +++ b/resource/i18n/en.json @@ -984,6 +984,7 @@ "exceedSizeCanceled": "Oversized has been cancelled", "downloadURLsFinished": "{count} links have been sent.", "downloadURLsTip": "Sending: {text}", + "resolveURLsTip": "Resolving torrent id:{id}, progress:{current}/{total}, approximately {min} minutes left", "search": { "needLogin": "[{siteName}] needs to log in and search again", "noTorrents": "[{siteName}] did not find the relevant torrent", @@ -1106,4 +1107,4 @@ } } } -} \ No newline at end of file +} diff --git a/resource/i18n/zh-CN.json b/resource/i18n/zh-CN.json index 2f1cbe3a9..4137aa993 100644 --- a/resource/i18n/zh-CN.json +++ b/resource/i18n/zh-CN.json @@ -979,6 +979,7 @@ "exceedSizeCanceled": "容量超限,已取消", "downloadURLsFinished": "{count}条链接已发送完成。", "downloadURLsTip": "正在发送:{text}", + "resolveURLsTip": "正在解析种子 id:{id}, 进度:{current}/{total}, 大约还需 {min} 分钟", "search": { "needLogin": "[{siteName}]需要登录后再搜索", "noTorrents": "[{siteName}]没有搜索到相关的种子", diff --git a/resource/sites/xp.m-team.io/common.js b/resource/sites/xp.m-team.io/common.js index 085585934..94f55e335 100644 --- a/resource/sites/xp.m-team.io/common.js +++ b/resource/sites/xp.m-team.io/common.js @@ -146,21 +146,20 @@ error(this.t("needPasskey")); return; } - let urls = this.getDownloadURLs(); - - if (!urls.length || typeof urls == "string") { - error(urls); - return; - } - - PTService.call(PTService.action.copyTextToClipboard, urls.join("\n")) + this.getDownloadURLs().then(urls => { + if (!urls.length || typeof urls == 'string') { + error(urls); + throw new Error('ignore') + } + return PTService.call(PTService.action.copyTextToClipboard, urls.join('\n')) + }) .then(result => { - console.log("命令执行完成", result); + console.log('命令执行完成', result); success(); }) .catch(() => { error(); - }); + }) }, onDrop: (data, event, success, error) => { if (checkPasskey && !PTService.site.passkey) { @@ -214,26 +213,19 @@ error(this.t("needPasskey")); return; } - let urls = this.getDownloadURLs(); - - if (!urls.length || typeof urls == "string") { - error(urls); - return; - } - - let downloads = []; - urls.forEach(url => { - downloads.push({ - url, - method: PTService.site.downloadMethod - }); - }); - - console.log(downloads); - - PTService.call(PTService.action.addBrowserDownloads, downloads) + this.getDownloadURLs().then(urls => { + if (!urls.length || typeof urls == 'string') { + error(urls); + throw new Error('ignore') + } + return urls.map(url => ({url, method: PTService.site.downloadMethod})) + }) + .then(downloads => { + console.log(downloads) + return PTService.call(PTService.action.addBrowserDownloads, downloads) + }) .then(result => { - console.log("命令执行完成", result); + console.log('命令执行完成', result); success(); }) .catch(e => { @@ -989,35 +981,27 @@ return; } - let urls = this.getDownloadURLs(); - if (!urls.length || typeof urls == "string") { - error(urls); - return; - } - - // 是否启用后台下载任务 - if (PTService.options.enableBackgroundDownload) { - this.downloadURLsInBackground( - urls, - msg => { - success({ - msg - }); - }, - downloadOptions - ); - } else { - this.downloadURLs( - urls, - urls.length, - msg => { - success({ - msg - }); - }, - downloadOptions - ); - } + this.getDownloadURLs().then(urls => { + if (!urls.length || typeof urls == 'string') { + error(urls); + throw new Error('ignore') + } + // 是否启用后台下载任务 + if (PTService.options.enableBackgroundDownload) { + this.downloadURLsInBackground( + urls, msg => { + success({msg}); + }, + downloadOptions + ); + } else { + this.downloadURLs(urls, urls.length, + msg => { + success({msg}); + }, downloadOptions + ); + } + }).catch(console.error) } downloadURLsInBackground(urls, callback, downloadOptions) { @@ -1086,8 +1070,7 @@ "/" + count + ")" - }), - 0 + }) ); if (!downloadOptions) { @@ -1127,13 +1110,12 @@ } } - showStatusMessage(msg) { + showStatusMessage(msg, width = 600) { if (!this.statusBar) { this.statusBar = PTService.showNotice({ text: msg, type: "info", - width: 600, - progressBar: false + width, progressBar: false }); } else { this.statusBar.find(".noticejs-content").html(msg); @@ -1274,6 +1256,45 @@ }); } } + + resolveDownloadURLById(id, showNotice = true) { + let res = $.ajax('/api/torrent/genDlToken', { + method: 'POST', + data: {id}, + cache: true, + headers: { + "x-api-key": PTService.site.authToken + }, + success: function (data) { + if (data.code === '0') { + console.log(`种子 ${id} 下载链接获取成功`, data) + // return data.data + } else { + let msg = `种子 ${id} 下载链接获取失败, code != 0` + console.log(msg, data) + if (showNotice) { + PTService.showNotice({msg}) + } + // return null + } + }, + error: function (data) { + let msg = `种子 ${id} 下载链接获取失败` + console.log(msg, data) + if (showNotice) { + PTService.showNotice({msg}) + } + }, + async: false + }) + return res.responseJSON.data || '' + } + + // @ts-ignore + // eslint-disable-next-line + async sleep(ms) { + return new Promise(resolve => setTimeout(() => resolve(), ms)) + } } window.NexusPHPCommon = Common; diff --git a/resource/sites/xp.m-team.io/config.json b/resource/sites/xp.m-team.io/config.json index cdee9c101..8b10da2f4 100644 --- a/resource/sites/xp.m-team.io/config.json +++ b/resource/sites/xp.m-team.io/config.json @@ -81,11 +81,6 @@ "name": "种子列表", "pages": ["/browse"], "scripts": ["common.js", "torrents.js"] - }, { - "name": "种子列表封面模式", - "pages": ["/torrents.php", "/movie.php", "/music.php", "/adult.php"], - "scripts": ["/libs/album/album.js", "torrents.js"], - "styles": ["/libs/album/style.css"] } ], "searchEntryConfig": { diff --git a/resource/sites/xp.m-team.io/details.js b/resource/sites/xp.m-team.io/details.js index 9202a3f7d..2c9f73c2d 100644 --- a/resource/sites/xp.m-team.io/details.js +++ b/resource/sites/xp.m-team.io/details.js @@ -21,27 +21,7 @@ _getDownloadUrlByPossibleHrefs() { let id = window.location.pathname.split('/').pop() - return $.ajax('/api/torrent/genDlToken', { - method: 'POST', - data: {id}, - headers: { - "x-api-key": PTService.site.authToken - }, - success: function (data) { - if (data.code === '0') { - console.log(`种子 ${id} 下载链接获取成功`, data) - // return data.data - } else { - console.log(`种子 ${id} 下载链接获取失败, code != 0`, data) - // return null - } - }, - error: function (data) { - console.log(`种子 ${id} 下载链接获取失败`, data) - // return null - }, - async: false - }) + return this.resolveDownloadURLById(id) } @@ -51,11 +31,7 @@ getDownloadURL() { let url = PTService.getFieldValue('downloadURL') if (!url) { - let res = this._getDownloadUrlByPossibleHrefs() - if (res.status === 200 && res.responseJSON.code === '0') { - url = res.responseJSON.data - } - return url ? url : '' + return this._getDownloadUrlByPossibleHrefs() } return this.getFullURL(url); diff --git a/resource/sites/xp.m-team.io/torrents.js b/resource/sites/xp.m-team.io/torrents.js index 61124e7f2..eef471da4 100644 --- a/resource/sites/xp.m-team.io/torrents.js +++ b/resource/sites/xp.m-team.io/torrents.js @@ -1,35 +1,126 @@ -(function($, window) { - // 添加封面模式 - PTService.addButton({ - title: PTService.i18n.t("buttons.coverTip"), //"以封面的方式进行查看", - icon: "photo", - label: PTService.i18n.t("buttons.cover"), //"封面模式", - click: (success, error) => { - // 获取目标表格 - let tables = $("table.torrentname"); - let images = []; - tables.each((index, item) => { - let img = $("img[onmouseover]", item); - let url = img.attr("src"); - let href = img.parent().attr("href"); - let title = $("td.embedded", item).text(); - images.push({ - url: url, - key: href, - title: title, //img.parent().attr("title"), - link: img.parent().attr("href") +(function ($) { + console.log('this is torrent.js'); + + class App extends window.NexusPHPCommon { + init() { + this.initButtons(); + this.initFreeSpaceButton(); + // 设置当前页面 + PTService.pageApp = this; + } + + /** + * 初始化按钮列表 + */ + initButtons() { + this.initListButtons(); + } + + // eslint-disable-next-line + async resolveDownloadURLs() { + let ids = $('tr').map(function () { + return $(this).data('row-key') + }).toArray().filter(_ => !!_) + ids = [...new Set(ids)] + console.log('ids', ids) + let urls = [] + return new Promise(async (resolve, reject) => { + for (let i = 0; i < ids.length; i++) { + const id = ids[i], timeout = 4000 + let min = Math.ceil(timeout * (ids.length - i) / 1000 / 60) + let msg = this.t('resolveURLsTip', {id, current: i + 1, total: ids.length, min}) + this.showStatusMessage(msg, 480) + let url = this.resolveDownloadURLById(id) + if (url) { + urls.push(url) + } else { + // 限流 + console.error(`can't get download url by id: ${id}`) + break + } + // 强制等待, 减缓站点压力. 觉得太慢自行修改站点每页种子数量 + await this.sleep(timeout) + } + $(this.statusBar).remove() + console.log(`已解析 ${urls.length} 个下载链接`, urls) + resolve(urls) + }) + } + + /** + * 获取下载链接 + */ + getDownloadURLs() { + let urlParser = PTService.filters.parseURL(location.href); + let site = PTService.getSiteFromHost(urlParser.host); + + let urls = PTService.getFieldValue('downloadURLs'); + if (!urls) { + return this.resolveDownloadURLs().then(links => { + if (links.length === 0) { + // "获取下载链接失败,未能正确定位到链接"; + return this.t('getDownloadURLsFailed'); + } + return links + }) + .catch(e => { + console.error(e) + return null + }) + } + + return Promise.resolve(urls) + } + + /** + * 确认大小是否超限 + */ + confirmWhenExceedSize() { + return this.confirmSize( + $('table:contains(\'類別\'), table:contains(\'Type\')').find( + 'td:contains(\'MB\'),td:contains(\'GB\'),td:contains(\'TB\'),td:contains(\'MiB\'),td:contains(\'GiB\'),td:contains(\'TiB\')' + ) + ); + } + + /** + * 获取有效的拖放地址 + * @param {*} url + */ + getDroperURL(url) { + let siteURL = PTService.site.url; + if (siteURL.substr(-1) != '/') { + siteURL += '/'; + } + + if (!url.getQueryString) { + PTService.showNotice({ + msg: + '系统依赖函数(getQueryString)未正确加载,请尝试刷新页面或重新启用插件。' }); - }); + return null; + } - // 创建预览 - new album({ - images: images, - onClose: () => { - PTService.buttonBar.show(); + if (url.indexOf('download.php') === -1) { + let id = url.getQueryString('id'); + if (id) { + // 如果站点没有配置禁用https,则默认添加https链接 + url = + siteURL + + 'download.php?id=' + + id + + (PTService.site.passkey + ? '&passkey=' + PTService.site.passkey + : '') + + (PTService.site.disableHttps ? '' : '&https=1'); + } else { + url = ''; } - }); - success(); - PTService.buttonBar.hide(); + } + + return url; } - }); -})(jQuery, window); + } + + new App().init(); +})(jQuery);