[Add] PixivURL 为RankingContentType增加isSupportedMode以检查指定的RankingMode是否支持;

[Update] 对CQProcess, PixivDownload, RankingUpdateTimer添加对RankingContentType.isSupportedMode的支持以消除无效请求带来的资源浪费;
This commit is contained in:
LamGC 2020-04-01 09:50:45 +08:00
parent 3e4874b67c
commit ae3f52532c
4 changed files with 86 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package net.lamgc.cgj; package net.lamgc.cgj;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.base.Throwables;
import com.google.common.reflect.TypeToken; import com.google.common.reflect.TypeToken;
import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.google.gson.*; import com.google.gson.*;
@ -112,7 +113,8 @@ public class CQProcess {
@Command @Command
public static String ranking( public static String ranking(
@Argument(force = false, name = "date") Date queryTime, @Argument(force = false, name = "date") Date queryTime,
@Argument(force = false, name = "contentMode", defaultValue = "DAILY") String contentMode @Argument(force = false, name = "mode", defaultValue = "DAILY") String contentMode,
@Argument(force = false, name = "type") String contentType
) { ) {
Date queryDate = queryTime; Date queryDate = queryTime;
if (queryDate == null) { if (queryDate == null) {
@ -135,6 +137,19 @@ public class CQProcess {
log.warn("无效的RankingMode值: {}", contentMode); log.warn("无效的RankingMode值: {}", contentMode);
} }
PixivURL.RankingContentType type = PixivURL.RankingContentType.TYPE_ILLUST;
try {
type = PixivURL.RankingContentType.valueOf("TYPE_" + contentMode.toUpperCase());
} catch (IllegalArgumentException e) {
log.warn("无效的RankingContentType值: {}", contentMode);
}
if(!type.isSupportedMode(mode)) {
log.warn("RankingContentType不支持指定的RankingMode.(ContentType: {}, RankingMode: {})",
type.name(), mode.name());
return "不支持的内容类型或模式!";
}
StringBuilder resultBuilder = new StringBuilder(mode.name() + " - 以下是 ").append(new SimpleDateFormat("yyyy-MM-dd").format(queryDate)).append(" 的Pixiv插画排名榜前十名\n"); StringBuilder resultBuilder = new StringBuilder(mode.name() + " - 以下是 ").append(new SimpleDateFormat("yyyy-MM-dd").format(queryDate)).append(" 的Pixiv插画排名榜前十名\n");
try { try {
int index = 0; int index = 0;
@ -154,7 +169,7 @@ public class CQProcess {
log.warn("配置项 {} 的参数值格式有误!", imageLimitPropertyKey); log.warn("配置项 {} 的参数值格式有误!", imageLimitPropertyKey);
} }
for (JsonObject rankInfo : getRankingInfoByCache(PixivURL.RankingContentType.TYPE_ILLUST, mode, queryDate, 0, itemLimit)) { for (JsonObject rankInfo : getRankingInfoByCache(type, mode, queryDate, 1, itemLimit)) {
index++; index++;
int rank = rankInfo.get("rank").getAsInt(); int rank = rankInfo.get("rank").getAsInt();
int illustId = rankInfo.get("illust_id").getAsInt(); int illustId = rankInfo.get("illust_id").getAsInt();
@ -561,9 +576,21 @@ public class CQProcess {
* @throws IOException 获取异常时抛出 * @throws IOException 获取异常时抛出
*/ */
public static List<JsonObject> getRankingInfoByCache(PixivURL.RankingContentType contentType, PixivURL.RankingMode mode, Date queryDate, int start, int range) throws IOException { public static List<JsonObject> getRankingInfoByCache(PixivURL.RankingContentType contentType, PixivURL.RankingMode mode, Date queryDate, int start, int range) throws IOException {
if(!contentType.isSupportedMode(mode)) {
log.warn("试图获取不支持的排行榜类型已拒绝.(ContentType: {}, RankingMode: {})", contentType.name(), mode.name());
if(log.isDebugEnabled()) {
try {
Thread.dumpStack();
} catch(Exception e) {
log.debug("本次非法请求的堆栈信息如下: \n{}", Throwables.getStackTraceAsString(e));
}
}
return new ArrayList<>(0);
}
String date = new SimpleDateFormat("yyyyMMdd").format(queryDate); String date = new SimpleDateFormat("yyyyMMdd").format(queryDate);
//int requestSign = ("Ranking." + contentType.name() + "." + mode.name() + "." + date).hashCode(); //int requestSign = ("Ranking." + contentType.name() + "." + mode.name() + "." + date).hashCode();
String requestSign = buildSyncKey("Ranking.", contentType.name(), ".", mode.name(), ".", date); String requestSign = buildSyncKey(contentType.name(), ".", mode.name(), ".", date);
if(!rankingCache.exists(requestSign)) { if(!rankingCache.exists(requestSign)) {
synchronized(requestSign) { synchronized(requestSign) {
if(!rankingCache.exists(requestSign)) { if(!rankingCache.exists(requestSign)) {

View File

@ -52,6 +52,9 @@ public class RankingUpdateTimer {
log.info("正在获取 {} 期排行榜数据...", calendar.getTime()); log.info("正在获取 {} 期排行榜数据...", calendar.getTime());
for (PixivURL.RankingMode rankingMode : PixivURL.RankingMode.values()) { for (PixivURL.RankingMode rankingMode : PixivURL.RankingMode.values()) {
for (PixivURL.RankingContentType contentType : PixivURL.RankingContentType.values()) { for (PixivURL.RankingContentType contentType : PixivURL.RankingContentType.values()) {
if(!contentType.isSupportedMode(rankingMode)) {
log.debug("不支持的类型, 填空值跳过...(类型: {}.{})", rankingMode.name(), contentType.name());
}
log.info("当前排行榜类型: {}.{}, 正在更新...", rankingMode.name(), contentType.name()); log.info("当前排行榜类型: {}.{}, 正在更新...", rankingMode.name(), contentType.name());
try { try {
CQProcess.getRankingInfoByCache(contentType, rankingMode, calendar.getTime(), 1, 0); CQProcess.getRankingInfoByCache(contentType, rankingMode, calendar.getTime(), 1, 0);

View File

@ -293,6 +293,10 @@ public class PixivDownload {
throw new IllegalArgumentException("range cannot be less than or equal to zero"); throw new IllegalArgumentException("range cannot be less than or equal to zero");
} }
if(!contentType.isSupportedMode(mode)) {
throw new IllegalArgumentException("ContentType不支持指定的RankingMode: ContentType: " + contentType.name() + ", Mode: " + mode.name());
}
int startPage = (int) Math.ceil(rankStart / 50F); int startPage = (int) Math.ceil(rankStart / 50F);
int requestFrequency = (int) Math.ceil((rankStart + (range - 1)) / 50F); int requestFrequency = (int) Math.ceil((rankStart + (range - 1)) / 50F);
int surplusQuantity = range; int surplusQuantity = range;

View File

@ -1,6 +1,7 @@
package net.lamgc.cgj.pixiv; package net.lamgc.cgj.pixiv;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
@ -175,7 +176,7 @@ public class PixivURL {
public static String getRankingLink(RankingContentType contentType, RankingMode mode, Date time, int pageIndex, boolean json){ public static String getRankingLink(RankingContentType contentType, RankingMode mode, Date time, int pageIndex, boolean json){
StringBuilder linkBuilder = new StringBuilder(PIXIV_RANKING_LINK); StringBuilder linkBuilder = new StringBuilder(PIXIV_RANKING_LINK);
linkBuilder.append("mode=").append(mode == null ? RankingMode.MODE_DAILY.modeParam : mode.modeParam); linkBuilder.append("mode=").append(mode == null ? RankingMode.MODE_DAILY.modeParam : mode.modeParam);
if(contentType != null){ if(contentType != null && !contentType.equals(RankingContentType.ALL)){
linkBuilder.append("&content=").append(contentType.typeName); linkBuilder.append("&content=").append(contentType.typeName);
} }
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
@ -263,28 +264,71 @@ public class PixivURL {
* 排名榜类型 * 排名榜类型
*/ */
public enum RankingContentType{ public enum RankingContentType{
ALL("", RankingMode.values()),
/** /**
* 插画 * 插画
* 支持的时间类型: 每天, 每周, 每月, 新人 * 支持的时间类型: 每天, 每周, 每月, 新人
*/ */
TYPE_ILLUST("illust"), TYPE_ILLUST("illust",
new RankingMode[]{
RankingMode.MODE_DAILY,
RankingMode.MODE_MONTHLY,
RankingMode.MODE_WEEKLY,
RankingMode.MODE_ROOKIE,
RankingMode.MODE_DAILY_R18,
RankingMode.MODE_WEEKLY_R18,
RankingMode.MODE_MALE_R18,
RankingMode.MODE_FEMALE_R18
}
),
/** /**
* 动图 * 动图
* 支持的时间类型:每天, 每周 * 支持的时间类型:每天, 每周
*/ */
TYPE_UGOIRA("ugoira"), TYPE_UGOIRA("ugoira",
new RankingMode[]{
RankingMode.MODE_DAILY,
RankingMode.MODE_WEEKLY,
RankingMode.MODE_DAILY_R18,
RankingMode.MODE_WEEKLY_R18
}
),
/** /**
* 漫画 * 漫画
* 支持的时间类型: 每天, 每周, 每月, 新人 * 支持的时间类型: 每天, 每周, 每月, 新人
*/ */
TYPE_MANGA("manga") TYPE_MANGA("manga",
new RankingMode[]{
RankingMode.MODE_DAILY,
RankingMode.MODE_MONTHLY,
RankingMode.MODE_WEEKLY,
RankingMode.MODE_ROOKIE,
RankingMode.MODE_DAILY_R18,
RankingMode.MODE_WEEKLY_R18,
RankingMode.MODE_MALE_R18,
RankingMode.MODE_FEMALE_R18
}
)
; ;
String typeName; String typeName;
RankingContentType(String typeName){ private final RankingMode[] supportedMode;
RankingContentType(String typeName, RankingMode[] supportedMode){
this.typeName = typeName; this.typeName = typeName;
this.supportedMode = supportedMode;
} }
/**
* 检查指定RankingMode是否支持
* @param mode 要检查的RankingMode项
* @return 如果支持返回true
*/
public boolean isSupportedMode(RankingMode mode) {
return Arrays.binarySearch(supportedMode, mode) != -1;
}
} }
} }