获取用户数据实验性的支持了获取用户所在dc (cdn) #6

This commit is contained in:
A.C.Sukazyo Eyre 2022-03-16 02:43:47 +08:00
parent 79e61e28a2
commit 82c9faea1e
Signed by: Eyre_S
GPG Key ID: EFB47D98FE082FAD
3 changed files with 38 additions and 4 deletions

View File

@ -1,6 +1,6 @@
## Core
VERSION = 0.6.0.2
VERSION = 0.7.0.0
# dependencies

View File

@ -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;
}

View File

@ -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 : <u>null</u>");
userInformation.append("\nusername : <u>null</u>\ndatacenter : <u>null</u>");
} else {
userInformation.append(String.format(
"""
@ -24,6 +54,10 @@ public class TelegramUserInformation {
- <code>%s</code>""",
escapeHtml(user.username())
));
// 依赖 username datacenter 查询
final String dataCenter = getDataCenterFromUsername(user.username());
if (dataCenter == null) { userInformation.append("\ndatacenter : <u>null</u>"); }
else { userInformation.append(String.format("\ndatacenter : <code>%s</code>", escapeHtml(dataCenter))); }
}
if (user.firstName() == null) {
userInformation.append("\nfirstname : <u>null</u>");