Closing #23 - 为部分命令【增加/优化】翻页功能 (#27)

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

[Change] BotCommandProcess 为`ranking`方法增加`pageIndex`参数, 增加对起始排名的计算, 移除起始排名的固定值, 改按pageIndex计算;
[Fix] CacheStoreCentral 修复`getRankingInfoByCache`方法中潜在的排行榜返回错误的问题;
[Delete] PixivDownload 移除无用的两个方法;
[Change] PixivDownload 调整`@SuppressWarnings`注解的范围;

* [Change] 调整搜索接口返回数据中Attribute的Enum存放位置;

[Change] PixivSearchLinkBuilder 调整SuppressWarnings注解;
[Deprecated] PixivSearchLinkBuilder.SearchArea.jsonKey 将属性标记deprecated;
[Add] PixivSearchAttribute 增加搜索结果属性枚举类, 用于代替`PixivSearchLinkBuilder.SearchArea.jsonKey`;

* [Add] 增加PixivSearchLinkBuilder中内部属性的getter, 增加Pixiv工具类;

[Update] PixivSearchAttribute 补充Javadoc;
[Add] PixivSearchLinkBuilder 增加大部分属性的getter方法;
[Add] PixivUtils 增加Pixiv工具类, 并添加通过字符串参数构造PixivSearchLinkBuilder的方法;

* [Fix] 修复搜索命令中area参数区分大小写的问题;

[Fix] PixivUtils 修复area参数解析时区分大小写的问题;

* [Fix] 修复搜索参数Area不在缓存标识范围内的问题;

[Fix] CacheStoreCentral 调整搜索功能的缓存Key形式, 移除Url不变部分, 增加Area部分到缓存标识中;

* [Add] 为搜索命令优化翻页功能;

[Change] BotCommandProcess 重制search命令, 增加用于获取范围相关搜索结果页面的`getSearchResult`方法, 调整消息格式;

* [Update] 优化搜索命令中的去重方法;

[Update] BotCommandProcess 调整搜索命令, 使用HashSet对artworkList进行去重;

* [Delete] 移除无用的`getSearchBody(String, ...)`方法;

[Delete] CacheStoreCentral 移除无用的方法;
This commit is contained in:
LamGC 2020-07-29 03:21:38 +08:00 committed by GitHub
parent cc4eeac728
commit 7bd9e1c410
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 289 additions and 199 deletions

View File

@ -13,8 +13,10 @@ import net.lamgc.cgj.bot.sort.PreLoadDataAttribute;
import net.lamgc.cgj.bot.sort.PreLoadDataAttributeComparator;
import net.lamgc.cgj.pixiv.PixivDownload;
import net.lamgc.cgj.pixiv.PixivDownload.PageQuality;
import net.lamgc.cgj.pixiv.PixivSearchAttribute;
import net.lamgc.cgj.pixiv.PixivSearchLinkBuilder;
import net.lamgc.cgj.pixiv.PixivURL;
import net.lamgc.cgj.util.PixivUtils;
import net.lamgc.utils.base.runner.Argument;
import net.lamgc.utils.base.runner.Command;
import org.slf4j.Logger;
@ -40,6 +42,8 @@ public class BotCommandProcess {
private final static RankingUpdateTimer updateTimer = new RankingUpdateTimer();
private final static int SEARCH_PAGE_MAX_ITEM = 60;
public static void initialize() {
log.info("正在初始化...");
@ -143,8 +147,13 @@ public class BotCommandProcess {
@Argument(force = false, name = "date") Date queryTime,
@Argument(force = false, name = "force") boolean force,
@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 {
if(pageIndex <= 0) {
return "色图姬找不到指定页数的排行榜!";
}
Date queryDate = queryTime;
if (queryDate == null) {
queryDate = new Date();
@ -212,8 +221,10 @@ public class BotCommandProcess {
log.warn("配置项 {} 的参数值格式有误!", imageLimitPropertyKey);
}
int startsIndex = itemLimit * pageIndex - (itemLimit - 1);
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()) {
return "无法查询排行榜,可能排行榜尚未更新。";
}
@ -314,12 +325,9 @@ public class BotCommandProcess {
@Argument(name = "option", force = false) String contentOption,
@Argument(name = "p", force = false, defaultValue = "1") int pagesIndex
) throws IOException, InterruptedException {
PixivSearchLinkBuilder searchLinkBuilder = PixivUtils.buildSearchLinkBuilderFromString(content, type, area,
includeKeywords, excludeKeywords, contentOption, pagesIndex);
log.debug("正在执行搜索...");
JsonObject resultBody = CacheStoreCentral.getCentral()
.getSearchBody(content, type, area, includeKeywords, excludeKeywords, contentOption, pagesIndex);
StringBuilder result = new StringBuilder("内容 " + content + " 的搜索结果:\n");
log.debug("正在处理信息...");
int limit = 8;
try {
limit = Integer.parseInt(SettingProperties.
@ -327,95 +335,112 @@ public class BotCommandProcess {
} catch (Exception e) {
log.warn("参数转换异常!将使用默认值(" + limit + ")", e);
}
int totalCount = 0;
for (PixivSearchLinkBuilder.SearchArea searchArea : PixivSearchLinkBuilder.SearchArea.values()) {
if (!resultBody.has(searchArea.jsonKey) ||
resultBody.getAsJsonObject(searchArea.jsonKey).getAsJsonArray("data").size() == 0) {
log.debug("返回数据不包含 {}", searchArea.jsonKey);
continue;
}
JsonArray illustsArray = resultBody
.getAsJsonObject(searchArea.jsonKey).getAsJsonArray("data");
ArrayList<JsonElement> illustsList = new ArrayList<>();
illustsArray.forEach(illustsList::add);
illustsList.sort(new PreLoadDataAttributeComparator(PreLoadDataAttribute.BOOKMARK));
log.debug("已找到与 {} 相关插图信息({})", content, searchArea.name().toLowerCase());
int count = 1;
for (JsonElement jsonElement : illustsList) {
if (count > limit) {
break;
}
JsonObject illustObj = jsonElement.getAsJsonObject();
if (!illustObj.has("illustId")) {
continue;
}
int illustId = illustObj.get("illustId").getAsInt();
StringBuilder builder = new StringBuilder("[");
illustObj.get("tags").getAsJsonArray().forEach(el -> builder.append(el.getAsString()).append(", "));
builder.replace(builder.length() - 2, builder.length(), "]");
log.debug("{} ({} / {})\n\t作品id: {}, \n\t作者名(作者id): {} ({}), \n\t" +
"作品标题: {}, \n\t作品Tags: {}, \n\t页数: {}页, \n\t作品链接: {}",
searchArea.name(),
count,
illustsList.size(),
illustId,
illustObj.get("userName").getAsString(),
illustObj.get("userId").getAsInt(),
illustObj.get("illustTitle").getAsString(),
builder,
illustObj.get("pageCount").getAsInt(),
PixivURL.getPixivRefererLink(illustId)
);
String imageMsg;
try {
imageMsg = CacheStoreCentral.getCentral()
.getImageById(fromGroup, illustId, PixivDownload.PageQuality.REGULAR, 1);
} catch (NoSuchElementException e) {
if(e.getMessage().startsWith("No work found: ")) {
log.warn("作品 {} 不存在, 跳过该作品...", illustId);
continue;
}
throw e;
}
if (isNoSafe(illustId, SettingProperties.getProperties(fromGroup), false)) {
log.warn("作品Id {} 为R-18作品, 跳过.", illustId);
continue;
} else if(isReported(illustId)) {
log.warn("作品Id {} 被报告, 正在等待审核, 跳过该作品.", illustId);
continue;
}
JsonObject illustPreLoadData = CacheStoreCentral.getCentral()
StringBuilder result = new StringBuilder("内容 " + content + " 的搜索结果:\n");
List<JsonObject> artworkList = getSearchResult(searchLinkBuilder, fromGroup, limit, pagesIndex);
artworkList.sort(new PreLoadDataAttributeComparator(PreLoadDataAttribute.LIKE));
int startIndex = limit * pagesIndex - limit + 1;
int pageStartIndex = startIndex % SEARCH_PAGE_MAX_ITEM;
for(int index = pageStartIndex; totalCount < limit && index < artworkList.size(); index++) {
int illustId = artworkList.get(index - 1).get("illustId").getAsInt();
// 预加载数据有更多信息可以提供
JsonObject artworkPreLoadData = CacheStoreCentral.getCentral()
.getIllustPreLoadData(illustId, false);
result.append(searchArea.name()).append(" (").append(count).append(" / ")
.append(limit).append(")\n\t作品id: ").append(illustId)
.append(", \n\t作者名: ").append(illustObj.get("userName").getAsString())
.append("\n\t作品标题: ").append(illustObj.get("illustTitle").getAsString())
.append("\n\t作品页数: ").append(illustObj.get("pageCount").getAsInt()).append("")
.append("\n\t点赞数")
.append(illustPreLoadData.get(PreLoadDataAttribute.LIKE.attrName).getAsInt())
.append("\n\t收藏数")
.append(illustPreLoadData.get(PreLoadDataAttribute.BOOKMARK.attrName).getAsInt())
.append("\n\t围观数")
.append(illustPreLoadData.get(PreLoadDataAttribute.VIEW.attrName).getAsInt())
.append("\n\t评论数")
.append(illustPreLoadData.get(PreLoadDataAttribute.COMMENT.attrName).getAsInt())
.append("\n").append(imageMsg).append("\n");
count++;
// 构造消息内容
result.append(startIndex++).append(". ").append(artworkPreLoadData.get("illustId").getAsInt()).append(" ")
.append(artworkPreLoadData.get("illustTitle").getAsString());
result.append("\n\t").append("作者:").append(artworkPreLoadData.get("userName").getAsString());
result.append("\n\t").append("作品页数:").append(artworkPreLoadData.get("pageCount").getAsInt()).append("");
result.append("\n\t").append("点赞数:")
.append(artworkPreLoadData.get(PreLoadDataAttribute.LIKE.attrName).getAsInt()).append("");
result.append("\n\t").append("收藏数:")
.append(artworkPreLoadData.get(PreLoadDataAttribute.BOOKMARK.attrName).getAsInt()).append("");
result.append("\n\t").append("围观数:")
.append(artworkPreLoadData.get(PreLoadDataAttribute.VIEW.attrName).getAsInt()).append("");
result.append("\n\t").append("评论数:")
.append(artworkPreLoadData.get(PreLoadDataAttribute.COMMENT.attrName).getAsInt()).append("");
result.append(CacheStoreCentral.getCentral()
.getImageById(fromGroup, illustId, PageQuality.REGULAR, 1)).append("\n");
totalCount++;
}
if (count > limit) {
break;
}
}
return totalCount <= 0 ?
"搜索完成,未找到相关作品。" :
Strings.nullToEmpty(result.toString()) + "预览图片并非原图,使用“.cgj image -id 作品id”获取原图\n" +
"如有不当作品,可使用\".cgj report -id 作品id\"向色图姬反馈。";
}
/**
* 获取 length 涉及的多个页的搜索结果.
* 例如:
* pageMaxItem = 60;
* length = 150;
* page = 3;
* 那么:
* endItemIndex = length * page = 450;
* startItemIndex = endItemIndex - length + 1 = 301;
* startPageIndex = ceil(startItemIndex / pageMaxItem) = 6;
* pageRange = ceil((endItemIndex - length + 1) / pageMaxItem) = 3;
* endPageIndex = startPageIndex - pageRange - 1 = 8;
* 该方法将会取搜索结果的 6 ~ 8 页结果并返回;
* @param searchLinkBuilder 已构造好除 Page 参数外其他参数的 {@link PixivSearchLinkBuilder}
* @param length 所需结果的范围
* @param page 所需结果的页数
* @return 返回包含范围的涉及页面所有搜索结果.
* @throws IOException 如获取发生异常则抛出.
*/
private static List<JsonObject> getSearchResult(PixivSearchLinkBuilder searchLinkBuilder, long groupId, int length, int page) throws IOException {
List<JsonObject> artworkList = new ArrayList<>(length);
int endsItemIndex = length * page;
int startsItemIndex = endsItemIndex - length + 1;
int startPageIndex = (int) Math.ceil(startsItemIndex / (double) SEARCH_PAGE_MAX_ITEM);
int pageRange = (int) Math.ceil((endsItemIndex - startsItemIndex) / (double) SEARCH_PAGE_MAX_ITEM);
PixivSearchAttribute areaAttribute = PixivSearchAttribute.valueOf(searchLinkBuilder.getSearchArea().toString());
Properties properties = SettingProperties.getProperties(groupId);
int expectedQuantity = pageRange * SEARCH_PAGE_MAX_ITEM;
for(int pageIndex = startPageIndex;
pageIndex <= startPageIndex + pageRange - 1 || artworkList.size() < length || artworkList.size() < expectedQuantity;
pageIndex++) {
searchLinkBuilder.setPage(pageIndex);
JsonObject searchBody = CacheStoreCentral.getCentral().getSearchBody(searchLinkBuilder);
for(String areaAttributeName : areaAttribute.attributeNames) {
JsonObject areaResult = searchBody.getAsJsonObject(areaAttributeName);
if(areaResult == null || !areaResult.has("data")) {
log.debug("作品类型属性 {} 无搜索结果, 跳过...", areaAttributeName);
continue;
}
JsonArray areaArray = areaResult.getAsJsonArray("data");
for(JsonElement element : areaArray) {
JsonObject artworkInfo = element.getAsJsonObject();
if(!artworkInfo.has("illustId")) {
log.warn("发现未含有illustId的JsonObject: '{}'", artworkInfo.toString());
continue;
}
final int illustId = artworkInfo.get("illustId").getAsInt();
if(isNoSafe(illustId, properties, false)) {
log.warn("作品 {} 为R18作品, 跳过.", illustId);
continue;
} else if(isReported(illustId)) {
log.warn("作品 {} 被报告, 跳过.", illustId);
continue;
}
artworkList.add(artworkInfo);
}
}
}
// 去重
Set<JsonObject> hashSet = new HashSet<>(artworkList.size());
hashSet.addAll(artworkList);
artworkList.clear();
artworkList.addAll(hashSet);
return artworkList;
}
/**
* 获取作品页面的下载链接
* @param illustId 作品Id

View File

@ -402,84 +402,28 @@ public final class CacheStoreCentral {
result = rankingCache.getCache(requestSign, start - 1, range);
log.trace("RequestSign [{}] 缓存命中.", requestSign);
}
return PixivDownload.getRanking(result, start - 1, range);
return result;
}
/**
* 获取搜索结果
* @param content 搜索内容
* @param type 类型
* @param area 范围
* @param includeKeywords 包含关键词
* @param excludeKeywords 排除关键词
* @param contentOption 内容类型
* @param searchBuilder 需要执行搜索的搜索链接构造器
* @return 返回完整搜索结果
* @throws IOException 当请求发生异常, 或接口返回异常信息时抛出.
*/
public JsonObject getSearchBody(
String content,
String type,
String area,
String includeKeywords,
String excludeKeywords,
String contentOption,
int pageIndex
) throws IOException {
PixivSearchLinkBuilder searchBuilder = new PixivSearchLinkBuilder(Strings.isNullOrEmpty(content) ? "" : content);
if (type != null) {
try {
searchBuilder.setSearchType(PixivSearchLinkBuilder.SearchType.valueOf(type.toUpperCase()));
} catch (IllegalArgumentException e) {
log.warn("不支持的SearchType: {}", type);
}
}
if (area != null) {
try {
searchBuilder.setSearchArea(PixivSearchLinkBuilder.SearchArea.valueOf(area));
} catch (IllegalArgumentException e) {
log.warn("不支持的SearchArea: {}", area);
}
}
if (contentOption != null) {
try {
searchBuilder.setSearchContentOption(
PixivSearchLinkBuilder.SearchContentOption.valueOf(contentOption.trim().toUpperCase()));
} catch (IllegalArgumentException e) {
log.warn("不支持的SearchContentOption: {}", contentOption);
}
}
if (!Strings.isNullOrEmpty(includeKeywords)) {
for (String keyword : includeKeywords.split(";")) {
searchBuilder.removeExcludeKeyword(keyword.trim());
searchBuilder.addIncludeKeyword(keyword.trim());
log.trace("已添加关键字: {}", keyword);
}
}
if (!Strings.isNullOrEmpty(excludeKeywords)) {
for (String keyword : excludeKeywords.split(";")) {
searchBuilder.removeIncludeKeyword(keyword.trim());
searchBuilder.addExcludeKeyword(keyword.trim());
log.trace("已添加排除关键字: {}", keyword);
}
}
if(pageIndex > 0) {
searchBuilder.setPage(pageIndex);
}
public JsonObject getSearchBody(PixivSearchLinkBuilder searchBuilder) throws IOException {
log.debug("正在搜索作品, 条件: {}", searchBuilder.getSearchCondition());
String requestUrl = searchBuilder.buildURL();
String searchIdentify = requestUrl.substring(requestUrl.lastIndexOf("/", requestUrl.lastIndexOf("/") - 1) + 1);
Locker<String> locker
= buildSyncKey(searchBuilder.buildURL());
String requestUrl = locker.getKey();
= buildSyncKey(searchIdentify);
log.debug("RequestUrl: {}", requestUrl);
JsonObject resultBody = null;
if(!searchBodyCache.exists(requestUrl)) {
if(!searchBodyCache.exists(searchIdentify)) {
try {
locker.lock();
synchronized (locker) {
if (!searchBodyCache.exists(requestUrl)) {
if (!searchBodyCache.exists(searchIdentify)) {
log.trace("searchBody缓存失效, 正在更新...");
JsonObject jsonObject;
HttpGet httpGetRequest = BotGlobal.getGlobal().getPixivDownload().
@ -504,8 +448,8 @@ public final class CacheStoreCentral {
} catch (Exception e) {
log.warn("全局配置项 \"{}\" 值非法, 已使用默认值: {}", propValue, expire);
}
resultBody = jsonObject.getAsJsonObject().getAsJsonObject("body");
searchBodyCache.update(requestUrl, jsonObject, expire);
resultBody = jsonObject;
searchBodyCache.update(searchIdentify, jsonObject, expire);
log.trace("searchBody缓存已更新(有效时间: {})", expire);
} else {
log.trace("搜索缓存命中.");
@ -519,9 +463,9 @@ public final class CacheStoreCentral {
}
if(Objects.isNull(resultBody)) {
resultBody = searchBodyCache.getCache(requestUrl).getAsJsonObject().getAsJsonObject("body");
resultBody = searchBodyCache.getCache(searchIdentify).getAsJsonObject();
}
return resultBody;
return resultBody.getAsJsonObject("body");
}
protected ImageChecksum getImageChecksum(int illustId, int pageIndex) {

View File

@ -4,7 +4,6 @@ import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import io.netty.handler.codec.http.HttpHeaderNames;
import net.lamgc.cgj.exception.HttpRequestException;
import org.apache.http.Header;
@ -32,7 +31,6 @@ import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer;
@SuppressWarnings("ALL")
public class PixivDownload {
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>
@ -421,6 +382,7 @@ public class PixivDownload {
/**
* 插图质量
*/
@SuppressWarnings("unused")
public enum PageQuality{
/**
* 原图画质

View File

@ -0,0 +1,22 @@
package net.lamgc.cgj.pixiv;
/**
* 搜索结果的属性枚举类.
* <p>按照请求的{@link PixivSearchLinkBuilder.SearchArea}获取所支持的属性数组</p>
*/
public enum PixivSearchAttribute {
ARTWORKS("illustManga"),
TOP("illustManga", "novel"),
ILLUSTRATIONS("illust"),
MANGA("manga"),
NOVELS("novel")
;
public final String[] attributeNames;
PixivSearchAttribute(String... attributeNames) {
this.attributeNames = attributeNames;
}
}

View File

@ -8,6 +8,7 @@ import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
/**
* Pixiv搜索URL构造器
@ -15,7 +16,7 @@ import java.util.Objects;
* @author LamGC
* @see PixivURL#PIXIV_SEARCH_CONTENT_URL
*/
@SuppressWarnings("unused")
@SuppressWarnings({"unused", "UnusedReturnValue"})
public class PixivSearchLinkBuilder {
private final String content;
@ -26,8 +27,8 @@ public class PixivSearchLinkBuilder {
private SearchOrder searchOrder = SearchOrder.DATE_D;
private SearchContentOption searchContentOption = SearchContentOption.ALL;
private final HashSet<String> includeKeywords = new HashSet<>(0);
private final HashSet<String> excludeKeywords = new HashSet<>(0);
private final Set<String> includeKeywords = new HashSet<>(0);
private final Set<String> excludeKeywords = new HashSet<>(0);
private int page = 1;
@ -161,11 +162,19 @@ public class PixivSearchLinkBuilder {
'}';
}
public String getContent() {
return content;
}
public PixivSearchLinkBuilder setSearchArea(SearchArea searchArea) {
this.searchArea = Objects.requireNonNull(searchArea);
return this;
}
public SearchArea getSearchArea() {
return searchArea;
}
/**
* 获取搜索条件.
* @return 搜索条件内容
@ -199,32 +208,60 @@ public class PixivSearchLinkBuilder {
return this;
}
public SearchMode getSearchMode() {
return searchMode;
}
public PixivSearchLinkBuilder setSearchType(SearchType searchType) {
this.searchType = Objects.requireNonNull(searchType);
return this;
}
public SearchType getSearchType() {
return searchType;
}
public PixivSearchLinkBuilder setSearchOrder(SearchOrder searchOrder) {
this.searchOrder = Objects.requireNonNull(searchOrder);
return this;
}
public SearchOrder getSearchOrder() {
return searchOrder;
}
public PixivSearchLinkBuilder setSearchContentOption(SearchContentOption searchContentOption) {
this.searchContentOption = Objects.requireNonNull(searchContentOption);
return this;
}
public SearchContentOption getSearchContentOption() {
return searchContentOption;
}
public PixivSearchLinkBuilder setRatioOption(RatioOption ratioOption) {
this.ratioOption = Objects.requireNonNull(ratioOption);
return this;
}
public RatioOption getRatioOption() {
return ratioOption;
}
public PixivSearchLinkBuilder setDateRange(Date startDate, Date endDate) {
this.startDate = startDate;
this.endDate = endDate;
return this;
}
public Date getStartDate() {
return startDate;
}
public Date getEndDate() {
return endDate;
}
public PixivSearchLinkBuilder setMaxSize(int width, int height) {
this.wgt = width;
this.hgt = height;
@ -245,6 +282,10 @@ public class PixivSearchLinkBuilder {
return this;
}
public int getPage() {
return page;
}
public PixivSearchLinkBuilder addExcludeKeyword(String keyword) {
excludeKeywords.add(keyword);
return this;
@ -265,6 +306,14 @@ public class PixivSearchLinkBuilder {
return this;
}
public Set<String> getIncludeKeywords() {
return new HashSet<>(includeKeywords);
}
public Set<String> getExcludeKeywords() {
return new HashSet<>(excludeKeywords);
}
/**
* 搜索区域
*/
@ -296,13 +345,18 @@ public class PixivSearchLinkBuilder {
*/
NOVELS("novel");
/**
* 可用的Json属性名
* @deprecated 该属性信息以移至 {@link PixivSearchAttribute}
* @see PixivSearchAttribute
*/
@Deprecated
public final String jsonKey;
SearchArea(String jsonKey) {
this.jsonKey = jsonKey;
}
}
/**
@ -401,5 +455,4 @@ public class PixivSearchLinkBuilder {
}
}

View File

@ -0,0 +1,84 @@
package net.lamgc.cgj.util;
import com.google.common.base.Strings;
import net.lamgc.cgj.pixiv.PixivSearchLinkBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Pixiv工具类
*/
public final class PixivUtils {
private final static Logger log = LoggerFactory.getLogger(PixivUtils.class);
private PixivUtils() {}
/**
* 快速构造一个PixivSearchLinkBuilder
* @param content 搜索内容
* @param type 搜索类型
* @param area 搜索范围
* @param includeKeywords 包含关键词
* @param excludeKeywords 排除关键词
* @param contentOption 内容级别选项
* @param pageIndex 搜索页数
* @return 返回PixivSearchLinkBuilder对象
* @see PixivSearchLinkBuilder
*/
public static PixivSearchLinkBuilder buildSearchLinkBuilderFromString(
String content,
String type,
String area,
String includeKeywords,
String excludeKeywords,
String contentOption,
int pageIndex
) {
PixivSearchLinkBuilder searchBuilder = new PixivSearchLinkBuilder(Strings.isNullOrEmpty(content) ? "" : content);
if (type != null) {
try {
searchBuilder.setSearchType(PixivSearchLinkBuilder.SearchType.valueOf(type.trim().toUpperCase()));
} catch (IllegalArgumentException e) {
log.warn("不支持的SearchType: {}", type);
}
}
if (area != null) {
try {
searchBuilder.setSearchArea(PixivSearchLinkBuilder.SearchArea.valueOf(area.trim().toUpperCase()));
} catch (IllegalArgumentException e) {
log.warn("不支持的SearchArea: {}", area);
}
}
if (contentOption != null) {
try {
searchBuilder.setSearchContentOption(
PixivSearchLinkBuilder.SearchContentOption.valueOf(contentOption.trim().toUpperCase()));
} catch (IllegalArgumentException e) {
log.warn("不支持的SearchContentOption: {}", contentOption);
}
}
if (!Strings.isNullOrEmpty(includeKeywords)) {
for (String keyword : includeKeywords.split(";")) {
searchBuilder.removeExcludeKeyword(keyword.trim());
searchBuilder.addIncludeKeyword(keyword.trim());
log.trace("已添加关键字: {}", keyword);
}
}
if (!Strings.isNullOrEmpty(excludeKeywords)) {
for (String keyword : excludeKeywords.split(";")) {
searchBuilder.removeIncludeKeyword(keyword.trim());
searchBuilder.addExcludeKeyword(keyword.trim());
log.trace("已添加排除关键字: {}", keyword);
}
}
if(pageIndex > 0) {
searchBuilder.setPage(pageIndex);
}
return searchBuilder;
}
}