diff --git a/src/main/java/com/yutou/qqbot/bilibili/BiliBiliUtils.java b/src/main/java/com/yutou/qqbot/bilibili/BiliBiliUtils.java index c51d8c0..3a7ce38 100644 --- a/src/main/java/com/yutou/qqbot/bilibili/BiliBiliUtils.java +++ b/src/main/java/com/yutou/qqbot/bilibili/BiliBiliUtils.java @@ -12,7 +12,9 @@ import java.net.InetSocketAddress; import java.net.Proxy; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; public class BiliBiliUtils { private static long oldBiliBiliHttpTime = 0; @@ -64,6 +66,10 @@ public class BiliBiliUtils { } public static T http(String url, HTTP model, String body, RET_MODEL ret_model) { + return http(url, model, body, null, ret_model); + } + + public static T http(String url, HTTP model, String body, Map headers, RET_MODEL ret_model) { JSONObject json = null; BufferedInputStream stream = null; ByteArrayOutputStream outputStream = null; @@ -81,8 +87,22 @@ public class BiliBiliUtils { if (model == HTTP.POST) { connection = getBiliHttpPost(url, getCookie()); } else { + if (body != null) { + if (url.contains("?")) { + url += "&" + body; + } else { + url += "?" + body; + } + body = null; + } connection = getBiliHttpGet(url, getCookie()); } + if (headers != null) { + for (String key : headers.keySet()) { + connection.setRequestProperty(key, headers.get(key)); + } + } + System.out.println("url = " + url); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); if (!StringUtils.isEmpty(body)) { @@ -182,7 +202,9 @@ public class BiliBiliUtils { oldBiliBiliHttpTime = System.currentTimeMillis(); } HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection(); - setConnection(cookie, connection); + if (cookie != null) { + setConnection(cookie, connection); + } connection.setReadTimeout(5000); connection.setConnectTimeout(5000); return connection; @@ -191,7 +213,7 @@ public class BiliBiliUtils { public static File download(final String url, final String saveName, boolean isProxy) { File jar = null; try { - File savePath = new File(HttpTools.downloadPath+saveName); + File savePath = new File(HttpTools.downloadPath + saveName); Proxy proxy = null; if (!savePath.exists()) { savePath.mkdirs(); @@ -230,7 +252,7 @@ public class BiliBiliUtils { } jar.renameTo(oldJar); Log.i("DOWNLOAD", "实际保存:" + oldJar.getAbsolutePath() + " " + oldJar.getName()); - return oldJar; + return oldJar; } catch (Exception e) { e.printStackTrace(); if (jar != null) { @@ -306,41 +328,45 @@ public class BiliBiliUtils { System.out.println("sign = " + sign); } - public static boolean sendLiveDanmu(long roomId,String msg){ - JSONObject body=new JSONObject(); - body.put("msg",msg); - body.put("roomid",roomId); - body.put("color",16777215); - body.put("fontsize",25); - body.put("rnd",System.currentTimeMillis()/1000); - body.put("csrf",BiliLogin.getCookieToken()); - body.put("csrf_token",BiliLogin.getCookieToken()); + public static boolean sendLiveDanmu(long roomId, String msg) { + JSONObject body = new JSONObject(); + body.put("msg", msg); + body.put("roomid", roomId); + body.put("color", 16777215); + body.put("fontsize", 25); + body.put("rnd", System.currentTimeMillis() / 1000); + body.put("csrf", BiliLogin.getCookieToken()); + body.put("csrf_token", BiliLogin.getCookieToken()); JSONObject post = BiliBiliUtils.http_post("https://api.live.bilibili.com/msg/send", HttpTools.toUrlParams(body)); - return post.getInteger("code")==0; + return post.getInteger("code") == 0; } - public static String liveSignIn(){ + + public static String liveSignIn() { //{"code":0,"data":{"coin":1,"gold":19500,"silver":106394,"tid":"Silver2Coin22101413201169763005873"},"message":"兑换成功"} - JSONObject body=new JSONObject(); - body.put("csrf",BiliLogin.getCookieToken()); - body.put("csrf_token",BiliLogin.getCookieToken()); + JSONObject body = new JSONObject(); + body.put("csrf", BiliLogin.getCookieToken()); + body.put("csrf_token", BiliLogin.getCookieToken()); JSONObject post = BiliBiliUtils.http_post("https://api.live.bilibili.com/xlive/revenue/v1/wallet/silver2coin", HttpTools.toUrlParams(body)); JSONObject post_ = BiliBiliUtils.http_get("https://api.live.bilibili.com/xlive/web-ucenter/v1/sign/DoSign"); - return post.getString("message")+"|"+post_.getString("message"); + return post.getString("message") + "|" + post_.getString("message"); } - public static JSONObject getLiveRoom(int roomId){ - JSONObject body=new JSONObject(); - body.put("room_id",roomId); - body.put("csrf",BiliLogin.getCookieToken()); - body.put("csrf_token",BiliLogin.getCookieToken()); + + public static JSONObject getLiveRoom(int roomId) { + JSONObject body = new JSONObject(); + body.put("room_id", roomId); + body.put("csrf", BiliLogin.getCookieToken()); + body.put("csrf_token", BiliLogin.getCookieToken()); return BiliBiliUtils.http_post("https://api.live.bilibili.com/room/v1/Room/get_info", HttpTools.toUrlParams(body)); } - public static JSONObject getUserInfo(int mid){ - JSONObject body=new JSONObject(); - body.put("mid",mid); - return BiliBiliUtils.http_get("https://api.bilibili.com/x/space/acc/info?"+ HttpTools.toUrlParams(body)); + + public static JSONObject getUserInfo(int mid) { + JSONObject body = new JSONObject(); + body.put("mid", mid); + return BiliBiliUtils.http_get("https://api.bilibili.com/x/space/acc/info?" + HttpTools.toUrlParams(body)); } - public static boolean checkLiveRoom(int roomId){ - JSONObject post=getLiveRoom(roomId); - return post.getInteger("code")==0; + + public static boolean checkLiveRoom(int roomId) { + JSONObject post = getLiveRoom(roomId); + return post.getInteger("code") == 0; } } diff --git a/src/main/java/com/yutou/qqbot/models/Commands/WoodenFish.java b/src/main/java/com/yutou/qqbot/models/Commands/WoodenFish.java index 92c0cd1..a600c48 100644 --- a/src/main/java/com/yutou/qqbot/models/Commands/WoodenFish.java +++ b/src/main/java/com/yutou/qqbot/models/Commands/WoodenFish.java @@ -3,7 +3,15 @@ package com.yutou.qqbot.models.Commands; import com.yutou.qqbot.Annotations.UseModel; import com.yutou.qqbot.QQBotManager; import com.yutou.qqbot.models.Model; +import net.mamoe.mirai.contact.AudioSupported; import net.mamoe.mirai.event.events.MessageEvent; +import net.mamoe.mirai.message.data.Audio; +import net.mamoe.mirai.message.data.MessageChainBuilder; +import net.mamoe.mirai.message.data.OfflineAudio; +import net.mamoe.mirai.utils.ExternalResource; + +import java.io.File; +import java.util.Objects; import static com.yutou.qqbot.models.Model.QQGroupCommands.QQ_WOODEN; @@ -28,6 +36,13 @@ public class WoodenFish extends Model { public void onMessage(Long qq, MessageEvent event, boolean isGroup) { super.onMessage(qq, event, isGroup); if(msg.contains("地狱笑话")){ + File file=new File("muyu.wav"); + if(file.exists()) { + OfflineAudio audio = Objects.requireNonNull(event.getBot().getGroup(qq)).uploadAudio(ExternalResource.create(file)); + MessageChainBuilder builder = new MessageChainBuilder(); + builder.append(audio); + QQBotManager.getInstance().sendMessage(qq,builder); + } QQBotManager.getInstance().sendMessage(qq,"功德+1"); } } diff --git a/src/main/java/com/yutou/qqbot/utlis/AppTools.java b/src/main/java/com/yutou/qqbot/utlis/AppTools.java index 5638e74..1831d90 100644 --- a/src/main/java/com/yutou/qqbot/utlis/AppTools.java +++ b/src/main/java/com/yutou/qqbot/utlis/AppTools.java @@ -4,6 +4,7 @@ import com.alibaba.fastjson2.JSON; import com.yutou.qqbot.QQBotManager; import com.yutou.qqbot.interfaces.DownloadInterface; import com.yutou.qqbot.interfaces.ObjectInterface; +import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; @@ -231,4 +232,8 @@ public class AppTools { return null; } } + + public static String getMD5(String str){ + return DigestUtils.md5Hex(str); + } } diff --git a/src/main/java/com/yutou/qqbot/utlis/HttpTools.java b/src/main/java/com/yutou/qqbot/utlis/HttpTools.java index 4e57641..5aca45c 100644 --- a/src/main/java/com/yutou/qqbot/utlis/HttpTools.java +++ b/src/main/java/com/yutou/qqbot/utlis/HttpTools.java @@ -144,6 +144,13 @@ public class HttpTools { string = new StringBuilder(string.substring(1, string.length()).replaceAll(" ", "")); return string.toString(); } + public static String toUrlParams(Map map) { + StringBuilder builder=new StringBuilder(); + for (String key : map.keySet()) { + builder.append(key).append("=").append(map.get(key)).append("&"); + } + return builder.substring(0, builder.length() - 1); + } public static Map getUrlParams(String url) { Map map = new HashMap<>();