下载地址:https://www.pan38.com/yun/share.php?code=JCnzE 提取密码:7782
完整的快手ID转换功能,包括3mid转真实ID、原始ID转快手号以及批量转换功能。代码结构清晰,包含了核心转换逻辑、主程序入口和单元测试。使用时需要确保网络连接正常,因为需要调用快手API进行转换。
<?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.kuaishou</groupId>
<artifactId>kuaishou-id-converter</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
</dependencies>
</project>
package com.kuaishou.converter;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
public class KuaishouIDConverter {
private static final String MID_TO_ID_API = "https://api.kuaishou.com/api/user/mid2id";
private static final String ID_TO_NUMBER_API = "https://api.kuaishou.com/api/user/id2number";
private static final Pattern MID_PATTERN = Pattern.compile("^[a-zA-Z0-9]{16}$");
private static final Pattern ID_PATTERN = Pattern.compile("^[a-zA-Z0-9]{32}$");
private final CloseableHttpClient httpClient;
public KuaishouIDConverter() {
this.httpClient = HttpClients.createDefault();
}
public String convertMidToId(String mid) throws IOException, InvalidMidException {
if (!MID_PATTERN.matcher(mid).matches()) {
throw new InvalidMidException("Invalid mid format: " + mid);
}
HttpGet request = new HttpGet(MID_TO_ID_API + "?mid=" + mid);
try (CloseableHttpResponse response = httpClient.execute(request)) {
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
JsonObject json = JsonParser.parseString(result).getAsJsonObject();
if (json.has("error")) {
throw new IOException("API error: " + json.get("error").getAsString());
}
return json.get("id").getAsString();
}
}
public String convertIdToNumber(String id) throws IOException, InvalidIdException {
if (!ID_PATTERN.matcher(id).matches()) {
throw new InvalidIdException("Invalid id format: " + id);
}
HttpGet request = new HttpGet(ID_TO_NUMBER_API + "?id=" + id);
try (CloseableHttpResponse response = httpClient.execute(request)) {
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
JsonObject json = JsonParser.parseString(result).getAsJsonObject();
if (json.has("error")) {
throw new IOException("API error: " + json.get("error").getAsString());
}
return json.get("number").getAsString();
}
}
public Map<String, String> batchConvertMidToId(String[] mids) throws IOException {
Map<String, String> resultMap = new HashMap<>();
for (String mid : mids) {
try {
String id = convertMidToId(mid);
resultMap.put(mid, id);
} catch (InvalidMidException e) {
resultMap.put(mid, "ERROR: " + e.getMessage());
}
}
return resultMap;
}
public void close() throws IOException {
httpClient.close();
}
public static class InvalidMidException extends Exception {
public InvalidMidException(String message) {
super(message);
}
}
public static class InvalidIdException extends Exception {
public InvalidIdException(String message) {
super(message);
}
}
}
com.kuaishou.converter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Map;
public class Main {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
public static void main(String[] args) {
KuaishouIDConverter converter = new KuaishouIDConverter();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("快手ID转换工具");
System.out.println("1. 3mid转真实ID");
System.out.println("2. 原始ID转快手号");
System.out.println("3. 批量3mid转真实ID");
System.out.print("请选择功能: ");
String choice = reader.readLine();
switch (choice) {
case "1":
handleMidToId(converter, reader);
break;
case "2":
handleIdToNumber(converter, reader);
break;
case "3":
handleBatchMidToId(converter, reader);
break;
default:
System.out.println("无效选择");
}
} catch (IOException e) {
System.err.println("发生IO错误: " + e.getMessage());
} finally {
try {
converter.close();
} catch (IOException e) {
System.err.println("关闭转换器时出错: " + e.getMessage());
}
}
}
private static void handleMidToId(KuaishouIDConverter converter, BufferedReader reader) throws IOException {
System.out.print("请输入3mid: ");
String mid = reader.readLine();
try {
String id = converter.convertMidToId(mid);
System.out.println("转换结果: " + id);
} catch (KuaishouIDConverter.InvalidMidException e) {
System.err.println("无效的3mid: " + e.getMessage());
}
}
private static void handleIdToNumber(KuaishouIDConverter converter, BufferedReader reader) throws IOException {
System.out.print("请输入原始ID: ");
String id = reader.readLine();
try {
String number = converter.convertIdToNumber(id);
System.out.println("转换结果: " + number);
} catch (KuaishouIDConverter.InvalidIdException e) {
System.err.println("无效的原始ID: " + e.getMessage());
}
}
private static void handleBatchMidToId(KuaishouIDConverter converter, BufferedReader reader) throws IOException {
System.out.print("请输入多个3mid,用逗号分隔: ");
String input = reader.readLine();
String[] mids = Arrays.stream(input.split(","))
.map(String::trim)
.toArray(String[]::new);
Map<String, String> result = converter.batchConvertMidToId(mids);
System.out.println("批量转换结果:");
System.out.println(GSON.toJson(result));
}
}
package com.kuaishou.converter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import static org.junit.Assert.*;
public class KuaishouIDConverterTest {
private KuaishouIDConverter converter;
@Before
public void setUp() {
converter = new KuaishouIDConverter();
}
@After
public void tearDown() throws IOException {
converter.close();
}
@Test(expected = KuaishouIDConverter.InvalidMidException.class)
public void testInvalidMid() throws Exception {
converter.convertMidToId("invalid_mid");
}
@Test(expected = KuaishouIDConverter.InvalidIdException.class)
public void testInvalidId() throws Exception {
converter.convertIdToNumber("invalid_id");
}
@Test
public void testBatchConvert() throws IOException {
String[] mids = {"3x4y5z6a7b8c9d0e", "1a2b3c4d5e6f7g8h"};
Map<String, String> result = converter.batchConvertMidToId(mids);
assertEquals(2, result.size());
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。