parser发布到maven central方便开发者依赖, pom文件结构调整

This commit is contained in:
q
2025-10-16 18:08:03 +08:00
parent eb06eb9f3d
commit c16bde6bb8
9 changed files with 642 additions and 101 deletions

View File

@@ -2,6 +2,7 @@ package cn.qaiu.parser;
import cn.qaiu.WebClientVertxInit;
import cn.qaiu.entity.ShareLinkInfo;
import cn.qaiu.util.HttpResponseHelper;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Promise;
@@ -17,10 +18,7 @@ import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Iterator;
@@ -223,20 +221,7 @@ public abstract class PanBase implements IPanTool {
* @return String
*/
protected String asText(HttpResponse<?> res) {
// 检查响应头中的Content-Encoding是否为gzip
String contentEncoding = res.getHeader("Content-Encoding");
try {
if ("gzip".equalsIgnoreCase(contentEncoding)) {
// 如果是gzip压缩的响应体解压
return decompressGzip((Buffer) res.body());
} else {
return res.bodyAsString();
}
} catch (Exception e) {
fail("解析失败: res格式异常");
//throw new RuntimeException("解析失败: res格式异常");
}
return null;
return HttpResponseHelper.asText(res);
}
protected void complete(String url) {
@@ -279,22 +264,16 @@ public abstract class PanBase implements IPanTool {
private String decompressGzip(Buffer compressedData) throws IOException {
try (ByteArrayInputStream bais = new ByteArrayInputStream(compressedData.getBytes());
GZIPInputStream gzis = new GZIPInputStream(bais);
BufferedReader reader = new BufferedReader(new InputStreamReader(gzis,
StandardCharsets.UTF_8))) {
InputStreamReader isr = new InputStreamReader(gzis, StandardCharsets.UTF_8);
StringWriter writer = new StringWriter()) {
// 用于存储解压后的字符串
StringBuilder decompressedData = new StringBuilder();
// 逐行读取解压后的数据
String line;
while ((line = reader.readLine()) != null) {
decompressedData.append(line);
char[] buffer = new char[4096];
int n;
while ((n = isr.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
// 此时decompressedData.toString()包含了解压后的字符串
return decompressedData.toString();
return writer.toString();
}
}
protected String getDomainName(){

View File

@@ -2,16 +2,12 @@ package cn.qaiu.parser.impl;
import cn.qaiu.entity.ShareLinkInfo;
import cn.qaiu.parser.PanBase;
import cn.qaiu.parser.PanDomainTemplate;
import cn.qaiu.parser.ParserCreate;
import io.vertx.core.Future;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.client.HttpRequest;
import java.net.URL;
import java.util.Arrays;
import java.util.Iterator;
/**
* <a href="https://github.com/cloudreve/Cloudreve">Cloudreve自建网盘解析</a> <br>

View File

@@ -29,6 +29,24 @@ public class LzTool extends PanBase {
public static final String SHARE_URL_PREFIX = "https://wwww.lanzoum.com";
MultiMap headers0 = HeaderUtils.parseHeaders("""
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6
Cache-Control: max-age=0
Cookie: codelen=1; pc_ad1=1
DNT: 1
Priority: u=0, i
Sec-CH-UA: "Chromium";v="140", "Not=A?Brand";v="24", "Microsoft Edge";v="140"
Sec-CH-UA-Mobile: ?0
Sec-CH-UA-Platform: "macOS"
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: cross-site
Sec-Fetch-User: ?1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0
""");
public LzTool(ShareLinkInfo shareLinkInfo) {
super(shareLinkInfo);
@@ -39,7 +57,7 @@ public class LzTool extends PanBase {
String pwd = shareLinkInfo.getSharePassword();
WebClient client = clientNoRedirects;
client.getAbs(sUrl).send().onSuccess(res -> {
client.getAbs(sUrl).putHeaders(headers0).send().onSuccess(res -> {
String html = res.bodyAsString();
// 匹配iframe
Pattern compile = Pattern.compile("src=\"(/fn\\?[a-zA-Z\\d_+/=]{16,})\"");
@@ -139,10 +157,13 @@ public class LzTool extends PanBase {
client.postAbs(url).putHeaders(headers).sendForm(map).onSuccess(res2 -> {
try {
JsonObject urlJson = asJson(res2);
String name = urlJson.getString("inf");
if (urlJson.getInteger("zt") != 1) {
fail(urlJson.getString("inf"));
fail(name);
return;
}
// 文件名
((FileInfo)shareLinkInfo.getOtherParam().get("fileInfo")).setFileName(name);
String downUrl = urlJson.getString("dom") + "/file/" + urlJson.getString("url");
headers.remove("Referer");
WebClientSession webClientSession = WebClientSession.create(client);

View File

@@ -0,0 +1,128 @@
package cn.qaiu.util;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpHeaders;
import io.vertx.ext.web.client.HttpResponse;
import io.vertx.core.json.JsonObject;
//import org.brotli.dec.BrotliInputStream;
import org.brotli.dec.BrotliInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPInputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
public class HttpResponseHelper {
static Logger LOGGER = LoggerFactory.getLogger(HttpResponseHelper.class);
// -------------------- 公共方法 --------------------
public static String asText(HttpResponse<?> res) {
String encoding = res.getHeader(HttpHeaders.CONTENT_ENCODING.toString());
try {
Buffer body = toBuffer(res);
if (encoding == null || "identity".equalsIgnoreCase(encoding)) {
return body.toString(StandardCharsets.UTF_8);
}
return decompress(body, encoding);
} catch (Exception e) {
LOGGER.error("asText: {}", e.getMessage(), e);
return null;
}
}
public static JsonObject asJson(HttpResponse<?> res) {
try {
String text = asText(res);
if (text != null) {
return new JsonObject(text);
} else {
LOGGER.error("asJson: asText响应数据为空");
return JsonObject.of();
}
} catch (Exception e) {
LOGGER.error("asJson: {}", e.getMessage(), e);
return JsonObject.of();
}
}
// -------------------- Buffer 转换 --------------------
private static Buffer toBuffer(HttpResponse<?> res) {
return res.body() instanceof Buffer ? (Buffer) res.body() : Buffer.buffer(res.bodyAsString());
}
// -------------------- 通用解压分发 --------------------
private static String decompress(Buffer compressed, String encoding) throws IOException {
return switch (encoding.toLowerCase()) {
case "gzip" -> decompressGzip(compressed);
case "deflate" -> decompressDeflate(compressed);
case "br" -> decompressBrotli(compressed);
//case "zstd" -> decompressZstd(compressed);
default -> throw new UnsupportedOperationException("不支持的 Content-Encoding: " + encoding);
};
}
// -------------------- gzip --------------------
private static String decompressGzip(Buffer compressed) throws IOException {
try (ByteArrayInputStream bais = new ByteArrayInputStream(compressed.getBytes());
GZIPInputStream gzis = new GZIPInputStream(bais);
InputStreamReader isr = new InputStreamReader(gzis, StandardCharsets.UTF_8);
StringWriter writer = new StringWriter()) {
char[] buffer = new char[4096];
int n;
while ((n = isr.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
return writer.toString();
}
}
// -------------------- deflate --------------------
private static String decompressDeflate(Buffer compressed) throws IOException {
byte[] bytes = compressed.getBytes();
try {
return inflate(bytes, false); // zlib 包裹
} catch (IOException e) {
return inflate(bytes, true); // 裸 deflate
}
}
private static String inflate(byte[] data, boolean nowrap) throws IOException {
try (ByteArrayInputStream bais = new ByteArrayInputStream(data);
InflaterInputStream iis = new InflaterInputStream(bais, new Inflater(nowrap));
InputStreamReader isr = new InputStreamReader(iis, StandardCharsets.UTF_8);
StringWriter writer = new StringWriter()) {
char[] buffer = new char[4096];
int n;
while ((n = isr.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
return writer.toString();
}
}
// -------------------- Brotli --------------------
private static String decompressBrotli(Buffer compressed) throws IOException {
try (ByteArrayInputStream bais = new ByteArrayInputStream(compressed.getBytes());
BrotliInputStream bis = new BrotliInputStream(bais);
InputStreamReader isr = new InputStreamReader(bis, StandardCharsets.UTF_8);
StringWriter writer = new StringWriter()) {
char[] buffer = new char[4096];
int n;
while ((n = isr.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
return writer.toString();
}
}
// -------------------- Zstandard --------------------
private static String decompressZstd(Buffer compressed) {
throw new UnsupportedOperationException("Zstandard");
}
}