新增漫画购买功能

This commit is contained in:
Yutousama 2022-04-08 12:59:47 +08:00
parent c04ada610f
commit 4cc5567568
4 changed files with 245 additions and 24 deletions

View File

@ -1,16 +1,173 @@
package com.yutou.qqbot.bilibili;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yutou.qqbot.interfaces.ObjectInterface;
import com.yutou.qqbot.utlis.HttpTools;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class BiliBiliManga {
public static JSONObject sign(){
JSONObject body=new JSONObject();
body.put("platform","android");
public static JSONObject sign() {
JSONObject body = new JSONObject();
body.put("platform", "android");
return BiliBiliUtils.http_post("https://manga.bilibili.com/twirp/activity.v1.Activity/ClockIn", HttpTools.toUrlParams(body));
}
private static JSONObject getListProductDate() {
return BiliBiliUtils.http_post("https://manga.bilibili.com/twirp/pointshop.v1.Pointshop/ListProduct", "");
}
private static boolean isPayMission = false;
private static final Product missionProduct = new Product();
private static Timer mission = null;
private final List<ObjectInterface> anInterface = new ArrayList<>();
public void addInterface(ObjectInterface objectInterface) {
anInterface.add(objectInterface);
}
public static boolean isPayMission() {
return isPayMission;
}
public static String getMission() {
return missionProduct.toString();
}
public static List<Product> getListProduct() {
List<Product> list = new ArrayList<>();
JSONObject product = getListProductDate();
if (product.getInteger("code") == 0) {
JSONArray array = product.getJSONArray("data");
for (Object o : array) {
JSONObject data = (JSONObject) o;
Product item = new Product();
item.setId(data.getInteger("id"));
item.setTitle(data.getString("title"));
item.setAmount(data.getInteger("amount"));
item.setRemain_amount(data.getInteger("remain_amount"));
item.setReal_cost(data.getInteger("real_cost"));
list.add(item);
}
}
return list;
}
private int getMyPoint() {
JSONObject user = BiliBiliUtils.http_post("https://manga.bilibili.com/twirp/pointshop.v1.Pointshop/GetUserPoint", "");
if (user != null && user.getInteger("code") == 0) {
return user.getJSONObject("data").getInteger("point");
}
return 0;
}
public JSONObject startPayMission(int id, int num) {
JSONObject json = new JSONObject();
if (isPayMission) {
json.put("code", 2);
json.put("msg", "任务正在进行:" + missionProduct);
return json;
}
int userPoint = getMyPoint();
List<Product> list = getListProduct();
Product item = null;
for (Product product : list) {
if (product.getId() == id) {
item = product;
break;
}
}
if (item == null) {
json.put("code", -1);
json.put("msg", "未找到商品可能id有误 id:" + id);
return json;
}
if (num == -1) {
num = 99999;
}
int userPointNum = userPoint / item.getReal_cost();
num = Math.min(num, userPointNum);
if (num < item.getRemain_amount()) {
num = item.getRemain_amount();
}
if (num == 0) {
json.put("code", 3);
json.put("msg", "商品无货,正在抢购");
return json;
}
JSONObject data = new JSONObject();
data.put("product_id", id);
data.put("product_num", num);
data.put("point", num * item.getReal_cost());
startPayMission(data);
isPayMission = true;
json.put("code", 0);
json.put("msg", "任务创建成功");
return json;
}
private void startPayMission(JSONObject data) {
if (mission != null) {
mission.cancel();
}
mission = new Timer();
mission.schedule(new TimerTask() {
@Override
public void run() {
JSONObject post = BiliBiliUtils.http_post("https://manga.bilibili.com/twirp/pointshop.v1.Pointshop/Exchange", HttpTools.toUrlParams(data));
if (post == null) {
for (ObjectInterface objectInterface : anInterface) {
objectInterface.out("网络请求失败,请查看日志");
}
cancel();
return;
}
if (post.getInteger("code") == 0) {
for (ObjectInterface mInt : anInterface) {
mInt.out("兑换成功,任务已取消");
}
isPayMission = false;
cancel();
} else {
for (ObjectInterface objectInterface : anInterface) {
objectInterface.out("[" + post.getInteger("code") + "]" + post.getString("msg"));
}
}
}
}, 0, 500);
}
public void stopPayMission() {
if (isPayMission) {
isPayMission = false;
if (mission != null) {
mission.cancel();
}
}
}
public static void main(String[] args) {
BiliBiliManga.sign();
}
@Data
public static class Product {
String title;
int id;
int remain_amount;
int real_cost;
int amount;
@Override
public String toString() {
return "[" + id + "]" + title + " (" + remain_amount + "/" + amount + ") $" + real_cost;
}
}
}

View File

@ -48,6 +48,10 @@ public class BiliLogin {
JSONObject tmp=BiliBiliUtils.http_post(_url,"");
System.out.println("tmp = " + tmp);
if(tmp==null){
cancel();
return;
}
String sid=tmp.getString("cookie");
sid=sid.split("sid=")[1];
sid=sid.split(";")[0];

View File

@ -39,12 +39,13 @@ public abstract class Model implements ModelInterface {
public static class QQFromCommands {
public static final String TURNIP_PROPHET = "大头菜";
public static final String TSDM_PAY = "!tsdm";
public static final String TSDM_SIGN = "!tsdm签到";
public static final String BAIDU_DOWN = "!bd";
public static final String ROUTER_ADD = "!添加设备";
public static final String ROUTER_DEL = "!删除设备";
public static final String BT_DOWNLOAD = "下载bt";
public static final String BILI_MANGA_SIGN="!b站漫画签到";
public static final String BILI_MANGA_PAY="!b站漫画积分兑换";
public static final String BILI_MANGA_PAY_STOP="!b站漫画积分兑换取消";
}
@ -65,7 +66,7 @@ public abstract class Model implements ModelInterface {
public void onMessage(Long qq, MessageEvent event, boolean isGroup) {
msg = event.getMessage().contentToString();
msg = msg.replace("", "!");
msg = msg.replace("", "!").trim();
this.isGroup = isGroup;
if (isGroup) {
user=event.getSource().getFromId();

View File

@ -3,13 +3,14 @@ package com.yutou.qqbot.models.WebSign;
import com.yutou.qqbot.QQBotManager;
import com.yutou.qqbot.bilibili.BiliBiliManga;
import com.yutou.qqbot.bilibili.BiliLogin;
import com.yutou.qqbot.interfaces.ObjectInterface;
import com.yutou.qqbot.models.Model;
import com.yutou.qqbot.utlis.QRCodeUtils;
import net.mamoe.mirai.event.events.MessageEvent;
import java.io.File;
public class BiliBiliMangeSign extends Model {
public class BiliBiliMangeSign extends Model {
@Override
public boolean isUserPublic() {
return false;
@ -18,40 +19,98 @@ public class BiliBiliMangeSign extends Model {
@Override
public String[] getUsePowers() {
return new String[]{
QQFromCommands.BILI_MANGA_SIGN
QQFromCommands.BILI_MANGA_SIGN,
QQFromCommands.BILI_MANGA_PAY,
QQFromCommands.BILI_MANGA_PAY_STOP
};
}
@Override
public void onMessage(Long qq, MessageEvent event, boolean isGroup) {
super.onMessage(qq, event, isGroup);
if(msg.equals(QQFromCommands.BILI_MANGA_SIGN)){
if (msg.equals(QQFromCommands.BILI_MANGA_SIGN)) {
String msg;
if(new BiliLogin().testLogin()){
if(BiliBiliManga.sign()==null){
msg="B站漫画已经签到过了";
}else{
msg="B站漫画签到完成";
if (new BiliLogin().testLogin()) {
if (BiliBiliManga.sign() == null) {
msg = "B站漫画已经签到过了";
} else {
msg = "B站漫画签到完成";
}
QQBotManager.getInstance().sendMessage(qq,msg);
}else{
QQBotManager.getInstance().sendMessage(qq, msg);
} else {
String url = new BiliLogin().login().getString("url");
File code = QRCodeUtils.createQRCode("bili_login",url);
QQBotManager.getInstance().sendMessage(code,qq,"B站未登录,请扫码登陆后再试");
File code = QRCodeUtils.createQRCode("bili_login", url);
QQBotManager.getInstance().sendMessage(code, qq, "B站未登录,请扫码登陆后再试");
}
} else if (msg.equals(QQFromCommands.BILI_MANGA_PAY)) {
StringBuilder builder = new StringBuilder();
if (BiliBiliManga.isPayMission()) {
builder.append("当前已有执行任务:").append(BiliBiliManga.getMission());
QQBotManager.getInstance().sendMessage(qq, builder.toString());
return;
}
builder.append("-------商城列表-------").append("\n");
for (BiliBiliManga.Product product : BiliBiliManga.getListProduct()) {
builder.append(product).append("\n");
}
builder.append("-------------------").append("\n");
builder.append("使用方法:")
.append(QQFromCommands.BILI_MANGA_PAY)
.append(" id 数量[num]")
.append("\n");
builder.append("参数说明:")
.append("\n")
.append("id=物品id")
.append("\n")
.append("num=物品数量,缺省或-1为全部")
.append("\n");
QQBotManager.getInstance().sendMessage(qq, builder.toString());
}else if(msg.equals(QQFromCommands.BILI_MANGA_PAY_STOP)){
new BiliBiliManga().stopPayMission();
} else if (msg.startsWith(QQFromCommands.BILI_MANGA_PAY)) {
msg = msg.replace(QQFromCommands.BILI_MANGA_PAY, "");
String[] message = msg.split(" ");
int num = 0;
int id = 0;
if (message.length < 2) {
try {
id = Integer.parseInt(message[0]);
} catch (Exception e) {
QQBotManager.getInstance().sendMessage(qq, "商城id错误");
return;
}
} else {
num = Integer.parseInt(message[1]);
}
if (id == 0) {
QQBotManager.getInstance().sendMessage(qq, "商城id错误");
return;
}
BiliBiliManga manga = new BiliBiliManga();
manga.addInterface(new ObjectInterface() {
@Override
public void out(String data) {
super.out(data);
if(!data.startsWith("[2]")||!data.startsWith("[4]")){
QQBotManager.getInstance().sendMessage(qq,"任务异常:"+data);
manga.stopPayMission();
}
}
});
QQBotManager.getInstance().sendMessage(qq, manga.startPayMission(id, num).getString("msg"));
}
}
@Override
public void onTime(String time) {
super.onTime(time);
if("00:01:00".equals(time)){
if(new BiliLogin().testLogin()){
if ("00:01:00".equals(time)) {
if (new BiliLogin().testLogin()) {
String msg;
if(BiliBiliManga.sign()==null){
msg="B站漫画已经签到过了";
}else{
msg="B站漫画签到完成";
if (BiliBiliManga.sign() == null) {
msg = "B站漫画已经签到过了";
} else {
msg = "B站漫画签到完成";
}
QQBotManager.getInstance().sendMessage(msg);
}