Add country support for Apple app commands

Refactored Apple app-related commands and data structures to include country information, allowing users to specify a country when adding or querying Apple apps. Updated method signatures, command help texts, and output formatting to reflect this change. This improves accuracy for apps that require country-specific data.
This commit is contained in:
2025-11-07 14:21:04 +07:00
parent 7857f29a58
commit 59b03b6f9a
8 changed files with 31 additions and 40 deletions
@@ -43,13 +43,13 @@ public class AppStoreScraper {
}
}
public static AppleAppResponse getResponse(String appId) {
public static AppleAppResponse getResponse(String appId, String country) {
boolean isInCache = AppleAppRepository.INSTANCE.exist(appId);
if (isInCache) {
var app = AppleAppRepository.INSTANCE.load(appId);
return app.getApp();
} else {
var response = app(new AppleAppRequest(appId));
var response = app(new AppleAppRequest(appId, country));
AppleAppRepository.INSTANCE.init(appId);
var app = AppleAppRepository.INSTANCE.load(appId);
app.setApp(response);
@@ -58,8 +58,8 @@ public class AppStoreScraper {
}
}
public static LocalDate getAppUpdated(String appId) {
var response = getResponse(appId);
public static LocalDate getAppUpdated(String appId, String country) {
var response = getResponse(appId, country);
if (response == null) {
log.error("response is null");
return LocalDate.ofInstant(Instant.ofEpochMilli(0), ZoneId.systemDefault());
@@ -67,8 +67,8 @@ public class AppStoreScraper {
return LocalDate.ofInstant(Instant.parse(response.updated()), ZoneId.systemDefault());
}
public static double getAppScore(String appId) {
var response = getResponse(appId);
public static double getAppScore(String appId, String country) {
var response = getResponse(appId, country);
if (response == null) {
log.error("response is null");
return 0.0;
@@ -76,8 +76,8 @@ public class AppStoreScraper {
return response.score();
}
public static long getAppReviews(String appId) {
var response = getResponse(appId);
public static long getAppReviews(String appId, String country) {
var response = getResponse(appId, country);
if (response == null) {
log.error("response is null");
return 0L;
@@ -85,8 +85,8 @@ public class AppStoreScraper {
return response.reviews();
}
public static long getAppRatings(String appId) {
var response = getResponse(appId);
public static long getAppRatings(String appId, String country) {
var response = getResponse(appId, country);
if (response == null) {
log.error("response is null");
return 0L;
@@ -1,24 +1,9 @@
package com.miti99.storescraperbot.api.apple.request;
public record AppleAppRequest(
Long id,
String appId,
String country, // Tạm thời chưa cần phân biệt
Boolean ratings) {
public AppleAppRequest(String appId) {
this(
null,
appId,
"vn",
true);
}
public record AppleAppRequest(Long id, String appId, String country, Boolean ratings) {
public AppleAppRequest(Long id) {
this(
id,
null,
"vn",
true);
public AppleAppRequest(Long id, String country) {
this(id, null, country, true);
}
public AppleAppRequest(String appId, String country) {
@@ -19,7 +19,7 @@ public class AddAppleAppCommand extends BaseStoreScraperBotCommand {
AddAppleAppCommand() {
super(
"addapple",
"<id/appId>. Thêm Apple app vào danh sách theo dõi của nhóm. id: <i>iTunes 'trackId'</i>, appId: <i>iTunes 'bundleId'</i>");
"<id/appId> [country]. Thêm Apple app vào danh sách theo dõi của nhóm. id: <i>iTunes 'trackId'</i>, appId: <i>iTunes 'bundleId'</i>. Một số app cần country để hoạt động đúng, country mặc định là 'vn'");
}
@Override
@@ -39,6 +39,7 @@ public class AddAppleAppCommand extends BaseStoreScraperBotCommand {
var appId = arguments[0];
long id = -1;
var country = arguments.length == 2 ? arguments[1] : "vn";
AppleAppResponse response = null;
try {
try {
@@ -47,9 +48,9 @@ public class AddAppleAppCommand extends BaseStoreScraperBotCommand {
// Input không phải id, bỏ qua
}
if (id != -1) {
response = AppStoreScraper.app(new AppleAppRequest(id));
response = AppStoreScraper.app(new AppleAppRequest(id, country));
} else {
response = AppStoreScraper.app(new AppleAppRequest(appId));
response = AppStoreScraper.app(new AppleAppRequest(appId, country));
}
} catch (Exception e) {
log.error("request app error for appId: '{}', id: '{}'", appId, id, e);
@@ -69,9 +70,11 @@ public class AddAppleAppCommand extends BaseStoreScraperBotCommand {
return;
}
group.getAppleApps().add(new AppleAppInfo(appId));
group.getAppleApps().add(new AppleAppInfo(appId, country));
GroupRepository.INSTANCE.save(groupId, group);
StoreScrapeBotTelegramClient.INSTANCE.sendMessage(
chat.getId(), "Apple app <code>%s</code> added successfully".formatted(appId));
chat.getId(),
"Apple app <code>%s</code>, country <b>%s</b> added successfully"
.formatted(appId, country));
}
}
@@ -61,6 +61,8 @@ public class AddGoogleAppCommand extends BaseStoreScraperBotCommand {
group.getGoogleApps().add(new GoogleAppInfo(appId, country));
GroupRepository.INSTANCE.save(groupId, group);
StoreScrapeBotTelegramClient.INSTANCE.sendMessage(
chat.getId(), "Google app <code>%s</code> added successfully".formatted(appId));
chat.getId(),
"Google app <code>%s</code>, country <b>%s</b> added successfully"
.formatted(appId, country));
}
}
@@ -46,7 +46,7 @@ public class CheckAppCommand extends BaseStoreScraperBotCommand {
sb.append("\n");
for (var app : group.getAppleApps()) {
var appId = app.appId();
var updated = AppStoreScraper.getAppUpdated(appId);
var updated = AppStoreScraper.getAppUpdated(appId, app.country());
long days = ChronoUnit.DAYS.between(updated, now);
boolean passed = days <= Constant.NUM_DAYS_WARNING_NOT_UPDATED;
sb.append(
@@ -42,8 +42,9 @@ public class CheckAppScoreCommand extends BaseStoreScraperBotCommand {
sb.append("\n");
for (var app : group.getAppleApps()) {
var appId = app.appId();
double score = AppStoreScraper.getAppScore(appId);
long ratings = AppStoreScraper.getAppRatings(appId);
var country = app.country();
double score = AppStoreScraper.getAppScore(appId, country);
long ratings = AppStoreScraper.getAppRatings(appId, country);
sb.append("%-20s | %-10s | %-10s\n".formatted(appId, score, ratings));
}
sb.append("</code>\n");
@@ -35,13 +35,13 @@ public class ListAppCommand extends BaseStoreScraperBotCommand {
var sb = new StringBuilder();
sb.append("<b>Apple Apps:</b>\n");
sb.append("<code>\n");
sb.append("%-2s | %-20s\n".formatted("#", "AppId"));
sb.append("%-2s | %-20s | %-7s\n".formatted("#", "AppId", "Country"));
sb.append("-".repeat(25));
sb.append("\n");
int i = 0;
for (var app : group.getAppleApps()) {
i++;
sb.append("%-2d | %-20s\n".formatted(i,app.appId()));
sb.append("%-2s | %-20s | %-7s\n".formatted(i, app.appId(), app.country()));
}
sb.append("</code>\n");
sb.append("\n");
@@ -1,3 +1,3 @@
package com.miti99.storescraperbot.model.entity;
public record AppleAppInfo(String appId) {}
public record AppleAppInfo(String appId, String country) {}