[Fix] PixivDownload 修复因排行榜总量的不确定导致排行榜获取异常的问题;

[Change] BotEventHandler 调整事件处理完成后的日志输出形式;
This commit is contained in:
LamGC 2020-04-30 19:08:34 +08:00
parent a28cb142b4
commit 15af939c3f
2 changed files with 13 additions and 5 deletions

View File

@ -188,7 +188,7 @@ public class BotEventHandler implements EventHandler {
log.error("执行命令时发生异常", e); log.error("执行命令时发生异常", e);
result = "命令执行时发生错误,无法完成!"; result = "命令执行时发生错误,无法完成!";
} }
log.info("命令处理完成.(耗时: {}ms)", System.currentTimeMillis() - time); long processTime = System.currentTimeMillis() - time;
if(Objects.requireNonNull(result) instanceof String) { if(Objects.requireNonNull(result) instanceof String) {
try { try {
event.sendMessage((String) result); event.sendMessage((String) result);
@ -196,7 +196,10 @@ public class BotEventHandler implements EventHandler {
log.error("发送消息时发生异常", e); log.error("发送消息时发生异常", e);
} }
} }
log.info("命令反馈完成.(耗时: {}ms)", System.currentTimeMillis() - time); long totalTime = System.currentTimeMillis() - time;
log.info("命令反馈完成.(事件耗时: {}ms, P: {}%({}ms), R: {}%({}ms))", totalTime,
String.format("%.3f", ((double) processTime / (double)totalTime) * 100F), processTime,
String.format("%.3f", ((double) (totalTime - processTime) / (double)totalTime) * 100F), totalTime - processTime);
} }
/** /**

View File

@ -285,7 +285,8 @@ public class PixivDownload {
} }
/** /**
* 获取排行榜 * 获取排行榜.
* <p>注意: 如果范围实际上没超出, 但返回排行榜不足, 会导致与实际请求的数量不符, 需要检查</p>
* @param contentType 排行榜类型 * @param contentType 排行榜类型
* @param mode 排行榜模式 * @param mode 排行榜模式
* @param time 查询时间 * @param time 查询时间
@ -316,7 +317,8 @@ public class PixivDownload {
int count = 0; int count = 0;
Gson gson = new Gson(); Gson gson = new Gson();
ArrayList<JsonObject> results = new ArrayList<>(range); ArrayList<JsonObject> results = new ArrayList<>(range);
for (int pageIndex = startPages; pageIndex <= endPages && count < range; pageIndex++) { boolean canNext = true;
for (int pageIndex = startPages; canNext && pageIndex <= endPages && count < range; pageIndex++) {
HttpGet request = createHttpGetRequest(PixivURL.getRankingLink(contentType, mode, time, pageIndex, true)); HttpGet request = createHttpGetRequest(PixivURL.getRankingLink(contentType, mode, time, pageIndex, true));
log.debug("RequestUri: {}", request.getURI()); log.debug("RequestUri: {}", request.getURI());
HttpResponse response = httpClient.execute(request); HttpResponse response = httpClient.execute(request);
@ -326,10 +328,13 @@ public class PixivDownload {
throw new IOException("Http Response Error: '" + response.getStatusLine() + "', ResponseBody: '" + responseBody + '\''); throw new IOException("Http Response Error: '" + response.getStatusLine() + "', ResponseBody: '" + responseBody + '\'');
} }
JsonArray resultArray = gson.fromJson(responseBody, JsonObject.class).getAsJsonArray("contents"); JsonObject resultObject = gson.fromJson(responseBody, JsonObject.class);
canNext = resultObject.get("next").getAsJsonPrimitive().isNumber();
JsonArray resultArray = resultObject.getAsJsonArray("contents");
for (int resultIndex = startIndex; resultIndex < resultArray.size() && count < range; resultIndex++, count++) { for (int resultIndex = startIndex; resultIndex < resultArray.size() && count < range; resultIndex++, count++) {
results.add(resultArray.get(resultIndex).getAsJsonObject()); results.add(resultArray.get(resultIndex).getAsJsonObject());
} }
// 重置索引 // 重置索引
startIndex = 0; startIndex = 0;
} }