新增漫画购买功能
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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];
|
||||
|
||||
Reference in New Issue
Block a user