175 lines
5.8 KiB
Java
175 lines
5.8 KiB
Java
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");
|
||
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 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;
|
||
}
|
||
|
||
public static 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+" 兑换数量:"+missionProduct.getPayAmount());
|
||
return json;
|
||
}
|
||
int userPoint = getMyPoint();
|
||
List<Product> list = getListProduct();
|
||
missionProduct = null;
|
||
for (Product product : list) {
|
||
if (product.getId() == id) {
|
||
missionProduct = product;
|
||
break;
|
||
}
|
||
}
|
||
if (missionProduct == null) {
|
||
json.put("code", -1);
|
||
json.put("msg", "未找到商品,可能id有误 id:" + id);
|
||
return json;
|
||
}
|
||
if (num == -1) {
|
||
num = 99999;
|
||
}
|
||
int userPointNum = userPoint / missionProduct.getReal_cost();
|
||
num = Math.min(num, userPointNum);
|
||
if (num < missionProduct.getRemain_amount()) {
|
||
num = missionProduct.getRemain_amount();
|
||
}
|
||
|
||
missionProduct.setPayAmount(num);
|
||
JSONObject data = new JSONObject();
|
||
data.put("product_id", id);
|
||
data.put("product_num", num);
|
||
data.put("point", num * missionProduct.getReal_cost());
|
||
startPayMission(data);
|
||
isPayMission = true;
|
||
|
||
if (num == 0) {
|
||
json.put("code", 3);
|
||
json.put("msg", "商品无货,正在抢购");
|
||
}else {
|
||
json.put("code", 0);
|
||
json.put("msg", "任务创建成功:"+missionProduct+" 兑换数量:"+num);
|
||
}
|
||
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, 1000);
|
||
}
|
||
|
||
public void stopPayMission() {
|
||
if (isPayMission) {
|
||
isPayMission = false;
|
||
if (mission != null) {
|
||
mission.cancel();
|
||
}
|
||
}
|
||
}
|
||
|
||
public static void main(String[] args) {
|
||
}
|
||
|
||
@Data
|
||
public static class Product {
|
||
String title;
|
||
int id;
|
||
int remain_amount;
|
||
int real_cost;
|
||
int amount;
|
||
int payAmount;
|
||
|
||
@Override
|
||
public String toString() {
|
||
return "[ " + id + " ] " + title + " (" + remain_amount + "/" + amount + ") $" + real_cost;
|
||
}
|
||
}
|
||
}
|