From 0593703907b625334a0f02b7d0054000f27af63f Mon Sep 17 00:00:00 2001 From: hellodk34 Date: Wed, 24 Aug 2022 13:27:01 +0800 Subject: [PATCH] first commit --- .gitignore | 2 + README.md | 21 +++- pom.xml | 66 +++++++++++++ src/main/java/MainService.java | 174 +++++++++++++++++++++++++++++++++ 4 files changed, 262 insertions(+), 1 deletion(-) create mode 100644 pom.xml create mode 100644 src/main/java/MainService.java diff --git a/.gitignore b/.gitignore index 0880771..e88b582 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ buildNumber.properties # https://github.com/takari/maven-wrapper#usage-without-binary-jar .mvn/wrapper/maven-wrapper.jar +# idea related +.idea/ diff --git a/README.md b/README.md index eb1a30d..04f1c67 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,22 @@ # link-cleaner -link cleaner for pinduoduo, taobao, jd, bilibili, and maybe more at the future. \ No newline at end of file +link cleaner for pinduoduo, taobao, jd, bilibili, and maybe more at the future. + +# Usage + +1. install [OpenJDK11](https://www.injdk.cn/) for your Windows or Mac or Linux device. +2. download the jar file to any folder you like. +3. copy the original link that came from pdd or jd or taobao or bilibili, then just type `java -jar link-cleaner-1.0.0.jar` and run +4. the output is like this + ``` + java -jar link-cleaner-1.0.0.jar + your original clipboard text is: 【【定格动画】当我用20天手绘800张在纸上看《猫和老鼠》????-哔哩哔哩】 https://b23.tv/gbyeley + your new clipboard text is: 【【定格动画】当我用20天手绘800张在纸上看《猫和老鼠》????-哔哩哔哩】 https://www.bilibili.com/video/BV19t4y1G7dL + ``` + ps: the last three chars of the `b23.tv` link have been mosaicked. +5. supported sites at the moment + - pinduoduo `yangkeduo.com` + - jd `jd.com` + - taobao `taobao.com`、`tb.cn` + - bilibili `bilibili.com`、`b23.tv` +6. welcome your issue or pr diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..2d9be78 --- /dev/null +++ b/pom.xml @@ -0,0 +1,66 @@ + + + 4.0.0 + + com.hellodk + link-cleaner + 1.0.0 + + + 11 + 11 + UTF-8 + + + + + cn.hutool + hutool-http + 5.7.10 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 11 + 11 + + + + org.apache.maven.plugins + maven-assembly-plugin + 3.2.0 + + + + + MainService + + true + + + + jar-with-dependencies + + + + + make-assembly + package + + single + + + + + + + + \ No newline at end of file diff --git a/src/main/java/MainService.java b/src/main/java/MainService.java new file mode 100644 index 0000000..0ed9299 --- /dev/null +++ b/src/main/java/MainService.java @@ -0,0 +1,174 @@ +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpResponse; + +import java.awt.Toolkit; +import java.awt.datatransfer.Clipboard; +import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.StringSelection; +import java.awt.datatransfer.Transferable; +import java.awt.datatransfer.UnsupportedFlavorException; +import java.io.IOException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * @author: hellodk + * @description main service + * @date: 2022/8/24 12:17 + */ + +public class MainService { + public static void main(String[] args) { + MainService main = new MainService(); + main.mainEntry(); + } + + private String pddGoodsUri(String url) { + String[] split = url.split("&"); + String[] goods = url.split("\\?"); + String param = ""; + if (split.length == 1) { + return split[0]; + } + for (int i = 0; i < split.length; i++) { + String item = split[i]; + if (item.startsWith("goods") && item.contains("goods_id=")) { + param = item; + break; + } + } + return goods[0].concat("?").concat(param); + } + + private String biliUri(String url) { + String uri = url; + int httpsIndex = uri.indexOf("https"); + String text = uri.substring(0, httpsIndex); + uri = uri.substring(httpsIndex); + if (uri.contains("b23.tv")) { + HttpResponse resp = HttpRequest.head(uri).timeout(20000).execute(); + String location = resp.header("Location"); + uri = location; + } + return (text.length() == 0 ? "" : (text + " ")) + removeParams(uri); + } + + private String jdUri(String url) { + return removeParams(url); + } + + private String removeParams(String uri) { + String[] split = uri.split("\\?"); + return split[0]; + } + + private String taobaoUri(String url) { + String uri = url; + if (uri.contains("taobao.com")) { + return getTaobaocomUri(uri); + } + int start = uri.indexOf("https"); + int end = uri.indexOf(" "); + String realUri = uri.substring(start, end); + String result = HttpRequest.get(realUri).timeout(20000).execute().body(); + String regex = "var url = '([^\\r\\n]*)';"; + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(result); + if (matcher.find()) { + uri = matcher.group(1); + } + return getTaobaocomUri(uri); + } + + private String getTaobaocomUri(String url) { + String uri = url; + String[] split = uri.split("&"); + if (split.length == 1) { + return split[0]; + } + String[] id = uri.split("\\?"); + String param = ""; + for (int i = 0; i < split.length; i++) { + String item = split[i]; + if (item.startsWith("id") && item.contains("id=")) { + param = item; + break; + } + } + return id[0].concat("?").concat(param); + } + + private void mainEntry() { + String uri = getSysClipboard(); + String site = ""; + if (uri.contains("yangkeduo.com")) { + site = "pdd"; + } + else if (uri.contains("jd.com")) { + site = "jd"; + } + else if (uri.contains("taobao.com") || uri.contains("tb.cn")) { + site = "taobao"; + } + else if (uri.contains("bilibili.com") || uri.contains("b23.tv")) { + site = "bili"; + } + // TODO 支持更多网站 + + if (ObjectUtil.isEmpty(site)) { + System.out.println(uri + " not supported at the moment."); + } + String result; + switch (site) { + case "pdd": + result = pddGoodsUri(uri); + break; + case "jd": + result = jdUri(uri); + break; + case "taobao": + result = taobaoUri(uri); + break; + case "bili": + result = biliUri(uri); + break; + default: + //System.exit(0); + return; + } + setSysClipboard(result); + System.out.println("your new clipboard text is: " + result); + } + + private void setSysClipboard(String myString) { + StringSelection stringSelection = new StringSelection(myString); + Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); + clipboard.setContents(stringSelection, null); + } + + private String getSysClipboard() { + String result = ""; + Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard(); + Transferable tr = clip.getContents(null); + if (tr != null) { + // 检查是文本类型再处理,其他类型不处理 + if (tr.isDataFlavorSupported(DataFlavor.stringFlavor)) { + try { + result = (String) tr.getTransferData(DataFlavor.stringFlavor); + } + catch (UnsupportedFlavorException | IOException ex) { + System.out.println(ex); + } + } + else { + System.out.println("only deal with text."); + } + } + else { + System.out.println("Transferable is null!"); + } + System.out.println("your original clipboard text is: " + result); + return result; + } +}