From 5c441806617a1a6dfea7479b69348fc846603c58 Mon Sep 17 00:00:00 2001 From: mediabot-pt <295750538+mediabot-pt@users.noreply.github.com> Date: Wed, 1 Jul 2026 11:11:25 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20ScriptUtils=20=E5=8A=A0=20formatSize/to?= =?UTF-8?q?Long/toInt=EF=BC=8C=E8=84=9A=E6=9C=AC=E4=B8=AD=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E7=94=A8=20utils.xxx()=20=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/mteam.groovy | 17 ++++------------- .../par/core/adapter/GroovyScriptEngine.java | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/docs/mteam.groovy b/docs/mteam.groovy index 9311da4..2dddc33 100644 --- a/docs/mteam.groovy +++ b/docs/mteam.groovy @@ -37,11 +37,11 @@ private Map buildProfile(d) { fields: [ id: d.id?.toString(), username: d.username?.toString(), - uploaded: formatSize(d.uploaded as long), - downloaded: formatSize(d.downloaded as long), + uploaded: utils.formatSize(d.uploaded as long), + downloaded: utils.formatSize(d.downloaded as long), bonus: d.bonus?.toString(), seeding: d.seeding?.toString(), - seedingSize: formatSize(d.seedingSize as long), + seedingSize: utils.formatSize(d.seedingSize as long), invite: d.memberCount?.invite?.toString(), ] ] @@ -52,7 +52,7 @@ private Map buildSearchResults(List items) { def st = it.status ?: [:] return [ title: it.name ?: '', - size: formatSize((it.size ?: 0) as long), + size: utils.formatSize((it.size ?: 0) as long), seeders: st.seeders ?: 0, leechers: st.leechers ?: 0, grabs: st.timesCompleted ?: 0, @@ -63,15 +63,6 @@ private Map buildSearchResults(List items) { return [pageName: '搜索', extracted: rows, message: "共 ${rows.size()} 条"] } -private String formatSize(long bytes) { - if (bytes == 0) return '0' - def units = ['B','KB','MB','GB','TB'] - double v = bytes - int i = 0 - while (v >= 1024 && i < units.size() - 1) { v /= 1024; i++ } - return String.format('%.2f %s', v, units[i]) -} - // ==================== mediabot 运行时入口 ==================== Map sync(PluginContext ctx) { diff --git a/par-core/src/main/java/com/par/core/adapter/GroovyScriptEngine.java b/par-core/src/main/java/com/par/core/adapter/GroovyScriptEngine.java index 7045af0..27c5768 100644 --- a/par-core/src/main/java/com/par/core/adapter/GroovyScriptEngine.java +++ b/par-core/src/main/java/com/par/core/adapter/GroovyScriptEngine.java @@ -132,5 +132,21 @@ public class GroovyScriptEngine { return null; } catch (Exception e) { return null; } } + /** 字节数 → 可读大小 */ + public String formatSize(long bytes) { + if (bytes == 0) return "0"; + String[] units = {"B","KB","MB","GB","TB"}; + double v = bytes; + int i = 0; + while (v >= 1024 && i < units.length - 1) { v /= 1024; i++; } + return String.format("%.2f %s", v, units[i]); + } + /** 字符串转数字 */ + public Long toLong(String s) { + try { return Long.parseLong(s.replaceAll("[^\\d-]", "")); } catch (NumberFormatException e) { return null; } + } + public Integer toInt(String s) { + try { return Integer.parseInt(s.replaceAll("[^\\d-]", "")); } catch (NumberFormatException e) { return null; } + } } }