From d7a882cf8f58cceb3af3d12bf0ba2756d1993393 Mon Sep 17 00:00:00 2001 From: yutou Date: Fri, 15 Jan 2021 10:58:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20!=E6=96=B0=E7=95=AA=20?= =?UTF-8?q?=E6=8C=87=E4=BB=A4=EF=BC=8C=E8=8E=B7=E5=8F=96=E6=9C=AC=E5=91=A8?= =?UTF-8?q?=E6=96=B0=E7=95=AA=E5=88=97=E8=A1=A8=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E5=99=A8=E4=B8=BA35=E7=A7=92=E4=B8=80?= =?UTF-8?q?=E6=AC=A1=EF=BC=8C=E9=98=B2=E6=AD=A2=E4=B8=80=E5=88=86=E9=92=9F?= =?UTF-8?q?=E5=86=85=E5=A4=9A=E6=AC=A1=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/yutou/tools/ToolsApplication.java | 2 +- .../com/yutou/tools/bangumi/BangumiTools.java | 84 ++++++++++++++----- .../yutou/tools/utils/ApplicationInit.java | 2 +- .../com/yutou/tools/utils/QQBotManager.java | 5 ++ 4 files changed, 68 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/yutou/tools/ToolsApplication.java b/src/main/java/com/yutou/tools/ToolsApplication.java index 6417410..948edf5 100644 --- a/src/main/java/com/yutou/tools/ToolsApplication.java +++ b/src/main/java/com/yutou/tools/ToolsApplication.java @@ -9,7 +9,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ToolsApplication { - public static final String version="1.0.15.01"; + public static final String version="1.0.15.03"; public static void main(String[] args) { System.out.println("当前版本号:"+version); diff --git a/src/main/java/com/yutou/tools/bangumi/BangumiTools.java b/src/main/java/com/yutou/tools/bangumi/BangumiTools.java index cd7856e..dbfcc18 100644 --- a/src/main/java/com/yutou/tools/bangumi/BangumiTools.java +++ b/src/main/java/com/yutou/tools/bangumi/BangumiTools.java @@ -4,12 +4,11 @@ import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.yutou.tools.utils.HttpTools; import com.yutou.tools.utils.RedisTools; +import org.springframework.util.StringUtils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; -import java.util.regex.Matcher; -import java.util.regex.Pattern; public class BangumiTools { private static final String url = "http://api.bgm.tv/"; @@ -184,6 +183,64 @@ public class BangumiTools { return builder.toString(); } + /** + * 格式化输出新番列表 + * @return 新番信息 + */ + public static String reportBangumiList(){ + try { + JSONObject json=getBangumi(-1); + StringBuilder builder=new StringBuilder(); + assert json != null; + JSONArray array=json.getJSONArray("bangumi"); + for (Object o : array) { + JSONObject data= (JSONObject) o; + JSONObject weekday=data.getJSONObject("weekday"); + JSONArray items=data.getJSONArray("items"); + builder.append(weekday.getString("cn")).append("(").append(weekday.getString("ja")).append(")"); + builder.append(":").append("\n"); + reportBangumi(builder, items,false); + builder.append("\n"); + } + return builder.toString(); + }catch (Exception e){ + e.printStackTrace(); + return e.getMessage(); + } + } + + private static void reportBangumi(StringBuilder builder, JSONArray items,boolean reportEp) { + int epIndex = 0; + String epName = "N/A"; + for (Object oj : items) { + JSONObject item = (JSONObject) oj; + builder.append("[").append(item.getInteger("id")).append("]"); + builder.append(StringUtils.isEmpty(item.getString("name_cn")) + ?item.getString("name"):item.getString("name_cn")); + if(!reportEp){ + builder.append("\n"); + continue; + } + JSONObject info=getBangumiInfo(item.getInteger("id")); + if(info.get("eps")==null){ + continue; + } + JSONArray eps = info.getJSONArray("eps"); + for (Object oe : eps) { + JSONObject ep = (JSONObject) oe; + if (ep.getString("status").equals("Air")) { + epIndex = ep.getInteger("sort"); + epName = ep.getString("name"); + } else { + break; + } + } + builder.append(" 第").append(epIndex).append("话:"); + builder.append(epName); + builder.append("\n"); + } + } + /** * 格式化字符串输出今日播放番剧列表 * @@ -202,26 +259,7 @@ public class BangumiTools { JSONArray items = toDay.getJSONArray("items"); builder.append("今日 ").append(weekday.get("cn")).append("(").append(weekday.get("ja")).append(")"); builder.append(" 放送列表:").append("\n"); - int epIndex = 0; - String epName = "N/A"; - for (Object o : items) { - JSONObject item = (JSONObject) o; - builder.append("[").append(item.getInteger("id")).append("]"); - builder.append(item.getString("name_cn")); - JSONArray eps = getBangumiInfo(item.getInteger("id")).getJSONArray("eps"); - for (Object oe : eps) { - JSONObject ep = (JSONObject) oe; - if (ep.getString("status").equals("Air")) { - epIndex = ep.getInteger("sort"); - epName = ep.getString("name"); - } else { - break; - } - } - builder.append(" 第").append(epIndex).append("话:"); - builder.append(epName); - builder.append("\n"); - } + reportBangumi(builder, items,true); } RedisTools.set("toDayBangumi", builder.toString()); return builder.toString(); @@ -284,7 +322,7 @@ public class BangumiTools { } public static void main(String[] args) { - String str = reportBangumiInfo(262897); + String str = reportBangumiList(); System.out.println(str); } diff --git a/src/main/java/com/yutou/tools/utils/ApplicationInit.java b/src/main/java/com/yutou/tools/utils/ApplicationInit.java index 7b7aaef..e21a64d 100644 --- a/src/main/java/com/yutou/tools/utils/ApplicationInit.java +++ b/src/main/java/com/yutou/tools/utils/ApplicationInit.java @@ -34,6 +34,6 @@ public class ApplicationInit implements ApplicationRunner { } } - },0,10 * 1000); + },0,35 * 1000); } } diff --git a/src/main/java/com/yutou/tools/utils/QQBotManager.java b/src/main/java/com/yutou/tools/utils/QQBotManager.java index 1638ffb..aafe313 100644 --- a/src/main/java/com/yutou/tools/utils/QQBotManager.java +++ b/src/main/java/com/yutou/tools/utils/QQBotManager.java @@ -32,6 +32,7 @@ public class QQBotManager { private final static String QQ_GET_VERSION = "!version"; private final static String QQ_CMD = "!cmd"; private final static String QQ_BANGUMI_TODAY = "!今日动画"; + private final static String QQ_BANGUMI_LIST="!新番"; private final static String QQ_BANGUMI_SUB = "!查动画"; } @@ -198,6 +199,10 @@ public class QQBotManager { case QQCommands.QQ_BANGUMI_TODAY: QQBotManager.getInstance().sendMessage(BangumiTools.reportToDayBangumi()); break; + case QQCommands.QQ_BANGUMI_LIST: + getInstance().sendMessage("获取中..."); + getInstance().sendMessage(BangumiTools.reportBangumiList()); + break; case QQCommands.QQ_HELP: StringBuilder builder=new StringBuilder(); for (Field field : QQCommands.class.getDeclaredFields()) {