[Add] 为Ranking命令增加翻页功能;

[Change] BotCommandProcess 为`ranking`方法增加`pageIndex`参数, 增加对起始排名的计算, 移除起始排名的固定值, 改按pageIndex计算;
[Fix] CacheStoreCentral 修复`getRankingInfoByCache`方法中潜在的排行榜返回错误的问题;
[Delete] PixivDownload 移除无用的两个方法;
[Change] PixivDownload 调整`@SuppressWarnings`注解的范围;
This commit is contained in:
LamGC 2020-07-15 16:17:52 +08:00
parent bf8de1ac1e
commit 1a3f99685b
Signed by: LamGC
GPG Key ID: 6C5AE2A913941E1D
3 changed files with 11 additions and 42 deletions

View File

@ -143,8 +143,13 @@ public class BotCommandProcess {
@Argument(force = false, name = "date") Date queryTime, @Argument(force = false, name = "date") Date queryTime,
@Argument(force = false, name = "force") boolean force, @Argument(force = false, name = "force") boolean force,
@Argument(force = false, name = "mode", defaultValue = "DAILY") String contentMode, @Argument(force = false, name = "mode", defaultValue = "DAILY") String contentMode,
@Argument(force = false, name = "type", defaultValue = "ALL") String contentType @Argument(force = false, name = "type", defaultValue = "ALL") String contentType,
@Argument(force = false, name = "p", defaultValue = "1") int pageIndex
) throws InterruptedException { ) throws InterruptedException {
if(pageIndex <= 0) {
return "色图姬找不到指定页数的排行榜!";
}
Date queryDate = queryTime; Date queryDate = queryTime;
if (queryDate == null) { if (queryDate == null) {
queryDate = new Date(); queryDate = new Date();
@ -212,8 +217,10 @@ public class BotCommandProcess {
log.warn("配置项 {} 的参数值格式有误!", imageLimitPropertyKey); log.warn("配置项 {} 的参数值格式有误!", imageLimitPropertyKey);
} }
int startsIndex = itemLimit * pageIndex - (itemLimit - 1);
List<JsonObject> rankingInfoList = CacheStoreCentral.getCentral() List<JsonObject> rankingInfoList = CacheStoreCentral.getCentral()
.getRankingInfoByCache(type, mode, queryDate, 1, Math.max(0, itemLimit), false); .getRankingInfoByCache(type, mode, queryDate,
Math.max(1, startsIndex), Math.max(0, itemLimit), false);
if(rankingInfoList.isEmpty()) { if(rankingInfoList.isEmpty()) {
return "无法查询排行榜,可能排行榜尚未更新。"; return "无法查询排行榜,可能排行榜尚未更新。";
} }

View File

@ -402,7 +402,7 @@ public final class CacheStoreCentral {
result = rankingCache.getCache(requestSign, start - 1, range); result = rankingCache.getCache(requestSign, start - 1, range);
log.trace("RequestSign [{}] 缓存命中.", requestSign); log.trace("RequestSign [{}] 缓存命中.", requestSign);
} }
return PixivDownload.getRanking(result, start - 1, range); return result;
} }
/** /**

View File

@ -4,7 +4,6 @@ import com.google.gson.Gson;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import io.netty.handler.codec.http.HttpHeaderNames; import io.netty.handler.codec.http.HttpHeaderNames;
import net.lamgc.cgj.exception.HttpRequestException; import net.lamgc.cgj.exception.HttpRequestException;
import org.apache.http.Header; import org.apache.http.Header;
@ -32,7 +31,6 @@ import java.util.*;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
@SuppressWarnings("ALL")
public class PixivDownload { public class PixivDownload {
private final static Logger log = LoggerFactory.getLogger(PixivDownload.class); private final static Logger log = LoggerFactory.getLogger(PixivDownload.class);
@ -249,43 +247,6 @@ public class PixivDownload {
}); });
} }
/**
* 从JsonArray获取数据
* @param rankingList JsonArray对象
* @param rankStart 开始索引, 从0开始
* @param range 范围
* @return 返回List对象
*/
public static List<JsonObject> getRanking(List<JsonObject> rankingList, int rankStart, int range) {
log.debug("正在读取JsonArray...(rankStart: {}, range: {})", rankStart, range);
ArrayList<JsonObject> results = new ArrayList<>(rankingList.size());
for (int rankIndex = rankStart; rankIndex < rankStart + range; rankIndex++) {
JsonElement jsonElement = rankingList.get(rankIndex - rankStart);
JsonObject rankInfo = jsonElement.getAsJsonObject();
int rank = rankInfo.get("rank").getAsInt();
int illustId = rankInfo.get("illust_id").getAsInt();
int authorId = rankInfo.get("user_id").getAsInt();
String authorName = rankInfo.get("user_name").getAsString();
String title = rankInfo.get("title").getAsString();
log.trace("Array-当前到第 {}/{} 名(总共 {} 名), IllustID: {}, Author: ({}) {}, Title: {}", rank, rankStart + range, range, illustId, authorId, authorName, title);
results.add(rankInfo);
}
log.debug("JsonArray读取完成.");
return results;
}
/**
* 从JsonArray获取数据
* @param rankingArray JsonArray对象
* @param rankStart 开始索引, 从0开始
* @param range 范围
* @return 返回List对象
*/
public static List<JsonObject> getRanking(JsonArray rankingArray, int rankStart, int range) {
List<JsonObject> list = new Gson().fromJson(rankingArray, new TypeToken<List<JsonObject>>(){}.getType());
return getRanking(list, rankStart, range);
}
/** /**
* 获取排行榜. * 获取排行榜.
* <p>注意: 如果范围实际上没超出, 但返回排行榜不足, 会导致与实际请求的数量不符, 需要检查</p> * <p>注意: 如果范围实际上没超出, 但返回排行榜不足, 会导致与实际请求的数量不符, 需要检查</p>
@ -421,6 +382,7 @@ public class PixivDownload {
/** /**
* 插图质量 * 插图质量
*/ */
@SuppressWarnings("unused")
public enum PageQuality{ public enum PageQuality{
/** /**
* 原图画质 * 原图画质