refactor: PTTIME脚本用http.get+utils.parseTraffic替代私有方法;工具方法移入ScriptUtils

This commit is contained in:
mediabot-pt
2026-07-01 23:16:05 +08:00
parent 0288af1866
commit 6f8108969d
2 changed files with 39 additions and 48 deletions

View File

@@ -193,7 +193,6 @@ 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"};
@@ -202,12 +201,33 @@ public class GroovyScriptEngine {
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; }
}
/** 解析流量字符串:600.44GB → 字节数 */
public long parseTraffic(String s) {
if (s == null) return 0;
double val = 0; String unit = null;
var m = java.util.regex.Pattern.compile("([\\d.,]+)\\s*(TB|GB|MB|KB|B)?", java.util.regex.Pattern.CASE_INSENSITIVE).matcher(s);
if (m.find()) {
val = Double.parseDouble(m.group(1).replace(",", ""));
unit = m.group(2);
}
if (unit == null) return (long) val;
return (long) (val * switch (unit.toUpperCase()) {
case "TB" -> 1099511627776L;
case "GB" -> 1073741824L;
case "MB" -> 1048576L;
case "KB" -> 1024L;
default -> 1L;
});
}
/** 解析 HTML 大小字符串:20.21GB → 字节数 */
public long parseHtmlSize(String s) {
return parseTraffic(s);
}
}
}