From 59b03b6f9aa49fecd1ea941bf4cc44f8dc71e4d5 Mon Sep 17 00:00:00 2001 From: tiennm99 Date: Fri, 7 Nov 2025 14:21:04 +0700 Subject: [PATCH] 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. --- .../api/apple/AppStoreScraper.java | 20 +++++++++--------- .../api/apple/request/AppleAppRequest.java | 21 +++---------------- .../bot/command/AddAppleAppCommand.java | 13 +++++++----- .../bot/command/AddGoogleAppCommand.java | 4 +++- .../bot/command/CheckAppCommand.java | 2 +- .../bot/command/CheckAppScoreCommand.java | 5 +++-- .../bot/command/ListAppCommand.java | 4 ++-- .../model/entity/AppleAppInfo.java | 2 +- 8 files changed, 31 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/miti99/storescraperbot/api/apple/AppStoreScraper.java b/src/main/java/com/miti99/storescraperbot/api/apple/AppStoreScraper.java index 02701ea..818b80e 100644 --- a/src/main/java/com/miti99/storescraperbot/api/apple/AppStoreScraper.java +++ b/src/main/java/com/miti99/storescraperbot/api/apple/AppStoreScraper.java @@ -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; diff --git a/src/main/java/com/miti99/storescraperbot/api/apple/request/AppleAppRequest.java b/src/main/java/com/miti99/storescraperbot/api/apple/request/AppleAppRequest.java index 46cfbeb..19f5f22 100644 --- a/src/main/java/com/miti99/storescraperbot/api/apple/request/AppleAppRequest.java +++ b/src/main/java/com/miti99/storescraperbot/api/apple/request/AppleAppRequest.java @@ -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) { diff --git a/src/main/java/com/miti99/storescraperbot/bot/command/AddAppleAppCommand.java b/src/main/java/com/miti99/storescraperbot/bot/command/AddAppleAppCommand.java index be2d628..9024fcf 100644 --- a/src/main/java/com/miti99/storescraperbot/bot/command/AddAppleAppCommand.java +++ b/src/main/java/com/miti99/storescraperbot/bot/command/AddAppleAppCommand.java @@ -19,7 +19,7 @@ public class AddAppleAppCommand extends BaseStoreScraperBotCommand { AddAppleAppCommand() { super( "addapple", - ". Thêm Apple app vào danh sách theo dõi của nhóm. id: iTunes 'trackId', appId: iTunes 'bundleId'"); + " [country]. Thêm Apple app vào danh sách theo dõi của nhóm. id: iTunes 'trackId', appId: iTunes 'bundleId'. 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 %s added successfully".formatted(appId)); + chat.getId(), + "Apple app %s, country %s added successfully" + .formatted(appId, country)); } } diff --git a/src/main/java/com/miti99/storescraperbot/bot/command/AddGoogleAppCommand.java b/src/main/java/com/miti99/storescraperbot/bot/command/AddGoogleAppCommand.java index 471cbad..cffd917 100644 --- a/src/main/java/com/miti99/storescraperbot/bot/command/AddGoogleAppCommand.java +++ b/src/main/java/com/miti99/storescraperbot/bot/command/AddGoogleAppCommand.java @@ -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 %s added successfully".formatted(appId)); + chat.getId(), + "Google app %s, country %s added successfully" + .formatted(appId, country)); } } diff --git a/src/main/java/com/miti99/storescraperbot/bot/command/CheckAppCommand.java b/src/main/java/com/miti99/storescraperbot/bot/command/CheckAppCommand.java index 12344ab..fa173a5 100644 --- a/src/main/java/com/miti99/storescraperbot/bot/command/CheckAppCommand.java +++ b/src/main/java/com/miti99/storescraperbot/bot/command/CheckAppCommand.java @@ -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( diff --git a/src/main/java/com/miti99/storescraperbot/bot/command/CheckAppScoreCommand.java b/src/main/java/com/miti99/storescraperbot/bot/command/CheckAppScoreCommand.java index 2db6646..0073d3e 100644 --- a/src/main/java/com/miti99/storescraperbot/bot/command/CheckAppScoreCommand.java +++ b/src/main/java/com/miti99/storescraperbot/bot/command/CheckAppScoreCommand.java @@ -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("\n"); diff --git a/src/main/java/com/miti99/storescraperbot/bot/command/ListAppCommand.java b/src/main/java/com/miti99/storescraperbot/bot/command/ListAppCommand.java index 0654fbc..bb0a54e 100644 --- a/src/main/java/com/miti99/storescraperbot/bot/command/ListAppCommand.java +++ b/src/main/java/com/miti99/storescraperbot/bot/command/ListAppCommand.java @@ -35,13 +35,13 @@ public class ListAppCommand extends BaseStoreScraperBotCommand { var sb = new StringBuilder(); sb.append("Apple Apps:\n"); sb.append("\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("\n"); sb.append("\n"); diff --git a/src/main/java/com/miti99/storescraperbot/model/entity/AppleAppInfo.java b/src/main/java/com/miti99/storescraperbot/model/entity/AppleAppInfo.java index 454fecb..5c10556 100644 --- a/src/main/java/com/miti99/storescraperbot/model/entity/AppleAppInfo.java +++ b/src/main/java/com/miti99/storescraperbot/model/entity/AppleAppInfo.java @@ -1,3 +1,3 @@ package com.miti99.storescraperbot.model.entity; -public record AppleAppInfo(String appId) {} +public record AppleAppInfo(String appId, String country) {}