mirror of
				https://github.com/LamGC/ContentGrabbingJi.git
				synced 2025-11-04 02:26:57 +00:00 
			
		
		
		
	[Add] 增加PixivSearchLinkBuilder中内部属性的getter, 增加Pixiv工具类;
[Update] PixivSearchAttribute 补充Javadoc; [Add] PixivSearchLinkBuilder 增加大部分属性的getter方法; [Add] PixivUtils 增加Pixiv工具类, 并添加通过字符串参数构造PixivSearchLinkBuilder的方法;
This commit is contained in:
		@ -1,5 +1,9 @@
 | 
				
			|||||||
package net.lamgc.cgj.pixiv;
 | 
					package net.lamgc.cgj.pixiv;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * 搜索结果的属性枚举类.
 | 
				
			||||||
 | 
					 * <p>按照请求的{@link PixivSearchLinkBuilder.SearchArea}获取所支持的属性数组</p>
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
public enum PixivSearchAttribute {
 | 
					public enum PixivSearchAttribute {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ARTWORKS("illustManga"),
 | 
					    ARTWORKS("illustManga"),
 | 
				
			||||||
 | 
				
			|||||||
@ -8,6 +8,7 @@ import java.text.SimpleDateFormat;
 | 
				
			|||||||
import java.util.Date;
 | 
					import java.util.Date;
 | 
				
			||||||
import java.util.HashSet;
 | 
					import java.util.HashSet;
 | 
				
			||||||
import java.util.Objects;
 | 
					import java.util.Objects;
 | 
				
			||||||
 | 
					import java.util.Set;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Pixiv搜索URL构造器
 | 
					 * Pixiv搜索URL构造器
 | 
				
			||||||
@ -26,8 +27,8 @@ public class PixivSearchLinkBuilder {
 | 
				
			|||||||
    private SearchOrder searchOrder = SearchOrder.DATE_D;
 | 
					    private SearchOrder searchOrder = SearchOrder.DATE_D;
 | 
				
			||||||
    private SearchContentOption searchContentOption = SearchContentOption.ALL;
 | 
					    private SearchContentOption searchContentOption = SearchContentOption.ALL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private final HashSet<String> includeKeywords = new HashSet<>(0);
 | 
					    private final Set<String> includeKeywords = new HashSet<>(0);
 | 
				
			||||||
    private final HashSet<String> excludeKeywords = new HashSet<>(0);
 | 
					    private final Set<String> excludeKeywords = new HashSet<>(0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private int page = 1;
 | 
					    private int page = 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -161,11 +162,19 @@ public class PixivSearchLinkBuilder {
 | 
				
			|||||||
                '}';
 | 
					                '}';
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public String getContent() {
 | 
				
			||||||
 | 
					        return content;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public PixivSearchLinkBuilder setSearchArea(SearchArea searchArea) {
 | 
					    public PixivSearchLinkBuilder setSearchArea(SearchArea searchArea) {
 | 
				
			||||||
        this.searchArea = Objects.requireNonNull(searchArea);
 | 
					        this.searchArea = Objects.requireNonNull(searchArea);
 | 
				
			||||||
        return this;
 | 
					        return this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public SearchArea getSearchArea() {
 | 
				
			||||||
 | 
					        return searchArea;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * 获取搜索条件.
 | 
					     * 获取搜索条件.
 | 
				
			||||||
     * @return 搜索条件内容
 | 
					     * @return 搜索条件内容
 | 
				
			||||||
@ -199,32 +208,60 @@ public class PixivSearchLinkBuilder {
 | 
				
			|||||||
        return this;
 | 
					        return this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public SearchMode getSearchMode() {
 | 
				
			||||||
 | 
					        return searchMode;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public PixivSearchLinkBuilder setSearchType(SearchType searchType) {
 | 
					    public PixivSearchLinkBuilder setSearchType(SearchType searchType) {
 | 
				
			||||||
        this.searchType = Objects.requireNonNull(searchType);
 | 
					        this.searchType = Objects.requireNonNull(searchType);
 | 
				
			||||||
        return this;
 | 
					        return this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public SearchType getSearchType() {
 | 
				
			||||||
 | 
					        return searchType;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public PixivSearchLinkBuilder setSearchOrder(SearchOrder searchOrder) {
 | 
					    public PixivSearchLinkBuilder setSearchOrder(SearchOrder searchOrder) {
 | 
				
			||||||
        this.searchOrder = Objects.requireNonNull(searchOrder);
 | 
					        this.searchOrder = Objects.requireNonNull(searchOrder);
 | 
				
			||||||
        return this;
 | 
					        return this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public SearchOrder getSearchOrder() {
 | 
				
			||||||
 | 
					        return searchOrder;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public PixivSearchLinkBuilder setSearchContentOption(SearchContentOption searchContentOption) {
 | 
					    public PixivSearchLinkBuilder setSearchContentOption(SearchContentOption searchContentOption) {
 | 
				
			||||||
        this.searchContentOption = Objects.requireNonNull(searchContentOption);
 | 
					        this.searchContentOption = Objects.requireNonNull(searchContentOption);
 | 
				
			||||||
        return this;
 | 
					        return this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public SearchContentOption getSearchContentOption() {
 | 
				
			||||||
 | 
					        return searchContentOption;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public PixivSearchLinkBuilder setRatioOption(RatioOption ratioOption) {
 | 
					    public PixivSearchLinkBuilder setRatioOption(RatioOption ratioOption) {
 | 
				
			||||||
        this.ratioOption = Objects.requireNonNull(ratioOption);
 | 
					        this.ratioOption = Objects.requireNonNull(ratioOption);
 | 
				
			||||||
        return this;
 | 
					        return this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public RatioOption getRatioOption() {
 | 
				
			||||||
 | 
					        return ratioOption;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public PixivSearchLinkBuilder setDateRange(Date startDate, Date endDate) {
 | 
					    public PixivSearchLinkBuilder setDateRange(Date startDate, Date endDate) {
 | 
				
			||||||
        this.startDate = startDate;
 | 
					        this.startDate = startDate;
 | 
				
			||||||
        this.endDate = endDate;
 | 
					        this.endDate = endDate;
 | 
				
			||||||
        return this;
 | 
					        return this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public Date getStartDate() {
 | 
				
			||||||
 | 
					        return startDate;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public Date getEndDate() {
 | 
				
			||||||
 | 
					        return endDate;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public PixivSearchLinkBuilder setMaxSize(int width, int height) {
 | 
					    public PixivSearchLinkBuilder setMaxSize(int width, int height) {
 | 
				
			||||||
        this.wgt = width;
 | 
					        this.wgt = width;
 | 
				
			||||||
        this.hgt = height;
 | 
					        this.hgt = height;
 | 
				
			||||||
@ -245,6 +282,10 @@ public class PixivSearchLinkBuilder {
 | 
				
			|||||||
        return this;
 | 
					        return this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public int getPage() {
 | 
				
			||||||
 | 
					        return page;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public PixivSearchLinkBuilder addExcludeKeyword(String keyword) {
 | 
					    public PixivSearchLinkBuilder addExcludeKeyword(String keyword) {
 | 
				
			||||||
        excludeKeywords.add(keyword);
 | 
					        excludeKeywords.add(keyword);
 | 
				
			||||||
        return this;
 | 
					        return this;
 | 
				
			||||||
@ -265,6 +306,14 @@ public class PixivSearchLinkBuilder {
 | 
				
			|||||||
        return this;
 | 
					        return this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public Set<String> getIncludeKeywords() {
 | 
				
			||||||
 | 
					        return new HashSet<>(includeKeywords);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public Set<String> getExcludeKeywords() {
 | 
				
			||||||
 | 
					        return new HashSet<>(excludeKeywords);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * 搜索区域
 | 
					     * 搜索区域
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
@ -406,5 +455,4 @@ public class PixivSearchLinkBuilder {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										84
									
								
								src/main/java/net/lamgc/cgj/util/PixivUtils.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										84
									
								
								src/main/java/net/lamgc/cgj/util/PixivUtils.java
									
									
									
									
									
										Normal 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.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);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return searchBuilder;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user