新增 !新番 指令,获取本周新番列表

优化定时器为35秒一次,防止一分钟内多次调用
This commit is contained in:
yutou 2021-01-15 10:58:55 +08:00
parent 92fc97e128
commit d7a882cf8f
4 changed files with 68 additions and 25 deletions

View File

@ -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);

View File

@ -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);
}

View File

@ -34,6 +34,6 @@ public class ApplicationInit implements ApplicationRunner {
}
}
},0,10 * 1000);
},0,35 * 1000);
}
}

View File

@ -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()) {