1. 优化123pan日志, 2. 微博短链测试, 3. 分享类添加其他参数Map(Cookie支持准备)

This commit is contained in:
QAIU
2024-11-01 18:18:29 +08:00
parent b042df93b7
commit f07800985d
5 changed files with 69 additions and 35 deletions

View File

@@ -21,6 +21,7 @@ public class ShareLinkInfo {
this.sharePassword = builder.sharePassword;
this.shareUrl = builder.shareUrl;
this.standardUrl = builder.standardUrl;
this.otherParam = builder.otherParam;
}
// Getter和Setter方法
@@ -78,6 +79,14 @@ public class ShareLinkInfo {
return type + ":" + shareKey;
}
public ShareLinkInfo setOtherParam(Map<String, Object> otherParam) {
this.otherParam = otherParam;
return this;
}
public Map<String, Object> getOtherParam() {
return otherParam;
}
// 静态方法创建建造者对象
public static ShareLinkInfo.Builder newBuilder() {

View File

@@ -95,11 +95,9 @@ public abstract class PanBase implements IPanTool {
protected void fail(String errorMsg, Object... args) {
try {
String s = String.format(errorMsg.replaceAll("\\{}", "%s"), args);
log.error("解析异常: " + s);
promise.fail(shareLinkInfo.getPanName() + "-" + shareLinkInfo.getType() + " - 解析异常: " + s);
} catch (Exception e) {
log.error("ErrorMsg format fail. The parameter has been discarded", e);
log.error("解析异常: " + errorMsg);
promise.fail(shareLinkInfo.getPanName() + "-" + shareLinkInfo.getType() + " - 解析异常: " + errorMsg);
}
}

View File

@@ -2,19 +2,24 @@ package cn.qaiu.parser.impl;
import cn.qaiu.entity.ShareLinkInfo;
import cn.qaiu.parser.PanBase;
import io.netty.handler.codec.http.cookie.Cookie;
import io.netty.handler.codec.http.cookie.DefaultCookie;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.MultiMap;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.impl.headers.HeadersMultiMap;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.client.HttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* t.cn 短链生成
* t.cn 短链生成 //
*/
public class GenShortUrl extends PanBase {
@@ -22,33 +27,46 @@ public class GenShortUrl extends PanBase {
private static final String COMMENT_URL = "https://www.weibo.com/aj/v6/comment/add";
private static final String DELETE_COMMENT_URL = "https://www.weibo.com/aj/comment/del";
private static final String MID = "5094736413852129"; // 微博的mid
private static final String WRAPPER_URL = "https://www.so.com/link?m=ewgUSYiFWXIoTybC3fJH8YoJy8y10iRquo6cazgINwWjTn3HvVJ92TrCJu0PmMUR0RMDfOAucP3wa4G8j64SrhNH9Z0Cr0PEyn9ASuvpkUGmAjjUEGJkO5%2BIDGWVrEkPHsL7UsoKO6%2BlT%2BD6r&ccc=";
private static final String MID = "5095144728824883"; // 微博的mid
private static final MultiMap HEADER = HeadersMultiMap.headers()
.add("Content-Type", "application/x-www-form-urlencoded")
.add("Referer", "https://www.weibo.com")
.add("Content-Type", "application/x-www-form-urlencoded")
.add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36");
Cookie cookie = new DefaultCookie("SUB", "_2A25KJE5vDeRhGeRJ6lsR9SjJzDuIHXVpWM-nrDV8PUJbkNAbLVPlkW1NUmJm3GjYtRHBsHdMUKafkdTL_YheMEmu");
public GenShortUrl(ShareLinkInfo build) {
super(build);
}
@Override
public Future<String> parse() {
String longUrl = shareLinkInfo.getStandardUrl();
return addComment(longUrl)
.compose(commentId -> deleteComment(commentId).map(v -> longUrl));
}
String longUrl = shareLinkInfo.getShareUrl();
private Future<String> addComment(String longUrl) {
Promise<String> promise = Promise.promise();
String url = WRAPPER_URL + Base64.getEncoder().encodeToString(longUrl.getBytes());
String payload = "mid=" + MID + "&content=" + longUrl;
String payload = "mid=" + MID + "&content=" + URLEncoder.encode(url, StandardCharsets.UTF_8);
clientSession.cookieStore().put(cookie);
clientSession.postAbs(COMMENT_URL)
.putHeader("Content-Type", "application/x-www-form-urlencoded")
.putHeaders(HEADER)
.sendBuffer(Buffer.buffer(payload))
.onSuccess(res -> {
String shortUrl = extractShortUrl(res);
JsonObject data = asJson(res).getJsonObject("data");
if (data.isEmpty()) {
fail(asJson(res).getString("msg"));
return;
}
String comment = data.getString("comment");
String shortUrl = extractShortUrl(comment);
if (shortUrl != null) {
log.info("生成的短链:{}", shortUrl);
String commentId = extractCommentId(res);
String commentId = extractCommentId(comment);
if (commentId != null) {
promise.complete(commentId);
deleteComment(commentId);
} else {
promise.fail("未能提取评论ID");
}
@@ -64,43 +82,38 @@ public class GenShortUrl extends PanBase {
return promise.future();
}
private Future<Void> deleteComment(String commentId) {
Promise<Void> promise = Promise.promise();
private void deleteComment(String commentId) {
String payload = "mid=" + MID + "&cid=" + commentId;
clientSession.postAbs(DELETE_COMMENT_URL)
.putHeader("Content-Type", "application/x-www-form-urlencoded")
.putHeaders(HEADER)
.sendBuffer(Buffer.buffer(payload))
.onSuccess(res -> {
JsonObject responseJson = res.bodyAsJsonObject();
if (responseJson.getString("code").equals("100000")) {
log.info("评论已删除: {}", commentId);
promise.complete();
} else {
promise.fail("删除评论失败,返回码:" + responseJson.getString("code"));
log.error("删除评论失败: {}", responseJson.encode());
}
})
.onFailure(err -> {
log.error("删除评论失败", err);
promise.fail(err);
});
return promise.future();
}
private String extractShortUrl(HttpResponse<Buffer> response) {
private String extractShortUrl(String comment) {
Pattern pattern = Pattern.compile("(https?)://t.cn/\\w+");
Matcher matcher = pattern.matcher(response.bodyAsString());
Matcher matcher = pattern.matcher(comment);
if (matcher.find()) {
return matcher.group(0);
}
return null;
}
private String extractCommentId(HttpResponse<Buffer> response) {
private String extractCommentId(String comment) {
Pattern pattern = Pattern.compile("comment_id=\"(\\d+)\"");
Matcher matcher = pattern.matcher(response.bodyAsString());
Matcher matcher = pattern.matcher(comment);
if (matcher.find()) {
return matcher.group(1);
}
@@ -108,7 +121,12 @@ public class GenShortUrl extends PanBase {
}
public static void main(String[] args) {
new GenShortUrl(ShareLinkInfo.newBuilder().build());
// http://t.cn/A6nfZn86
// http://t.cn/A6nfZn86
new GenShortUrl(ShareLinkInfo.newBuilder().shareUrl("https://qaiu.top/sdfsdf").build()).parse().onSuccess(
System.out::println
);
}
}

View File

@@ -102,7 +102,7 @@ public class MkgsTool extends PanBase {
}).onFailure(handleFail(locationURL));
}
public static class MkgTool extends MkgsTool{
public static class MkgTool extends MkgsTool {
public MkgTool(ShareLinkInfo shareLinkInfo) {
super(shareLinkInfo);
@@ -112,10 +112,12 @@ public class MkgsTool extends PanBase {
public Future<String> parse() {
downUrl(shareLinkInfo.getStandardUrl());
return promise.future();
};
}
;
}
public static class Mkgs2Tool extends MkgTool{
public static class Mkgs2Tool extends MkgTool {
public Mkgs2Tool(ShareLinkInfo shareLinkInfo) {
super(shareLinkInfo);
}

View File

@@ -1,6 +1,6 @@
package cn.qaiu.parser.impl;
import cn.qaiu.entity.ShareLinkInfo;
import cn.qaiu.entity.ShareLinkInfo;
import cn.qaiu.parser.PanBase;
import cn.qaiu.util.CommonUtils;
import cn.qaiu.util.JsExecUtils;
@@ -42,11 +42,17 @@ public class YeTool extends PanBase {
client.getAbs(UriTemplate.of(FIRST_REQUEST_URL)).setTemplateParam("key", dataKey).send().onSuccess(res -> {
String html = res.bodyAsString();
// 判断分享是否已经失效
if (html.contains("分享链接已失效")) {
fail("该分享已失效({})已失效", shareLinkInfo.getShareUrl());
return;
}
Pattern compile = Pattern.compile("window.g_initialProps\\s*=\\s*(.*);");
Matcher matcher = compile.matcher(html);
if (!matcher.find()) {
fail(html + "\n Ye: " + dataKey + " 正则匹配失败");
fail("该分享({})文件信息找不到, 可能分享已失效", shareLinkInfo.getShareUrl());
return;
}
String fileInfoString = matcher.group(1);
@@ -59,6 +65,7 @@ public class YeTool extends PanBase {
return;
}
String shareKey = resJson.getJsonObject("data").getString("ShareKey");
if (resListJson == null || resListJson.getInteger("code") != 0) {
// 加密分享
if (StringUtils.isNotEmpty(pwd)) {