refactor: PTTIME脚本用http.get+utils.parseTraffic替代私有方法;工具方法移入ScriptUtils
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
// PTTIME 适配脚本
|
||||
// 内置变量: content, url, jsoup(Jsoup), json(Jackson), utils, http(httpHelper)
|
||||
// 导入: groovy.json.*, java.net.http.*, com.par.core.adapter.*, com.mediabot.site.*
|
||||
// 内置: content, url, jsoup, json, utils, http
|
||||
|
||||
// ==================== PAR 校验入口 ====================
|
||||
// ==================== PAR 校验 ====================
|
||||
|
||||
Map parse(String content, String url) {
|
||||
def doc = Jsoup.parse(content)
|
||||
@@ -12,11 +11,9 @@ Map parse(String content, String url) {
|
||||
if (url.contains('/attendance.php')) return parseSign(doc)
|
||||
if (url.contains('/index.php') && !url.contains('?')) return parseIndex(doc)
|
||||
|
||||
return [pageName: '未知页面', message: "未配置解析: ${url}"]
|
||||
return [pageName: '未知页面', message: "未配置: ${url}"]
|
||||
}
|
||||
|
||||
// ── 首页 / 用户信息 ──
|
||||
|
||||
private Map parseIndex(doc) {
|
||||
def infoEl = doc.selectFirst('#info_block')
|
||||
if (!infoEl) return [pageName: '首页', message: '未找到用户信息']
|
||||
@@ -34,11 +31,9 @@ private Map parseIndex(doc) {
|
||||
if (bm) fields.bonus = bm[0][1]
|
||||
if (sm) fields.seeding = sm[0][1]
|
||||
if (lm) fields.leeching = lm[0][1]
|
||||
return [pageName: '用户信息', fields: fields, message: fields.isEmpty() ? '未提取到数据' : '解析成功']
|
||||
return [pageName: '用户信息', fields: fields, message: fields.isEmpty() ? '未提取' : '解析成功']
|
||||
}
|
||||
|
||||
// ── 搜索(无 tbody, 列索引方式)──
|
||||
|
||||
private Map parseSearch(doc) {
|
||||
def rows = []
|
||||
doc.select('table.torrents tr:has(td.rowfollow):not(:has(.colhead))').each { row ->
|
||||
@@ -62,26 +57,21 @@ private Map parseSearch(doc) {
|
||||
return [pageName: '搜索', extracted: rows, message: "共 ${rows.size()} 条"]
|
||||
}
|
||||
|
||||
// ── 种子详情 ──
|
||||
|
||||
private Map parseDetail(doc) {
|
||||
def titleEl = doc.selectFirst('a.torrentname_title b, #top h1')
|
||||
def descEl = doc.selectFirst('td.embedded font[title], #kdescr')
|
||||
return [pageName: '种子详情', fields: [
|
||||
title: titleEl?.text(),
|
||||
description: descEl?.text()?.take(200)
|
||||
title: titleEl?.text(), description: descEl?.text()?.take(200)
|
||||
]]
|
||||
}
|
||||
|
||||
// ── 签到 ──
|
||||
|
||||
private Map parseSign(doc) {
|
||||
def t = doc.body().text()
|
||||
def matched = t ==~ /.*(签到成功|已签到|重复签到|获得.*魔力值).*/
|
||||
return [pageName: '签到', fields: [result: t.take(200)], message: matched ? '成功' : '未知']
|
||||
}
|
||||
|
||||
// ==================== mediabot 运行时 ====================
|
||||
// ==================== mediabot 运行时(统一用 http.get/post + utils) ====================
|
||||
|
||||
Map sign(PluginContext ctx) {
|
||||
if (!ctx.auth) return [message: '未配置认证', loginRequired: true]
|
||||
@@ -98,7 +88,7 @@ Map sync(PluginContext ctx) {
|
||||
def body = httpGet(ctx, '/index.php')
|
||||
def doc = Jsoup.parse(body)
|
||||
def infoEl = doc.selectFirst('#info_block')
|
||||
if (!infoEl) { ctx.incomplete = true; return [message: '未找到用户信息区域'] }
|
||||
if (!infoEl) { ctx.incomplete = true; return [message: '未找到用户信息'] }
|
||||
def t = infoEl.text()
|
||||
def result = [:]
|
||||
def um = t =~ /上传[::]\s*([0-9.,]+)\s*(TB|GB|MB|KB|B)?/
|
||||
@@ -106,8 +96,8 @@ Map sync(PluginContext ctx) {
|
||||
def rm = t =~ /分享率[::]\s*([\d.,]+)/
|
||||
def bm = t =~ /魔力值\([^)]*\)[^0-9]*([\d.,]+)/
|
||||
def sm = t =~ /⬆\s*(\d+)/
|
||||
if (um) result.upload = parseTraffic(um[0][1].replace(',','') as double, um[0][2])
|
||||
if (dm) result.download = parseTraffic(dm[0][1].replace(',','') as double, dm[0][2])
|
||||
if (um) result.upload = utils.parseTraffic(um[0][1] + (um[0][2] ?: ''))
|
||||
if (dm) result.download = utils.parseTraffic(dm[0][1] + (dm[0][2] ?: ''))
|
||||
if (rm) result.shareRatio = rm[0][1].replace(',','') as double
|
||||
if (bm) result.bonus = bm[0][1].replace(',','') as double
|
||||
if (sm) result.seeding = sm[0][1] as int
|
||||
@@ -131,40 +121,21 @@ List search(PluginContext ctx, Map params) {
|
||||
rows << [
|
||||
title: titleEl.text(),
|
||||
detailUrl: "${ctx.siteUrl}${titleEl.attr('href')}",
|
||||
size: tds.size() > 5 ? parseHtmlSize(tds[5].text()) : 0,
|
||||
seeders: tds.size() > 6 ? (tds[6].text() as int ?: 0) : 0,
|
||||
leechers: tds.size() > 7 ? (tds[7].text() as int ?: 0) : 0,
|
||||
grabs: tds.size() > 8 ? (tds[8].text() as int ?: 0) : 0,
|
||||
size: tds.size() > 5 ? utils.parseHtmlSize(tds[5].text()) : 0,
|
||||
seeders: tds.size() > 6 ? utils.toInt(tds[6].text()) ?: 0 : 0,
|
||||
leechers: tds.size() > 7 ? utils.toInt(tds[7].text()) ?: 0 : 0,
|
||||
grabs: tds.size() > 8 ? utils.toInt(tds[8].text()) ?: 0 : 0,
|
||||
]
|
||||
}
|
||||
return rows
|
||||
} catch (e) { return [] }
|
||||
}
|
||||
|
||||
// ==================== HTTP 工具 ====================
|
||||
// ── HTTP 封装(mediabot用,设置 capturedUrl/Body 以便自动提交到 PAR)──
|
||||
|
||||
private String httpGet(PluginContext ctx, String path) {
|
||||
def url = ctx.siteUrl.replaceAll('/+\$', '') + '/' + path.replaceAll('^/+', '')
|
||||
def builder = HttpRequest.newBuilder().uri(java.net.URI.create(url))
|
||||
.timeout(Duration.ofSeconds(15))
|
||||
.header('User-Agent', 'MediaBot/1.0')
|
||||
.GET()
|
||||
if (ctx.auth) builder.header('Cookie', ctx.auth)
|
||||
def resp = ctx.httpClient.send(builder.build(), HttpResponse.BodyHandlers.ofString())
|
||||
def url = path.startsWith('http') ? path : ctx.siteUrl.replaceAll('/+\$', '') + '/' + path.replaceAll('^/+', '')
|
||||
ctx.capturedUrl = url
|
||||
ctx.capturedBody = resp.body()
|
||||
return resp.body()
|
||||
}
|
||||
|
||||
// ==================== 数字工具 ====================
|
||||
|
||||
private long parseTraffic(double val, String unit) {
|
||||
def m = [B:1, KB:1024, MB:1048576, GB:1073741824, TB:1099511627776]
|
||||
return (val * (m[unit?.toUpperCase()] ?: 1)) as long
|
||||
}
|
||||
|
||||
private long parseHtmlSize(String s) {
|
||||
def m = s =~ /([\d.,]+)\s*(TB|GB|MB|KB|B)?/
|
||||
if (!m) return 0
|
||||
return parseTraffic(m[0][1].replace(',','') as double, m[0][2])
|
||||
ctx.capturedBody = http.get(url, ctx.auth)
|
||||
return ctx.capturedBody
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user