From e93c322c024dd665406af96138c4603a82d8697b Mon Sep 17 00:00:00 2001 From: LamGC Date: Thu, 4 Jun 2020 20:14:57 +0800 Subject: [PATCH] =?UTF-8?q?[Change]=20BotGlobal,=20BotCommandProcess=20?= =?UTF-8?q?=E8=B0=83=E6=95=B4PixivDownload=E7=9A=84=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E8=BF=87=E7=A8=8B;=20[Change]=20BotCommandProcess=20?= =?UTF-8?q?=E5=B0=86'Search'=E6=89=80=E5=B1=9E=E7=BC=93=E5=AD=98=E9=83=A8?= =?UTF-8?q?=E5=88=86=E6=8A=BD=E5=87=BA=E5=88=B0=E5=8D=95=E7=8B=AC=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E6=B3=95('getSearchBody');?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/lamgc/cgj/bot/BotCommandProcess.java | 220 ++++++++++-------- .../net/lamgc/cgj/bot/boot/BotGlobal.java | 13 +- 2 files changed, 130 insertions(+), 103 deletions(-) diff --git a/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java b/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java index ceaab9a..6f6d122 100644 --- a/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java +++ b/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java @@ -331,88 +331,7 @@ public class BotCommandProcess { @Argument(name = "page", force = false, defaultValue = "1") int pagesIndex ) throws IOException { log.info("正在执行搜索..."); - PixivSearchBuilder searchBuilder = new PixivSearchBuilder(Strings.isNullOrEmpty(content) ? "" : content); - if (type != null) { - try { - searchBuilder.setSearchType(PixivSearchBuilder.SearchType.valueOf(type.toUpperCase())); - } catch (IllegalArgumentException e) { - log.warn("不支持的SearchType: {}", type); - } - } - if (area != null) { - try { - searchBuilder.setSearchArea(PixivSearchBuilder.SearchArea.valueOf(area)); - } catch (IllegalArgumentException e) { - log.warn("不支持的SearchArea: {}", area); - } - } - if (contentOption != null) { - try { - searchBuilder.setSearchContentOption(PixivSearchBuilder.SearchContentOption.valueOf(contentOption)); - } 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.debug("已添加关键字: {}", keyword); - } - } - if (!Strings.isNullOrEmpty(excludeKeywords)) { - for (String keyword : excludeKeywords.split(";")) { - searchBuilder.removeIncludeKeyword(keyword.trim()); - searchBuilder.addExcludeKeyword(keyword.trim()); - log.debug("已添加排除关键字: {}", keyword); - } - } - - log.info("正在搜索作品, 条件: {}", searchBuilder.getSearchCondition()); - - String requestUrl = searchBuilder.buildURL().intern(); - log.debug("RequestUrl: {}", requestUrl); - JsonObject resultBody = null; - if(!searchBodyCache.exists(requestUrl)) { - synchronized (requestUrl) { - if (!searchBodyCache.exists(requestUrl)) { - log.debug("searchBody缓存失效, 正在更新..."); - JsonObject jsonObject; - HttpGet httpGetRequest = BotGlobal.getGlobal().getPixivDownload().createHttpGetRequest(requestUrl); - HttpResponse response = BotGlobal.getGlobal().getPixivDownload().getHttpClient().execute(httpGetRequest); - - String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); - log.debug("ResponseBody: {}", responseBody); - jsonObject = BotGlobal.getGlobal().getGson().fromJson(responseBody, JsonObject.class); - - if (jsonObject.get("error").getAsBoolean()) { - log.error("接口请求错误, 错误信息: {}", jsonObject.get("message").getAsString()); - return "处理命令时发生错误!"; - } - - long expire = 7200 * 1000; - String propValue = SettingProperties - .getProperty(SettingProperties.GLOBAL, "cache.searchBody.expire", "7200000"); - try { - expire = Long.parseLong(propValue); - } catch (Exception e) { - log.warn("全局配置项 \"{}\" 值非法, 已使用默认值: {}", propValue, expire); - } - resultBody = jsonObject.getAsJsonObject().getAsJsonObject("body"); - searchBodyCache.update(requestUrl, jsonObject, expire); - log.debug("searchBody缓存已更新(有效时间: {})", expire); - } else { - log.debug("搜索缓存命中."); - } - } - } else { - log.debug("搜索缓存命中."); - } - - if(Objects.isNull(resultBody)) { - resultBody = searchBodyCache.getCache(requestUrl).getAsJsonObject().getAsJsonObject("body"); - } + JsonObject resultBody = getSearchBody(content, type, area, includeKeywords, excludeKeywords, contentOption); StringBuilder result = new StringBuilder("内容 " + content + " 的搜索结果:\n"); log.debug("正在处理信息..."); @@ -721,6 +640,22 @@ public class BotCommandProcess { return returnRaw || settingProp == null ? rawValue : rawValue && !settingProp.getProperty("image.allowR18", "false").equalsIgnoreCase("true"); } + /** + * 获取图片存储目录. + *

每次调用都会检查目录是否存在, 如不存在则会抛出异常

+ * @return 返回File对象 + * @throws RuntimeException 当目录创建失败时将包装{@link IOException}异常并抛出. + */ + private static File getImageStoreDir() { + if(!imageStoreDir.exists() && !Files.isSymbolicLink(imageStoreDir.toPath())) { + if(!imageStoreDir.mkdirs()) { + log.warn("酷Q图片缓存目录失效!(Path: {} )", imageStoreDir.getAbsolutePath()); + throw new RuntimeException(new IOException("文件夹创建失败!")); + } + } + return imageStoreDir; + } + /** * 获取作品信息 * @param illustId 作品Id @@ -811,23 +746,6 @@ public class BotCommandProcess { return result; } - /** - * 获取图片存储目录. - *

每次调用都会检查目录是否存在, 如不存在则会抛出异常

- * @return 返回File对象 - * @throws RuntimeException 当目录创建失败时将包装{@link IOException}异常并抛出. - */ - private static File getImageStoreDir() { - if(!imageStoreDir.exists() && !Files.isSymbolicLink(imageStoreDir.toPath())) { - if(!imageStoreDir.mkdirs()) { - log.warn("酷Q图片缓存目录失效!(Path: {} )", imageStoreDir.getAbsolutePath()); - throw new RuntimeException(new IOException("文件夹创建失败!")); - } - } - return imageStoreDir; - } - - private final static Random expireTimeFloatRandom = new Random(); /** * 获取排行榜 @@ -881,6 +799,110 @@ public class BotCommandProcess { return PixivDownload.getRanking(result, start - 1, range); } + /** + * 获取搜索结果 + * @param content 搜索内容 + * @param type 类型 + * @param area 范围 + * @param includeKeywords 包含关键词 + * @param excludeKeywords 排除关键词 + * @param contentOption 内容类型 + * @return 返回完整搜索结果 + * @throws IOException 当请求发生异常, 或接口返回异常信息时抛出. + */ + public static JsonObject getSearchBody( + String content, + String type, + String area, + String includeKeywords, + String excludeKeywords, + String contentOption) throws IOException { + PixivSearchBuilder searchBuilder = new PixivSearchBuilder(Strings.isNullOrEmpty(content) ? "" : content); + if (type != null) { + try { + searchBuilder.setSearchType(PixivSearchBuilder.SearchType.valueOf(type.toUpperCase())); + } catch (IllegalArgumentException e) { + log.warn("不支持的SearchType: {}", type); + } + } + if (area != null) { + try { + searchBuilder.setSearchArea(PixivSearchBuilder.SearchArea.valueOf(area)); + } catch (IllegalArgumentException e) { + log.warn("不支持的SearchArea: {}", area); + } + } + if (contentOption != null) { + try { + searchBuilder.setSearchContentOption(PixivSearchBuilder.SearchContentOption.valueOf(contentOption)); + } 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.debug("已添加关键字: {}", keyword); + } + } + if (!Strings.isNullOrEmpty(excludeKeywords)) { + for (String keyword : excludeKeywords.split(";")) { + searchBuilder.removeIncludeKeyword(keyword.trim()); + searchBuilder.addExcludeKeyword(keyword.trim()); + log.debug("已添加排除关键字: {}", keyword); + } + } + + log.info("正在搜索作品, 条件: {}", searchBuilder.getSearchCondition()); + + String requestUrl = searchBuilder.buildURL().intern(); + log.debug("RequestUrl: {}", requestUrl); + JsonObject resultBody = null; + if(!searchBodyCache.exists(requestUrl)) { + synchronized (requestUrl) { + if (!searchBodyCache.exists(requestUrl)) { + log.debug("searchBody缓存失效, 正在更新..."); + JsonObject jsonObject; + HttpGet httpGetRequest = BotGlobal.getGlobal().getPixivDownload().createHttpGetRequest(requestUrl); + HttpResponse response = BotGlobal.getGlobal().getPixivDownload().getHttpClient().execute(httpGetRequest); + + String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); + log.debug("ResponseBody: {}", responseBody); + jsonObject = BotGlobal.getGlobal().getGson().fromJson(responseBody, JsonObject.class); + + if (jsonObject.get("error").getAsBoolean()) { + log.error("接口请求错误, 错误信息: {}", jsonObject.get("message").getAsString()); + throw new IOException("Interface Request Error: " + jsonObject.get("message").getAsString()); + } + + long expire = 7200 * 1000; + String propValue = SettingProperties + .getProperty(SettingProperties.GLOBAL, "cache.searchBody.expire", "7200000"); + try { + expire = Long.parseLong(propValue); + } catch (Exception e) { + log.warn("全局配置项 \"{}\" 值非法, 已使用默认值: {}", propValue, expire); + } + resultBody = jsonObject.getAsJsonObject().getAsJsonObject("body"); + searchBodyCache.update(requestUrl, jsonObject, expire); + log.debug("searchBody缓存已更新(有效时间: {})", expire); + } else { + log.debug("搜索缓存命中."); + } + } + } else { + log.debug("搜索缓存命中."); + } + + if(Objects.isNull(resultBody)) { + resultBody = searchBodyCache.getCache(requestUrl).getAsJsonObject().getAsJsonObject("body"); + } + return resultBody; + } + + private static String buildSyncKey(String... keys) { StringBuilder sb = new StringBuilder(); for (String string : keys) { diff --git a/src/main/java/net/lamgc/cgj/bot/boot/BotGlobal.java b/src/main/java/net/lamgc/cgj/bot/boot/BotGlobal.java index c5f13b2..ce639fe 100644 --- a/src/main/java/net/lamgc/cgj/bot/boot/BotGlobal.java +++ b/src/main/java/net/lamgc/cgj/bot/boot/BotGlobal.java @@ -45,7 +45,7 @@ public final class BotGlobal { .serializeNulls() .create(); - private final PixivDownload pixivDownload; + private PixivDownload pixivDownload; private BotGlobal() { this.redisUri = URI.create("redis://" + System.getProperty("cgj.redisAddress")); @@ -68,9 +68,6 @@ public final class BotGlobal { } } this.proxy = temp; - - this.pixivDownload = - new PixivDownload(BotGlobal.getGlobal().getCookieStore(), BotGlobal.getGlobal().getProxy()); } public URI getRedisUri() { @@ -94,11 +91,19 @@ public final class BotGlobal { public CookieStore getCookieStore() { + if(pixivDownload == null) { + throw new IllegalStateException("CookieStore needs to be set before PixivDownload can be obtained"); + } return cookieStore; } public void setCookieStore(CookieStore cookieStore) { + if(this.cookieStore != null) { + throw new IllegalStateException("CookieStore set"); + } this.cookieStore = cookieStore; + this.pixivDownload = + new PixivDownload(cookieStore, proxy); } public Gson getGson() {