Skip to content
This repository has been archived by the owner on Jul 6, 2022. It is now read-only.

Commit

Permalink
Release 0.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
lostkira committed Mar 30, 2021
1 parent 8ade524 commit 54ee237
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 72 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#更新日志

##[0.1.2] - 2021-3-30
###新增
* 插件载入时将检查是否正确填写了API Key
###优化
* 现在API Key以外部文件的方式存储了
* 请求错误现在将打印更详细的报告
* 重写了README.md

##[0.1.1] - 2021-3-29
###新增
* API发生请求错误时,将通过Mirai打印相关提示
###变更
* Mirai-Console版本更新为2.5.0

## [0.1.0] - 2021-3-23
###新增
* 完成了核心功能
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,30 @@
# 塔科夫市场插件 Tarkov Tools
一个Mirai-Console插件,用于从[Tarkov Market](https://tarkov-market.com)上抓取市场价格数据,并响应指令进行价格信息转发

A mirai-console plugin used to collect data from [Tarkov Market](https://tarkov-market.com) and forwarding result to QQ groups
A mirai-console plugin used to collect data from [Tarkov Market](https://tarkov-market.com) and forwarding result to QQ groups

## 环境
* JRE: 11+
* Mirai-Console: 2.5.0
* MCL

## 构建
Clone并通过IntelliJ IDEA导入本项目,执行buildPlugin任务即可

## 安装
1. 自行构建本项目或在Releases页面下载预构建的版本
2. 将插件放入Mirai-Console的plugins目录
3. 启动Mirai-Console
4. **第一次使用启动后需要在config配置文件中填写[Tarkov Market API](https://tarkov-market.com/dev/api)** KEY
5. 在Mirai-Console中配置命令执行权限

## 使用
目前只有一个主要命令
### /eft search <搜索关键字>(中文关键字)

返回[Tarkov Market](https://tarkov-market.com)上前三个搜索结果的市场价格和最高商人收购价格

## 关于
本项目跟随[Mirai](https://github.com/mamoe/mirai) 使用 `AGPLv3` 开源

码力不强,献丑了。欢迎提Issue,也欢迎各路大佬优化代码提交Pull Request
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group 'tech.ezrealc'
version '0.1.1'
version '0.1.2'

repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
rootProject.name = 'TarkovTools'
rootProject.name = 'Mirai-TarkovMarket-Plugin'

10 changes: 6 additions & 4 deletions src/main/java/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ public interface HttpClient {

@Request(
url = "${url}${uid}",
headers = "x-api-key:fREhtc8ysOAI2f5j",
headers = "x-api-key:${apiKey}",
dataType = "text"
)
String getByUid(@DataVariable("url")String url, @DataVariable("uid")String uid);
String getByUid(@DataVariable("apiKey")String apiKey, @DataVariable("url")String url,
@DataVariable("uid")String uid);

@Post(
url = "https://tarkov-market.com/api/v1/item",
headers = {
"x-api-key:fREhtc8ysOAI2f5j",
"x-api-key:${apiKey}",
"Content-Type:application/json"
},
dataType = "text"
)
String getByKeyword(@JSONBody("q")String keyword, @JSONBody("lang")String language);
String getByKeyword(@DataVariable("apiKey")String apiKey, @JSONBody("q")String keyword,
@JSONBody("lang")String language);
}
4 changes: 4 additions & 0 deletions src/main/java/ItemBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ public class ItemBean {
private String img;
private String imgBig;
private String reference;

public ItemBean () {
}

}
28 changes: 14 additions & 14 deletions src/main/java/ItemInfo.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
import com.dtflys.forest.exceptions.ForestNetworkException;
import com.dtflys.forest.http.ForestResponse;

import java.util.ArrayList;

public class ItemInfo {

public final ArrayList<ItemBean> itemBeanList;
ItemInfo(String uid){

ItemInfo (String apiKey, String uid) {
HttpClient httpClient = Utils.getHttpClient();
String result = null;
result = httpClient.getByUid("https://tarkov-market.com/api/v1/item?uid=", uid);
String result;
result = httpClient.getByUid(apiKey, "https://tarkov-market.com/api/v1/item?uid=", uid);
itemBeanList = Utils.parseItemBean(result);
}

ItemInfo(String keyword, String language){
ItemInfo (String apiKey, String keyword, String language) {
HttpClient httpClient = Utils.getHttpClient();
String result = null;
result = httpClient.getByKeyword(keyword, language);
String result;
result = httpClient.getByKeyword(apiKey, keyword, language);
itemBeanList = Utils.parseItemBean(result);
}

public String getNameByIndex(int index){
public String getNameByIndex (int index) {
return itemBeanList.get(index).getName();
}

public int getTraderPriceByIndex(int index){
public int getTraderPriceByIndex (int index) {
return itemBeanList.get(index).getTraderPrice();
}

public String getTraderPriceCurByIndex(int index){
public String getTraderPriceCurByIndex (int index) {
return itemBeanList.get(index).getTraderPriceCur();
}

public int getPriceByIndex(int index){
public int getPriceByIndex (int index) {
return itemBeanList.get(index).getPrice();
}

public String getTraderNameByIndex(int index){
public String getTraderNameByIndex (int index) {
return itemBeanList.get(index).getTraderName();
}

}
8 changes: 8 additions & 0 deletions src/main/java/PluginConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import net.mamoe.mirai.console.data.AutoSavePluginConfig
import net.mamoe.mirai.console.data.ValueDescription
import net.mamoe.mirai.console.data.value

object PluginConfig : AutoSavePluginConfig("config"){
@ValueDescription("tarkov-market.com提供的API Key")
var apiKey: String by value("")
}
92 changes: 55 additions & 37 deletions src/main/java/TarkovPluginMain.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import com.dtflys.forest.exceptions.ForestNetworkException
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.unregister
import net.mamoe.mirai.console.command.CommandSender
Expand All @@ -12,66 +13,83 @@ object TarkovPluginMain : KotlinPlugin(
) {
override fun onEnable() {
super.onEnable()
logger.info("Tarkov plugin loaded")

serverStatus.register()
help.register()
eft.register()
ServerStatus.register()
Help.register()
EFT.register()

PluginConfig.reload()

logger.info("Tarkov Market plugin loaded")

if (PluginConfig.apiKey.length != 16){
logger.warning("API Key可能没有正确配置,请检查配置文件")
}
}

override fun onDisable() {
super.onDisable()
serverStatus.unregister()
help.unregister()
eft.unregister()
ServerStatus.unregister()
Help.unregister()
EFT.unregister()
}

object serverStatus : SimpleCommand(
object ServerStatus : SimpleCommand(
TarkovPluginMain, "stat",
description = "服务器状态检查",
){
description = "Bot存活检查",
) {
@Handler
suspend fun CommandSender.handler(){
sendMessage("Bot正常响应\n作者: EzrealC")
suspend fun CommandSender.handler() {
sendMessage("Bot还活着\n作者: EzrealC")
}
}

object help : SimpleCommand(
object Help : SimpleCommand(
TarkovPluginMain, "?",
description = "帮助"
){
) {
@Handler
suspend fun CommandSender.handler(){
suspend fun CommandSender.handler() {
sendMessage("/stat 检查服务器状态\n/eft search 物品名关键字 检索物品价格")
}
}

object eft : CompositeCommand(
object EFT : CompositeCommand(
TarkovPluginMain, "eft"
){
) {
@SubCommand
suspend fun CommandSender.search(keyword:String){
sendMessage("正在查询中...")
val tempString = StringBuilder()
try {
val searchResult = ItemInfo(keyword, "cn")
if (searchResult.itemBeanList.size != 0){
for (index in searchResult.itemBeanList.indices){
tempString.appendLine("物品名:" + searchResult.getNameByIndex(index))
tempString.appendLine("市场价格:" + searchResult.getPriceByIndex(index) + searchResult.getTraderPriceCurByIndex(index))
tempString.appendLine("商人收购价格:" + searchResult.getTraderNameByIndex(index)+ " " +searchResult.getTraderPriceByIndex(index) +
searchResult.getTraderPriceCurByIndex(index))
tempString.append("\n")
suspend fun CommandSender.search(keyword: String) {
if (PluginConfig.apiKey.length == 16) {
sendMessage("正在查询中...")
val tempString = StringBuilder()
try {
val searchResult = ItemInfo(PluginConfig.apiKey, keyword, "cn")
if (searchResult.itemBeanList.size != 0) {
for (index in searchResult.itemBeanList.indices) {
tempString.appendLine("物品名:" + searchResult.getNameByIndex(index))
tempString.appendLine("市场价格:" + searchResult.getPriceByIndex(index) + searchResult.getTraderPriceCurByIndex(index))
tempString.appendLine(
"商人收购价格:" + searchResult.getTraderNameByIndex(index) + " " + searchResult.getTraderPriceByIndex(index) +
searchResult.getTraderPriceCurByIndex(index)
)
tempString.append("\n")
}
tempString.deleteRange(tempString.length - 2, tempString.length)
} else {
tempString.append("!没有搜索到相关物品!")
}
tempString.deleteAt(tempString.length - 1)
tempString.deleteAt(tempString.length - 1)
}else{
tempString.append("!没有搜索到相关物品!")
} catch (e : ForestNetworkException){
tempString.appendLine("请求出错啦!")
tempString.appendLine("状态码: ${e.statusCode}")
tempString.append(e.response.content)
}catch (e: Exception) {
tempString.append("!请求出错了,请再试一次吧~")
}
} catch (e: Exception) {
tempString.append("!请求出错了,请再试一次吧~")
sendMessage(String(tempString))
} else {
logger.error("API Key设置错误")
sendMessage("!API Key设置错误,请联系管理员!")
}
sendMessage(String(tempString))
}
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.ArrayList;

public class Utils {
public static final String uid = "5ac84494-465a-424a-b36e-fe22869ba5ec";

public static HttpClient getHttpClient(){
ForestConfiguration forestConfiguration = ForestConfiguration.configuration();
Expand All @@ -21,11 +20,12 @@ public static HttpClient getHttpClient(){
public static ArrayList<ItemBean> parseItemBean(String result){
JsonArray jsonArray = JsonParser.parseString(result).getAsJsonArray();
Gson gson = new Gson();
ArrayList<ItemBean> itemBeanList = new ArrayList<ItemBean>();
ArrayList<ItemBean> itemBeanList = new ArrayList<>();
for (JsonElement item : jsonArray){
ItemBean itemBean = gson.fromJson(item, ItemBean.class);
itemBeanList.add(itemBean);
}
return itemBeanList;
}

}
5 changes: 3 additions & 2 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
id: "tech.ezrealc.tarkov-plugin"
version: "0.1.1"
id: "tech.ezrealc.mirai-tarkovmarket-plugin"
name: "mirai-tarkovmarket-plugin"
version: "0.1.2"
14 changes: 6 additions & 8 deletions src/test/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
import com.dtflys.forest.config.ForestConfiguration;
import com.dtflys.forest.converter.json.ForestGsonConverter;
import com.dtflys.forest.exceptions.ForestNetworkException;
import com.dtflys.forest.http.ForestResponse;
import com.dtflys.forest.ssl.SSLUtils;
import com.google.gson.*;

import java.util.ArrayList;

public class Main {
private static final String url = "https://tarkov-market.com/api/v1/item?uid=";
private static final String uid = "5ac84494-465a-424a-b36e-fe22869ba5ec";
private static final String DEFAULT_LANGUAGE = "cn";
private static final String apiKey = "YOUR_API_KEY";

public static void main(String[] args){
HttpClient httpClient = Utils.getHttpClient();

String result = null;
try {
result = httpClient.getByKeyword("btc", DEFAULT_LANGUAGE);
result = httpClient.getByKeyword(apiKey ,"火药", DEFAULT_LANGUAGE);
}catch (ForestNetworkException e){
/*int status = e.getStatusCode();
int status = e.getStatusCode();
ForestResponse response = e.getResponse();
System.out.println("StatusCode: " + status);
System.out.println(response.getContent());
System.out.println(response.getRequest());*/
System.out.println("请求出错");
System.out.println(response.getRequest());
// System.out.println("请求出错");
}catch (Exception exception){
System.out.println("请求错误");
}
Expand All @@ -42,6 +39,7 @@ public static void main(String[] args){
// System.out.println(itemBeanList.get(0).getName());
// System.out.println(itemBeanList.get(0).getTraderPrice() + itemBeanList.get(0).getTraderPriceCur());

System.out.println("EOF");
System.exit(0);
}

Expand Down
5 changes: 3 additions & 2 deletions src/test/java/RunConsole.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import net.mamoe.mirai.alsoLogin
import net.mamoe.mirai.console.MiraiConsole
import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.enable
import net.mamoe.mirai.console.terminal.MiraiConsoleTerminalLoader
import net.mamoe.mirai.console.util.ConsoleExperimentalApi

@ConsoleExperimentalApi
suspend fun main(){
MiraiConsoleTerminalLoader.startAsDaemon()

TarkovPluginMain.enable()

val bot = MiraiConsole.addBot(3108655379, "Ekcgok8Mg29FvBaz").alsoLogin()

val bot = MiraiConsole.addBot(123456, "abcdef").alsoLogin()

MiraiConsole.job.join()
}

0 comments on commit 54ee237

Please sign in to comment.