From 82c9faea1edbd457df34b766c31dc50bcb865d6e Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Wed, 16 Mar 2022 02:43:47 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E7=94=A8=E6=88=B7=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=AE=9E=E9=AA=8C=E6=80=A7=E7=9A=84=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E4=BA=86=E8=8E=B7=E5=8F=96=E7=94=A8=E6=88=B7=E6=89=80=E5=9C=A8?= =?UTF-8?q?dc=20(cdn)=20#6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +-- .../morny/util/TelegramUserInformation.java | 36 ++++++++++++++++++- 3 files changed, 38 insertions(+), 4 deletions(-) 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");