新增涩图功能

优化一堆不想写的代码
This commit is contained in:
yutou 2021-01-22 18:18:08 +08:00
parent 811a7fbd4c
commit 20d797adb9
9 changed files with 371 additions and 94 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.05";
public static final String version="1.0.16.05";
public static void main(String[] args) {
System.out.println("当前版本号:"+version);

View File

@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yutou.tools.utils.HttpTools;
import com.yutou.tools.utils.RedisTools;
import com.yutou.tools.utils.Tools;
import org.springframework.util.StringUtils;
import java.text.ParseException;
@ -81,7 +82,7 @@ public class BangumiTools {
public static String getPlayNextTime(int id) {
JSONObject info = getBangumiInfo(id);
JSONArray eps = info.getJSONArray("eps");
String toDayTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
String toDayTime = Tools.getToDayTime();
for (Object o : eps) {
JSONObject ep = (JSONObject) o;
String time = ep.getString("airdate");
@ -247,7 +248,7 @@ public class BangumiTools {
* @return 番剧列表
*/
public static String reportToDayBangumi() {
String toDayTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
String toDayTime = Tools.getToDayTime();
if (!toDayTime.equals(RedisTools.get("reportToDayBangumi"))) {
RedisTools.set("reportToDayBangumi", toDayTime);
StringBuilder builder = new StringBuilder();

View File

@ -0,0 +1,192 @@
package com.yutou.tools.other;
import com.alibaba.fastjson.JSONObject;
import com.yutou.tools.utils.QQBotManager;
import com.yutou.tools.utils.RedisTools;
import com.yutou.tools.utils.Tools;
import net.mamoe.mirai.event.events.GroupMessageEvent;
import net.mamoe.mirai.message.data.At;
import net.mamoe.mirai.message.data.Image;
import net.mamoe.mirai.message.data.MessageChainBuilder;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
public class QQSetu {
public static void printTodaySetu() {
String js = RedisTools.get(Tools.getToDayTime() + "_setu", 1);
if (js != null) {
JSONObject json = JSONObject.parseObject(js);
Map<String,Float> groupAverage=new HashMap<>();
Map<String,String> groupImage=new HashMap<>();
JSONObject setu=null;
for (String id : json.keySet()) {
String group=json.getJSONObject(id).getJSONObject("info").getLong("group")+"";
if(groupAverage.containsKey(group)){
if(groupAverage.get(group)<=json.getJSONObject(id).getFloat("average")){
groupAverage.put(group,json.getJSONObject(id).getFloat("average"));
groupImage.put(group,id);
}
}else{
groupAverage.put(group,json.getJSONObject(id).getFloat("average"));
groupImage.put(group,id);
}
}
for (String id : groupImage.keySet()) {
setu=json.getJSONObject(id);
if(setu!=null){
JSONObject info=setu.getJSONObject("info");
JSONObject score=setu.getJSONObject("score");
MessageChainBuilder builder = new MessageChainBuilder();
builder.append(Image.fromId(info.getString("id")));
builder.append("本日最佳涩图由").append(new At(info.getLong("sourQQ"))).append("提供\n");
builder.append("获得分数 ").append(String.valueOf(setu.getFloat("average"))).append("\n");
builder.append("共有 ").append(String.valueOf(score.getIntValue("userNumber"))).append(" 参与投票");
QQBotManager.getInstance().sendMessage(info.getLong("group"),builder);
}
}
}
}
private boolean isSetu(Image image) {
String url = Image.queryUrl(image);
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
int length = connection.getContentLength();
connection.disconnect();
if (length > 50000) {
return true;
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
return false;
}
private void setuBuilder(Image image, GroupMessageEvent event) {
if (!isSetu(image)) {
return;
}
if (RedisTools.get(event.getGroup().getId()+"setu") != null) {
printSetu(event.getGroup().getId());
}
setuScore.clear();
JSONObject json = new JSONObject();
json.put("id", image.getImageId());
json.put("sourName", event.getSenderName());
json.put("sourQQ", event.getSender().getId());
json.put("group", event.getGroup().getId());
RedisTools.set(event.getGroup().getId()+"setu", json.toJSONString(),5*60);
if (timer != null) {
timer.cancel();
timer = null;
}
startTimer(event.getGroup().getId());
}
private void startTimer(long group) {
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if(!setuScore.isEmpty()){
printSetu(group);
}
timer = null;
}
}, 5 * 60 * 1000);
}
private void printSetu(long group) {
JSONObject jt = JSONObject.parseObject(RedisTools.get(group+"setu"));
String id = jt.getString("id");
float average = 0;
float max = 0;
float min = 10;
float length = 0;
String maxName = "";
String minName = "";
if(setuScore.size()==0){
return;
}
for (String name : setuScore.keySet()) {
length += setuScore.get(name);
average += setuScore.get(name);
if (setuScore.get(name) > max) {
max = setuScore.get(name);
maxName = name;
}
if (setuScore.get(name) < min) {
min = setuScore.get(name);
minName = name;
}
}
JSONObject score = new JSONObject();
score.put("max", max);
score.put("min", min);
score.put("sum", length);
score.put("maxName", maxName);
score.put("minName", minName);
score.put("userNumber", setuScore.size());
average = average / setuScore.size();
String builder = "涩图评分:" + average +
"其中最高分由:" + maxName + " 给与:" + max +
"其中最低分由:" + minName + " 给与:" + min;
QQBotManager.getInstance().sendMessage(group, builder);
String st = RedisTools.get(Tools.getToDayTime() + "_setu", 1);
JSONObject json;
if (st == null) {
json = new JSONObject();
} else {
json = JSONObject.parseObject(st);
}
if (!json.containsKey(id)) {
JSONObject item;
if (json.containsKey("item")) {
item = json.getJSONObject("item");
} else {
item = new JSONObject();
}
item.put("score", score);
item.put("info", jt);
item.put("average", average);
json.put(id, item);
RedisTools.set(1, Tools.getToDayTime() + "_setu", json.toJSONString());
}
RedisTools.remove(group+"setu",0);
setuScore.clear();
}
public void setu(String msg, GroupMessageEvent event) {
if (msg.trim().equals("[图片]")) {
Image image = (Image) event.getMessage().stream().filter(Image.class::isInstance).findFirst().orElse(null);
if (image != null) {
setuBuilder(image, event);
return;
}
}
try {
float i = Float.parseFloat(msg.trim());
if (i > 0 && i < 10) {
String name = event.getSenderName();
if (!setuScore.containsKey(name)) {
setuScore.put(name, i);
}
}
} catch (Exception ignored) {
ignored.printStackTrace();
}
}
private Timer timer;
private static Map<String, Float> setuScore = new HashMap<>();
}

View File

@ -81,7 +81,7 @@ public class tools {
json.put("params", params);
json.put("cookie", cookies);
json.put("header", header);
Tools.sendServer("打印请求", json.toJSONString());
//Tools.sendServer("打印请求", json.toJSONString());
try {
switch (RedisTools.get("request")){
case "success":
@ -93,7 +93,7 @@ public class tools {
return RedisTools.get("request");
}
}catch (Exception e){
e.printStackTrace();
// e.printStackTrace();
}
return json.toJSONString();

View File

@ -1,5 +1,6 @@
package com.yutou.tools.utils;
import com.yutou.tools.other.QQSetu;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@ -31,6 +32,9 @@ public class ApplicationInit implements ApplicationRunner {
case "08:00":
QQBotManager.getInstance().reportToDayBangumi();
break;
case "23:59":
QQSetu.printTodaySetu();
break;
}
}

View File

@ -3,6 +3,7 @@ package com.yutou.tools.utils;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.yutou.tools.utils.Interfaces.NetworkInterface;
import org.jetbrains.annotations.NotNull;
import java.io.BufferedReader;
import java.io.IOException;
@ -12,12 +13,15 @@ import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
public class HttpTools {
public static String get(String url){
public static String get(String url) {
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestProperty("User-Agent", getExtUa());
connection.setRequestProperty("User-Agent", getKuKuUA());
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder str = new StringBuilder();
String tmp;
@ -32,6 +36,7 @@ public class HttpTools {
}
return null;
}
public static void post(final String url, final byte[] body, final NetworkInterface networkInterface) {
new Thread(new Runnable() {
@ -78,9 +83,15 @@ public class HttpTools {
}
}).start();
}
private static String getExtUa() {
return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36";
}
private static String getKuKuUA() {
return "/KUKU_APP(Android/#/cn.kuku.sdk/ttsdk17228/29401/A-2.9.4.01.KUSDK/868139039134314/fcddf839c8c135fa/F4:60:E2:AB:25:1A/460019406520644/+8618569400341/#/9/Redmi 6 Pro/xiaomi/1736/76fda4d6-cd6b-485f-987b-8d347b007f24/#/KUKU/Native/92972ea9651fbd2e)";
}
public String toUrlParams(JSONObject json) {
StringBuilder string = new StringBuilder();
Set<String> keys = json.keySet();
@ -101,4 +112,26 @@ public class HttpTools {
string = new StringBuilder(string.substring(1, string.length()).replaceAll(" ", ""));
return string.toString();
}
public static void main(String[] args) {
JSONObject json = new JSONObject();
json.put("pid", "102");
json.put("gid", "100584");
json.put("gameKey", "0gha58u1c9FjZkeAsEmYIzTvp");
json.put("access_token", "659c-S1gV0DwMXdYjPDlSrSLNYOvA8qUoCSvmdFEHvZugKgNX4Z2BCwF18A7W2gRdG7WiWfKsbZgF6YssZHhaozksI9RBn2QQFTXzmAHtbMd4ginEEtwdKmPCM4JbJGg1ollqoNE0PcGENpa4F3e7EdSOa_JFyE6XyUQN1iurJU3F8MZfLlTIcTR9USYoHX15vsAkCht_0mrapZblkeY1_8HFrmK8rlenbZLxccy7PrMz5eZ9uPPDJL5OYiEahyrtLENB8SVmlGofJfQw8wUjN8_XVZSfLMujdwz24");
String url = "http://192.168.1.156:9020/Faxing/reg?" +
"&tpyeCode=dimai" +
"&regParamJson=" + json.toJSONString();
/* ExecutorService service= Executors.newCachedThreadPool();
for (int i = 0; i < 3000; i++) {
service.submit(new Runnable() {
@Override
public void run() {
get(url);
}
});
}*/
System.out.println(url);
//String str=get(url);
}
}

View File

@ -1,8 +1,10 @@
package com.yutou.tools.utils;
import com.alibaba.fastjson.JSONObject;
import com.yutou.tools.ToolsApplication;
import com.yutou.tools.bangumi.BangumiTools;
import com.yutou.tools.interfaces.DownloadInterface;
import com.yutou.tools.other.QQSetu;
import net.mamoe.mirai.Bot;
import net.mamoe.mirai.BotFactory;
import net.mamoe.mirai.event.GlobalEventChannel;
@ -13,10 +15,11 @@ import net.mamoe.mirai.utils.BotConfiguration;
import net.mamoe.mirai.utils.ExternalResource;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -32,7 +35,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_LIST = "!新番";
private final static String QQ_BANGUMI_SUB = "!查动画";
}
@ -40,11 +43,15 @@ public class QQBotManager {
private Bot bot;
private static final long qqGroup = 891655174L;
private boolean isLogin = false;
private static boolean isInit=false;
private QQBotManager() {
Object isRun = ConfigTools.load(ConfigTools.CONFIG, "qq_bot");
if (isRun != null && (boolean) isRun) {
isLogin = true;
isInit=true;
init();
}
}
@ -81,7 +88,7 @@ public class QQBotManager {
} catch (InterruptedException e) {
e.printStackTrace();
}
String str=sendMessage("姬妻酱上线拉~");
String str = sendMessage("姬妻酱上线拉~");
System.out.println(str);
}
@ -94,7 +101,7 @@ public class QQBotManager {
}
public static QQBotManager getInstance() {
if (botManager == null) {
if (botManager == null&&!isInit) {
botManager = new QQBotManager();
}
return botManager;
@ -118,6 +125,7 @@ public class QQBotManager {
public void reportToDayBangumi() {
getInstance().sendMessage(BangumiTools.reportToDayBangumi());
}
public String sendMessage(String text) {
if (bot != null) {
try {
@ -129,6 +137,23 @@ public class QQBotManager {
return getNotLoginQQ();
}
public String sendMessage(Long group, String text) {
if (bot != null) {
try {
return Objects.requireNonNull(bot.getGroup(group)).sendMessage(text).toString();
} catch (Exception e) {
e.printStackTrace();
}
}
return getNotLoginQQ();
}
public void sendMessage(Long group,MessageChainBuilder builder) {
if(bot!=null){
Objects.requireNonNull(bot.getGroup(group)).sendMessage(builder.asMessageChain());
}
}
public String sendMessage(File imageFile, String text) {
try {
if (bot != null) {
@ -178,75 +203,85 @@ public class QQBotManager {
private static class MessageListener implements Consumer<GroupMessageEvent> {
@Override
public void accept(GroupMessageEvent event) {
if (event.getGroup().getId() == qqGroup) {
String msg = event.getMessage().contentToString();
msg = msg.replace("", "!").toLowerCase();
switch (msg) {
case QQCommands.QQ_OPEN_PC:
RedisTools.Consumer.system("openPC", null);
break;
case QQCommands.QQ_UPDATE_IP:
RedisTools.Consumer.system("updateIP", null);
break;
case QQCommands.QQ_GET_IP:
RedisTools.Consumer.bot("getip");
break;
case QQCommands.QQ_GET_VERSION:
sendVersion();
break;
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()) {
try {
field.setAccessible(true);
builder.append(field.get(null)).append("\n");
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
getInstance().sendMessage(builder.toString());
break;
default:
if (msg.startsWith(QQCommands.QQ_CMD)) {
RedisTools.Consumer.system("cmd", msg.replace(QQCommands.QQ_CMD, ""));
} else if (msg.startsWith(QQCommands.QQ_BANGUMI_SUB)) {
String info = null;
try {
int id = Integer.parseInt(msg.replace(QQCommands.QQ_BANGUMI_SUB, "").trim());
info = BangumiTools.reportBangumiInfo(id);
} catch (Exception e) {
String key=msg.replace(QQCommands.QQ_BANGUMI_SUB, "").trim();
info = BangumiTools.reportSearchBangumi(key);
}
List<String> imgs=new ArrayList<>();
if (info.contains("<img") && info.contains(" /img>")) {
imgs = getImages(info);
for (String img : imgs) {
info=info.replace("<img "+img+" /img>","");
}
}
sendImagesMsg(imgs,info);
}
}
}/*else{
sendMessage("群:"+event.getGroup().getId()+" 发来消息:\n"+event.getMessage().contentToString());
}*/
String msg = event.getMessage().contentToString();
switch (event.getGroup().getId() + "") {
case qqGroup + "":
myGroup(msg);
case "570197155":
new QQSetu().setu(msg, event);
}
}
private void myGroup(String msg) {
msg = msg.replace("", "!").toLowerCase();
switch (msg) {
case QQCommands.QQ_OPEN_PC:
RedisTools.Consumer.system("openPC", null);
break;
case QQCommands.QQ_UPDATE_IP:
RedisTools.Consumer.system("updateIP", null);
break;
case QQCommands.QQ_GET_IP:
RedisTools.Consumer.bot("getip");
break;
case QQCommands.QQ_GET_VERSION:
sendVersion();
break;
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()) {
try {
field.setAccessible(true);
builder.append(field.get(null)).append("\n");
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
getInstance().sendMessage(builder.toString());
break;
default:
if (msg.startsWith(QQCommands.QQ_CMD)) {
RedisTools.Consumer.system("cmd", msg.replace(QQCommands.QQ_CMD, ""));
} else if (msg.startsWith(QQCommands.QQ_BANGUMI_SUB)) {
String info = null;
try {
int id = Integer.parseInt(msg.replace(QQCommands.QQ_BANGUMI_SUB, "").trim());
info = BangumiTools.reportBangumiInfo(id);
} catch (Exception e) {
String key = msg.replace(QQCommands.QQ_BANGUMI_SUB, "").trim();
info = BangumiTools.reportSearchBangumi(key);
}
List<String> imgs = new ArrayList<>();
if (info.contains("<img") && info.contains(" /img>")) {
imgs = getImages(info);
for (String img : imgs) {
info = info.replace("<img " + img + " /img>", "");
}
}
sendImagesMsg(imgs, info);
}
}
}
private List<File> files;
private int index=0;
private void sendImagesMsg(List<String> imgs,String text){
files=new ArrayList<>();
index=0;
if(imgs.size()==0){
private int index = 0;
private void sendImagesMsg(List<String> imgs, String text) {
files = new ArrayList<>();
index = 0;
if (imgs.size() == 0) {
getInstance().sendMessage(text);
return;
}
@ -263,17 +298,19 @@ public class QQBotManager {
public void onError(Exception e) {
super.onError(e);
index++;
send(imgs.size(),text);
send(imgs.size(), text);
}
});
}
}
private void send(int size,String text){
if((files.size()+index)==size){
String str= getInstance().sendMessage(files,text);
System.out.println("str = "+str);
private void send(int size, String text) {
if ((files.size() + index) == size) {
String str = getInstance().sendMessage(files, text);
System.out.println("str = " + str);
}
}
private void sendVersion() {
String localVersion = ToolsApplication.version;
String serverVersion = HttpTools.get("http://tools.yutou233.cn:8000/public/version.do?token=zIrsh9TUZP2lfRW753PannG49E7VJvor");

View File

@ -96,17 +96,7 @@ public class RedisTools {
}
public static boolean remove(String key) {
if (isNotInstallRedis) {
return false;
}
Jedis jedis = getRedis();
Long i = jedis.del(key);
jedis.close();
if (i > 0) {
return true;
} else {
return false;
}
return remove(key,0);
}
public static void removeLoginState(String uid) {
@ -141,6 +131,21 @@ public class RedisTools {
return new Jedis(host, port);
}
public static boolean remove(String key, int index) {
if (isNotInstallRedis) {
return false;
}
Jedis jedis = getRedis();
jedis.select(index);
Long i = jedis.del(key);
jedis.close();
if (i > 0) {
return true;
} else {
return false;
}
}
private static class PropertyUtil {
// 加载property文件到io流里面

View File

@ -21,6 +21,7 @@ import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
import java.util.Random;
@ -378,4 +379,8 @@ public class Tools {
printWriter.close();
return writer.toString();
}
public static String getToDayTime() {
return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
}
}