From eddee5647fa893626197d16d062cc481d6c0c459 Mon Sep 17 00:00:00 2001 From: zhaoqichao <1416537683@qq.com> Date: Thu, 6 Jul 2023 10:28:04 +0800 Subject: [PATCH] =?UTF-8?q?Url=E8=A7=A3=E6=9E=90=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E9=87=8D=E6=9E=84=EF=BC=88=E4=B8=8D=E5=BD=B1?= =?UTF-8?q?=E5=93=8D=E6=97=A7=E7=89=88=E6=9C=AC=E4=BD=BF=E7=94=A8=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/utils/chain/CommonUrlHandler.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 mallchat-common/src/main/java/com/abin/mallchat/common/common/utils/chain/CommonUrlHandler.java diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/common/utils/chain/CommonUrlHandler.java b/mallchat-common/src/main/java/com/abin/mallchat/common/common/utils/chain/CommonUrlHandler.java new file mode 100644 index 0000000..5d85f00 --- /dev/null +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/common/utils/chain/CommonUrlHandler.java @@ -0,0 +1,51 @@ +package com.abin.mallchat.common.common.utils.chain; + +import cn.hutool.core.util.StrUtil; +import org.jetbrains.annotations.Nullable; +import org.jsoup.nodes.Document; + +/** + * Description: + * Author: achao + * Date: 2023/7/6 9:25 + */ +public class CommonUrlHandler extends FactoryUrlHandler { + + @Nullable + @Override + public String getTitle(Document document) { + return document.title(); + } + + @Nullable + @Override + public String getDescription(Document document) { + String description = document.head().select("meta[name=description]").attr("content"); + String keywords = document.head().select("meta[name=keywords]").attr("content"); + String content = StrUtil.isNotBlank(description) ? description : keywords; + //只保留一句话的描述 + return StrUtil.isNotBlank(content) ? content.substring(0, content.indexOf("。")) : content; + } + + @Nullable + @Override + public String getImage(String url, Document document) { + String image = document.select("link[type=image/x-icon]").attr("href"); + //如果没有去匹配含有icon属性的logo + String href = StrUtil.isEmpty(image) ? document.select("link[rel$=icon]").attr("href") : image; + //如果icon中已经包含了url部分域名 + if (StrUtil.isNotBlank(StrUtil.removeAny(StrUtil.removeAny(href, "/"), "favicon.ico")) && + StrUtil.containsAny(StrUtil.removePrefix(url, "http://"), StrUtil.removeAny(StrUtil.removeAny(href, "/"), "favicon.ico"))) { + return "http://" + StrUtil.removePrefix(href, "/"); + } + //如果url已经包含了logo + if (StrUtil.containsAny(url, "favicon")) { + return url; + } + //如果logo中有url + if (StrUtil.containsAny(href, "http") || StrUtil.containsAny(href, "https")) { + return href; + } + return StrUtil.format("{}/{}", url, StrUtil.removePrefix(href, "/")); + } +}