Browse Source

first commit

main
hellodk34 2 years ago
parent
commit
0593703907
  1. 2
      .gitignore
  2. 21
      README.md
  3. 66
      pom.xml
  4. 174
      src/main/java/MainService.java

2
.gitignore

@ -11,3 +11,5 @@ buildNumber.properties
# https://github.com/takari/maven-wrapper#usage-without-binary-jar # https://github.com/takari/maven-wrapper#usage-without-binary-jar
.mvn/wrapper/maven-wrapper.jar .mvn/wrapper/maven-wrapper.jar
# idea related
.idea/

21
README.md

@ -1,3 +1,22 @@
# link-cleaner # link-cleaner
link cleaner for pinduoduo, taobao, jd, bilibili, and maybe more at the future. 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

66
pom.xml

@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hellodk</groupId>
<artifactId>link-cleaner</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-http</artifactId>
<version>5.7.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<!-- 运行 jar 包时运行的主类,要求写全类名 需要包含 package name -->
<mainClass>MainService</mainClass>
<!-- 是否指定项目 classpath 下的依赖 -->
<addClasspath>true</addClasspath>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

174
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;
}
}
Loading…
Cancel
Save