diff --git a/gradle.properties b/gradle.properties index f0b6faa..6a0e7c4 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.6.0.2 +VERSION = 0.7.0.0 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 1316cb7..600389b 100644 --- a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java +++ b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java @@ -4,6 +4,6 @@ package cc.sukazyo.cono.morny; * the final field that will be updated by gradle automatically. */ public class GradleProjectConfigures { - public static final String VERSION = "0.6.0.2"; - public static final long COMPILE_TIMESTAMP = 1647237887029L; + public static final String VERSION = "0.7.0.0"; + public static final long COMPILE_TIMESTAMP = 1647369713069L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/util/TelegramUserInformation.java b/src/main/java/cc/sukazyo/cono/morny/util/TelegramUserInformation.java index 011e4da..18e109f 100644 --- a/src/main/java/cc/sukazyo/cono/morny/util/TelegramUserInformation.java +++ b/src/main/java/cc/sukazyo/cono/morny/util/TelegramUserInformation.java @@ -1,10 +1,40 @@ package cc.sukazyo.cono.morny.util; import com.pengrad.telegrambot.model.User; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import okhttp3.ResponseBody; + +import javax.annotation.Nullable; +import java.io.IOException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml; public class TelegramUserInformation { + public static final String DC_QUERY_SOURCE_SITE = "https://t.me/"; + public static final Pattern DC_QUERY_PROCESSOR_REGEX = Pattern.compile("(cdn[1-9]).tele(sco.pe|gram-cdn.org)"); + + private static final OkHttpClient httpClient = new OkHttpClient(); + + @Nullable + public static String getDataCenterFromUsername (String username) { + final Request request = new Request.Builder().url(DC_QUERY_SOURCE_SITE + username).build(); + try (Response response = httpClient.newCall(request).execute()) { + final ResponseBody body = response.body(); + if (body == null) return "empty upstream response"; + final Matcher matcher = DC_QUERY_PROCESSOR_REGEX.matcher(body.string()); + if (matcher.find()) { + return matcher.group(1); + } + } catch (IOException e) { + return e.getMessage(); + } + return null; + } + public static String informationOutputHTML (User user) { final StringBuilder userInformation = new StringBuilder(); @@ -15,7 +45,7 @@ public class TelegramUserInformation { user.id() )); if (user.username() == null) { - userInformation.append("\nusername : null"); + userInformation.append("\nusername : null\ndatacenter : null"); } else { userInformation.append(String.format( """ @@ -24,6 +54,10 @@ public class TelegramUserInformation { - %s""", escapeHtml(user.username()) )); + // 依赖 username 的 datacenter 查询 + final String dataCenter = getDataCenterFromUsername(user.username()); + if (dataCenter == null) { userInformation.append("\ndatacenter : null"); } + else { userInformation.append(String.format("\ndatacenter : %s", escapeHtml(dataCenter))); } } if (user.firstName() == null) { userInformation.append("\nfirstname : null");