feat: ScriptUtils 加 formatSize/toLong/toInt,脚本中直接用 utils.xxx() 调用
This commit is contained in:
@@ -37,11 +37,11 @@ private Map buildProfile(d) {
|
|||||||
fields: [
|
fields: [
|
||||||
id: d.id?.toString(),
|
id: d.id?.toString(),
|
||||||
username: d.username?.toString(),
|
username: d.username?.toString(),
|
||||||
uploaded: formatSize(d.uploaded as long),
|
uploaded: utils.formatSize(d.uploaded as long),
|
||||||
downloaded: formatSize(d.downloaded as long),
|
downloaded: utils.formatSize(d.downloaded as long),
|
||||||
bonus: d.bonus?.toString(),
|
bonus: d.bonus?.toString(),
|
||||||
seeding: d.seeding?.toString(),
|
seeding: d.seeding?.toString(),
|
||||||
seedingSize: formatSize(d.seedingSize as long),
|
seedingSize: utils.formatSize(d.seedingSize as long),
|
||||||
invite: d.memberCount?.invite?.toString(),
|
invite: d.memberCount?.invite?.toString(),
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
@@ -52,7 +52,7 @@ private Map buildSearchResults(List items) {
|
|||||||
def st = it.status ?: [:]
|
def st = it.status ?: [:]
|
||||||
return [
|
return [
|
||||||
title: it.name ?: '',
|
title: it.name ?: '',
|
||||||
size: formatSize((it.size ?: 0) as long),
|
size: utils.formatSize((it.size ?: 0) as long),
|
||||||
seeders: st.seeders ?: 0,
|
seeders: st.seeders ?: 0,
|
||||||
leechers: st.leechers ?: 0,
|
leechers: st.leechers ?: 0,
|
||||||
grabs: st.timesCompleted ?: 0,
|
grabs: st.timesCompleted ?: 0,
|
||||||
@@ -63,15 +63,6 @@ private Map buildSearchResults(List items) {
|
|||||||
return [pageName: '搜索', extracted: rows, message: "共 ${rows.size()} 条"]
|
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 运行时入口 ====================
|
// ==================== mediabot 运行时入口 ====================
|
||||||
|
|
||||||
Map sync(PluginContext ctx) {
|
Map sync(PluginContext ctx) {
|
||||||
|
|||||||
@@ -132,5 +132,21 @@ public class GroovyScriptEngine {
|
|||||||
return null;
|
return null;
|
||||||
} catch (Exception e) { 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; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user