新增B站AI总结功能

This commit is contained in:
Yutou 2024-01-17 15:12:12 +08:00
parent 3437a5386f
commit 9ea1450066
9 changed files with 184 additions and 13 deletions

View File

@ -6,7 +6,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class QQBotApplication {
public static final String version="QQBot v.1.5.2";
public static final String version="QQBot v.1.6";
public static void main(String[] args) {
System.out.println("version = " + version);
SpringApplication.run(QQBotApplication.class, args);

View File

@ -44,7 +44,7 @@ public class QQBotManager {
new Thread(new Runnable() {
private void reset() {
try {
Log.i("QQBot","签名加密服务未启动,1分钟后重试");
Log.i("QQBot", "签名加密服务未启动,1分钟后重试");
Thread.sleep(60 * 1000);
init();
} catch (InterruptedException e) {

View File

@ -2,7 +2,9 @@ package com.yutou.qqbot.bilibili;
import com.alibaba.fastjson2.JSONObject;
import com.yutou.qqbot.QQBotManager;
import com.yutou.qqbot.models.BiliBili.BiliVideo;
import com.yutou.qqbot.utlis.AppTools;
import com.yutou.qqbot.utlis.BiliBiliWbiSign;
import com.yutou.qqbot.utlis.HttpTools;
import javax.net.ssl.HttpsURLConnection;
@ -135,8 +137,51 @@ public class BiliBiliAppUtils {
});
return task;
}
public static String getVideoAI(String url){
if(url.startsWith("https://b23.tv")){
url=b23ToUrl(url);
}
JSONObject videoInfo = new BiliVideo().getVideoInfo(url);
if(videoInfo==null){
return null;
}
String cid = videoInfo.getJSONObject("data").getString("cid");
TreeMap<String,String> body=new TreeMap<>();
body.put("cid",cid);
body.put("up_mid",videoInfo.getJSONObject("data").getJSONObject("owner").getString("mid"));
body.put("bvid",videoInfo.getJSONObject("data").getString("bvid"));
BiliBiliWbiSign.getWbiSign(body);
JSONObject object = BiliBiliUtils.getInstance(QQBotManager.defQQ).http_get("https://api.bilibili.com/x/web-interface/view/conclusion/get"+"?"+HttpTools.toUrlParams(body));
if(object.getInteger("code")==0){
if(object.getJSONObject("data").getInteger("code")==0) {
return object.getJSONObject("data").getJSONObject("model_result").getString("summary");
}else{
return "没得省流";
}
}
return null;
}
public static String b23ToUrl(String url){
try {
HttpsURLConnection connection = BiliBiliUtils.getInstance(QQBotManager.defQQ).getBiliHttpGet(url, BiliBiliUtils.getInstance(QQBotManager.defQQ).getCookie());
connection.setInstanceFollowRedirects(false);
connection.connect();
if(connection.getResponseCode()==302){
connection.setConnectTimeout(5000);
return connection.getHeaderField("Location");
}
return connection.getURL().toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) throws Exception {
System.out.println(new BiliBiliAppUtils(QQBotManager.defQQ).startAppTask().toMessageFormat());
BiliBiliWbiSign.updateRawWbiKey();
String url="https://b23.tv/NtqDorB?share_medium=android&share_source=qq&bbid=XUDCA4BDD60B5853ACDC17794BFAEF91F874A&ts=1705470976484";
// url="https://www.bilibili.com/video/BV1fw411E75p/?buvid=XUDCA4BDD60B5853ACDC17794BFAEF91F874A&from_spmid=tm.recommend.0.0&is_story_h5=false&mid=7G8S%2B4e7nx6XSaU3oMQKXA%3D%3D&p=1&plat_id=116&share_from=ugc&share_medium=android&share_plat=android&share_session_id=ab27db6e-47a5-43b5-b0ec-b027bcfdeccc&share_source=QQ&share_tag=s_i&spmid=main.ugc-video-detail.0.0&timestamp=1705470976&unique_k=NtqDorB&up_id=1156809979";
String ai = getVideoAI(url);
System.out.println("ai = " + ai);
}
}

View File

@ -40,7 +40,7 @@ public class BiliBiliUtils {
public synchronized JSONObject http_get(String url) {
try {
// Log.i("调用url = "+url);
Log.i("调用url = "+url);
HttpsURLConnection connection = getBiliHttpGet(url, getCookie());
BufferedInputStream stream = new BufferedInputStream(connection.getInputStream());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

View File

@ -67,12 +67,12 @@ public class BiliBiliLive extends Model {
builder.append("BiliLiveSign").append(id).append(":").append(sign).append("\n");
}
QQBotManager.getInstance().sendMessage(sendQQ, builder.toString());
BiliBiliAppUtils appUtils = new BiliBiliAppUtils(QQBotManager.defQQ);
/* BiliBiliAppUtils appUtils = new BiliBiliAppUtils(QQBotManager.defQQ);
AppUserTask oldTask = appUtils.startAppTask();
AppUserTask newTask = appUtils.getTaskProgress();
builder = new StringBuilder();
builder.append("执行APP任务").append("\n").append(AppUserTask.toMessageFormat(oldTask, newTask));
QQBotManager.getInstance().sendMessage(sendQQ, builder.toString());
QQBotManager.getInstance().sendMessage(sendQQ, builder.toString());*/
}
@Override

View File

@ -7,10 +7,9 @@ import com.yutou.qqbot.QQBotManager;
import com.yutou.qqbot.bilibili.*;
import com.yutou.qqbot.interfaces.ObjectInterface;
import com.yutou.qqbot.models.Model;
import com.yutou.qqbot.utlis.AppTools;
import com.yutou.qqbot.utlis.ConfigTools;
import com.yutou.qqbot.utlis.HttpTools;
import com.yutou.qqbot.utlis.StringUtils;
import com.yutou.qqbot.utlis.*;
import net.mamoe.mirai.event.events.MessageEvent;
import net.mamoe.mirai.message.data.QuoteReply;
import java.io.File;
import java.io.FileWriter;
@ -39,7 +38,7 @@ public class BiliVideo extends Model {
@Override
public boolean isUserPublic() {
return false;
return true;
}
@Override
@ -52,6 +51,53 @@ public class BiliVideo extends Model {
return "B站视频下载";
}
@Override
public void onMessage(Long qq, MessageEvent event, boolean isGroup) {
super.onMessage(qq, event, isGroup);
if (event.getMessage().serializeToMiraiCode().contains("mirai:app")) {
int id = event.getSource().getIds()[0];
String json = event.getMessage().get(1).contentToString();
System.out.println("id = " + id);
System.out.println("json = " + json);
RedisTools.set("qq_msg_id_" + id, json, 60 * 60 * 2);
} else if (isAt()) {
if (event.getMessage().get(1) instanceof QuoteReply) {
int id = ((QuoteReply) event.getMessage().get(1)).getSource().getIds()[0];
if (msg.contains("省流") || msg.contains("总结")) {
onAIVideo(id);
}
}
}
}
private String onAIVideo(int id) {
String string = RedisTools.get("qq_msg_id_" + id);
//RedisTools.remove("qq_msg_id_"+id);
String url = null;
if (StringUtils.isEmpty(string)) {
url = ((QuoteReply) event.getMessage().get(1)).getSource().getOriginalMessage().contentToString();
} else {
JSONObject json = JSONObject.parseObject(string);
if (json.containsKey("ver")) {
url = json.getJSONObject("meta").getJSONObject("detail_1").getString("qqdocurl");
}
}
if (StringUtils.isEmpty(url)) {
return "地址不正确";
}
if (url.startsWith("BV")) {
url = "https://www.bilibili.com/video/" + url.trim();
}
if (!url.startsWith("https://www.bilibili.com/video/") && !url.startsWith("https://b23.tv")) {
return "这是B站吗?";
}
String ai = BiliBiliAppUtils.getVideoAI(url.trim());
if (!StringUtils.isEmpty(ai)) {
return ai;
}
return "省流失败";
}
public void downVideo(String url) {
downVideo(url, true, false);
}
@ -347,7 +393,6 @@ public class BiliVideo extends Model {
return json;
}
public static void main(String[] args) {
BiliVideo video = new BiliVideo(QQBotManager.defQQ);
JSONObject login = new BiliLogin(QQBotManager.defQQ).login();

View File

@ -35,6 +35,9 @@ public class BaiduGPT extends Model {
BaiduGPTManager.getManager().clear();
QQBotManager.getInstance().sendMessage(qq, "已经失忆捏");
} else if (isAt()) {
if(msg.contains("省流")|| msg.contains("总结")){
return;
}
ResponseMessage message = BaiduGPTManager.getManager().sendMessage(String.valueOf(user), msg.replace("@2476945931", "").trim());
QQBotManager.getInstance().sendMessage(qq, message.getResult());
}

View File

@ -3,6 +3,7 @@ package com.yutou.qqbot.models;
import com.yutou.qqbot.QQBotManager;
import com.yutou.qqbot.QQNumberManager;
import com.yutou.qqbot.interfaces.ModelInterface;
import com.yutou.qqbot.utlis.ConfigTools;
import net.mamoe.mirai.Bot;
import net.mamoe.mirai.event.events.GroupMessageEvent;
import net.mamoe.mirai.event.events.MessageEvent;
@ -64,6 +65,7 @@ public abstract class Model implements ModelInterface {
public static List<Class<?>> classList;
long group;
public MessageEvent event;
static {
classList = new ArrayList<>();
@ -82,6 +84,7 @@ public abstract class Model implements ModelInterface {
public Long user;
public void onMessage(Long qq, MessageEvent event, boolean isGroup) {
this.event=event;
msg = event.getMessage().contentToString();
msg = msg.replace("", "!").trim();
this.isGroup = isGroup;
@ -121,6 +124,6 @@ public abstract class Model implements ModelInterface {
return chain;
}
public boolean isAt(){
return msg.contains("@2476945931");
return msg.contains("@"+ ConfigTools.load(ConfigTools.CONFIG,"qq_number",String.class));
}
}

View File

@ -0,0 +1,75 @@
package com.yutou.qqbot.utlis;
import com.alibaba.fastjson2.JSONObject;
import com.yutou.qqbot.QQBotManager;
import com.yutou.qqbot.bilibili.BiliBiliUtils;
import java.nio.charset.StandardCharsets;
import java.util.TreeMap;
public class BiliBiliWbiSign {
private static final byte[] MIXIN_KEY_ENC_TAB = {
46, 47, 18, 2, 53, 8, 23, 32, 15, 50, 10, 31, 58, 3, 45, 35, 27, 43, 5, 49, 33, 9, 42,
19, 29, 28, 14, 39, 12, 38, 41, 13, 37, 48, 7, 16, 24, 55, 40, 61, 26, 17, 0, 1, 60,
51, 30, 4, 22, 25, 54, 21, 56, 59, 6, 63, 57, 62, 11, 36, 20, 34, 44, 52
};
private static String rawWbiKey;
public static TreeMap<String, String> getWbiSign(TreeMap<String, String> body) {
if (rawWbiKey == null) {
updateRawWbiKey();
try {
Thread.sleep(300);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return getWbiSign(body);
}
body.put("wts", String.valueOf(System.currentTimeMillis() / 1000));
body.put("wts", "1705394671");
String params = HttpTools.toUrlParams(body);
System.out.println("params = " + params);
body.put("w_rid", AppTools.getMD5(params + genMixinKey(rawWbiKey)));
return body;
}
private static String genMixinKey(String rawWbiKey) {
byte[] rawBytes = rawWbiKey.getBytes(StandardCharsets.UTF_8);
byte[] mixinKey = new byte[32];
for (int i = 0; i < 32; i++) {
mixinKey[i] = rawBytes[MIXIN_KEY_ENC_TAB[i]];
}
return new String(mixinKey);
}
public static void updateRawWbiKey() {
JSONObject loginInfo = new BiliBiliUtils(QQBotManager.defQQ).getLoginInfo();
if (loginInfo.getInteger("code") == -1) {
rawWbiKey = null;
return;
}
JSONObject wbi = loginInfo.getJSONObject("data").getJSONObject("wbi_img");
String imgKey = wbi.getString("img_url");
String subKey = wbi.getString("sub_url");
imgKey = imgKey.substring(imgKey.lastIndexOf("/") + 1).replace(".png", "");
subKey = subKey.substring(subKey.lastIndexOf("/") + 1).replace(".png", "");
rawWbiKey = imgKey + subKey;
System.out.println(rawWbiKey);
}
public static void main(String[] args) throws Exception {
//System.out.println(genMixinKey("7cd084941338484aae1ad9425b84077c4932caff0ff746eab6f01bf08b70ac45"));
rawWbiKey = "7cd084941338484aae1ad9425b84077c4932caff0ff746eab6f01bf08b70ac45";
TreeMap<String, String> json = new TreeMap<>();
json.put("bvid", "BV1L94y1H7CV");
json.put("cid", "1335073288");
json.put("up_mid", "297242063");
json.put("web_location", "333.788");
//updateRawWbiKey();
TreeMap<String, String> sign = getWbiSign(json);
System.out.println(sign);
//https://api.bilibili.com/x/web-interface/view/conclusion/get?bvid=BV1L94y1H7CV&cid=1335073288&up_mid=297242063&web_location=333.788&w_rid=a5d90f60ac6b6b6fc9d49be3ba3fee53&wts=1705394671
//updateRawWbiKey();
}
}