feat: ScriptUtils 加 formatSize/toLong/toInt,脚本中直接用 utils.xxx() 调用

This commit is contained in:
mediabot-pt
2026-07-01 11:11:25 +08:00
parent 343f06cd62
commit 5c44180661
2 changed files with 20 additions and 13 deletions

View File

@@ -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) {

View File

@@ -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; }
}
}
}