效果图:
需要的Model层:
package Modio;import com.sun.xml.internal.bind.v2.model.core.ID;
//import lombok.Data;//获取余下的
//@Data
public class GptBalanceModel {private Integer id;private String sum;private String use;private String balance;private String date; //放入时间private String key;//秘钥private String start;//状态public GptBalanceModel() {}public GptBalanceModel(String key) {this.key = key;}@Overridepublic String toString() {return "GptBalanceModel{" +"id=" + id +", sum='" + sum + '\'' +", use='" + use + '\'' +", balance='" + balance + '\'' +", date='" + date + '\'' +", key='" + key + '\'' +", start='" + start + '\'' +'}';}public GptBalanceModel(String sum, String use, String balance) {this.sum = sum;this.use = use;this.balance = balance;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getSum() {return sum;}public void setSum(String sum) {this.sum = sum;}public String getUse() {return use;}public void setUse(String use) {this.use = use;}public String getBalance() {return balance;}public void setBalance(String balance) {this.balance = balance;}public String getDate() {return date;}public void setDate(String date) {this.date = date;}public String getKey() {return key;}public void setKey(String key) {this.key = key;}public String getStart() {return start;}public void setStart(String start) {this.start = start;}
}
主方法:
package ljx;import java.io.IOException;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;import Modio.GptBalanceModel;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;public class Main {public static void main(String[] args) {List<GptBalanceModel> balanceList=new ArrayList<>();balanceList.add(new GptBalanceModel("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"));balanceList.add(new GptBalanceModel("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"));balanceList.add(new GptBalanceModel("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"));balanceList.add(new GptBalanceModel("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"));balanceList.add(new GptBalanceModel("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"));balanceList.add(new GptBalanceModel("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"));balanceList.add(new GptBalanceModel("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"));balanceList.add(new GptBalanceModel("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"));balanceList.add(new GptBalanceModel("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"));balanceList.add(new GptBalanceModel("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"));Main main=new Main();List<GptBalanceModel> balance = main.gptFromBalancesList(balanceList);System.out.println("输出测试");for (GptBalanceModel gptBalanceModel : balance) {System.out.println("key:"+gptBalanceModel.getKey());System.out.println("一共:"+gptBalanceModel.getSum());System.out.println("已使用:"+gptBalanceModel.getUse());System.out.println("余额:"+gptBalanceModel.getBalance());System.out.println("\n");}
// System.out.println("Key:"+balance);
// System.out.println(balance);}public List<GptBalanceModel> gptFromBalancesList(List<GptBalanceModel> balanceList) {ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); //设置线程池线程池大小用当前系统能给的大小来代替for (GptBalanceModel gptBalance : balanceList) {//进行for循环查询遍历executor.submit(() -> getFromBalance(gptBalance));}executor.shutdown(); //等待线程执行完成try {executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);} catch (InterruptedException e) {e.printStackTrace();}return balanceList; //返回数据}
// public List<GptBalanceModel> gptFromBalancesList(List<GptBalanceModel> balanceList){
//
//
// for (GptBalanceModel gptBalance : balanceList) {
// getFromBalance(gptBalance);
// }
// return balanceList;
// }String gptUrl="https://openai.1rmb.tk"; //反向代理访问地址访问gpt官网public GptBalanceModel getFromBalance(GptBalanceModel gptBalance){String url = gptUrl+"/v1/dashboard/billing/subscription";String apiKey = gptBalance.getKey(); //设置秘钥try {
// 拿到当前剩余余下的钱Document doc = Jsoup.connect(url).header("Authorization", "Bearer " + apiKey).header("Content-Type", "application/json").header("Connection", "keep-alive").ignoreContentType(true).get();// 查找当前已经使用多少String startDate = LocalDate.now().minusDays(99).toString();String endDate = LocalDate.now().plusDays(1).toString();String billingUrl = gptUrl+"/v1/dashboard/billing/usage?start_date=" + startDate + "&end_date=" + endDate;Document billingDoc = Jsoup.connect(billingUrl).header("Authorization", "Bearer " + apiKey).header("Content-Type", "application/json").header("Connection", "keep-alive").ignoreContentType(true).get();// 拿到一共剩余String html = doc.body().text(); // 获取HTML代码JSONObject json =JSONObject.parseObject(html); // 将HTML代码转换为JSON对象Double hardLimitUsd = Double.parseDouble(json.getString("hard_limit_usd")); // 获取hard_limit_usd的值// 拿到已经使用的String html1 = billingDoc.body().text(); // 获取HTML代码JSONObject json1 = (JSONObject) JSONObject.parse(html1); // 将HTML代码转换为JSON对象Double total_usage = Double.parseDouble(json1.getString("total_usage")) / 100.0;// 保留两位小数String sum = String.format("%.3f", hardLimitUsd);String use = String.format("%.3f", total_usage);String balance= String.format("%.3f", hardLimitUsd-total_usage);
// System.out.println("一共: " + sum);
// System.out.println("已使用:"+use);
// System.out.println("剩余金额:"+balance);gptBalance.setSum(sum);//一共gptBalance.setUse(use);//已使用gptBalance.setBalance(balance);//剩余金额return gptBalance;} catch (IOException e) {e.printStackTrace();System.out.println("余额更新失败");return gptBalance;}}
}
需要的jar包:
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.20</version><scope>provided</scope></dependency><dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>1.11.2</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.47</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.1</version><scope>test</scope></dependency><dependency><groupId>org.thymeleaf</groupId><artifactId>thymeleaf</artifactId><version>3.0.12.RELEASE</version></dependency><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>RELEASE</version><scope>compile</scope></dependency><dependency><groupId>org.jdom</groupId><artifactId>jdom2</artifactId><version>2.0.6</version></dependency>