Skip to content

Commit

Permalink
Sync.
Browse files Browse the repository at this point in the history
  • Loading branch information
rong-xiaoli committed Jan 28, 2025
1 parent ce565aa commit a4faab2
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,33 @@

import net.mamoe.mirai.console.command.CommandContext;
import net.mamoe.mirai.utils.MiraiLogger;
import top.rongxiaoli.ArisuBot;
import top.rongxiaoli.backend.Commands.ArisuBotAbstractCompositeCommand;
import top.rongxiaoli.backend.interfaces.Plugin;
import top.rongxiaoli.plugins.helldivers.backend.Constants;
import top.rongxiaoli.plugins.helldivers.config.HD2Config;

@Plugin(name = "HelldiversHelper")
public class HelldiversHelper extends ArisuBotAbstractCompositeCommand {
private final MiraiLogger LOGGER = MiraiLogger.Factory.INSTANCE.create(HelldiversHelper.class, "ArisuBot.HelldiversHelper");
private volatile boolean pluginStatus = false;

public static final HelldiversHelper INSTANCE = new HelldiversHelper();
public static final HD2Config CONFIG = new HD2Config();
public HelldiversHelper() {
super("helldivers", "Helldivers", "HD2", "hd2");
}
@SubCommand({"notice", "通知"})
public void getLatestNotice(CommandContext context) {
}

}
/**
* Load method. First time loading.
*/
@Override
public void load() {
LOGGER.debug("Helldiver helper loading. ");
CONFIG.load();
enablePlugin();
LOGGER.debug("Helldiver helper loaded. ");
}
Expand All @@ -33,6 +39,7 @@ public void load() {
@Override
public void reload() {
LOGGER.debug("Helldiver helper reloading. ");
CONFIG.reload();
enablePlugin();
LOGGER.debug("Helldiver helper reloaded. ");
}
Expand All @@ -43,6 +50,7 @@ public void reload() {
@Override
public void shutdown() {
LOGGER.debug("Helldiver helper shutting down. ");
CONFIG.shutdown();
disablePlugin();
LOGGER.debug("Helldiver helper shut down. ");
}
Expand All @@ -53,6 +61,7 @@ public void shutdown() {
@Override
public void saveData() {
LOGGER.debug("Helldiver Helper data saving. ");
CONFIG.saveData();
LOGGER.debug("Helldiver Helper data saved. ");
}

Expand All @@ -62,6 +71,7 @@ public void saveData() {
@Override
public void reloadData() {
LOGGER.debug("Helldiver Helper data reloading. ");
CONFIG.reload();
LOGGER.debug("Helldiver Helper data reloaded. ");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package top.rongxiaoli.plugins.helldivers.backend;

import java.util.Map;

public class Constants {
public static class HD2API {
public static final String API_ROOT = "https://api.helldivers2.dev";
public static final String SE_NEWS_API = "/raw/api/NewsFeed/801";
public static final String API_RAW_ROOT = "/raw/api";
public static final String API_V1_ROOT = "/api/v1";
public static final String SE_NEWS_API = "/NewsFeed/801";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package top.rongxiaoli.plugins.helldivers.backend.apifetch;

import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import top.rongxiaoli.plugins.helldivers.HelldiversHelper;
import top.rongxiaoli.plugins.helldivers.backend.Constants;

public class APIChecker {
/**
* Return state in boolean.
* @return True if alive, false otherwise.
*/
public static boolean stateInBoolean() {
try {
getRawResponse();
return true;
} catch (HttpException e) {
return false;
}
}
private static String getRawResponse() throws HttpException {
HttpRequest req = HttpRequest.get(Constants.HD2API.API_ROOT);
req.addHeaders(HelldiversHelper.CONFIG.getXSuperClientMap());
req.addHeaders(HelldiversHelper.CONFIG.getXSuperContactMap());
try(HttpResponse response = req.execute()){
return response.body();
} catch (HttpException e) {
throw e;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
package top.rongxiaoli.plugins.helldivers.backend.apifetch;

import cn.hutool.Hutool;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import top.rongxiaoli.plugins.helldivers.HelldiversHelper;
import top.rongxiaoli.plugins.helldivers.backend.Constants;
import top.rongxiaoli.plugins.helldivers.backend.datatype.Language;
import top.rongxiaoli.plugins.helldivers.backend.datatype.NewsFeedItem;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NewsFeedHelper {
public static String getLatestNews() {
String apiURL = Constants.HD2API.API_ROOT + Constants.HD2API.SE_NEWS_API;
String response;

public static String getLatestNews(Language language) {
String apiURL = Constants.HD2API.API_ROOT + Constants.HD2API.API_RAW_ROOT + Constants.HD2API.SE_NEWS_API;
HttpRequest req = HttpRequest.get(apiURL);
Map<String, String> headerMap = new HashMap<>(language.toHeaderMap());
headerMap.putAll(HelldiversHelper.CONFIG.getXSuperClientMap());
headerMap.putAll(HelldiversHelper.CONFIG.getXSuperContactMap());
try(HttpResponse response = req.execute()) {
String jsonStr = response.body();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package top.rongxiaoli.plugins.helldivers.backend.datatype;

import java.util.HashMap;

public enum Language {
EN(),
ZHS(),
ZHT();
public HashMap<String, String> toHeaderMap() {
final String headerName = "Accept-Language";
HashMap<String, String> ret = new HashMap<>();
switch (this) {
case EN:
ret.put(headerName, "en");
break;
default:
case ZHS:
ret.put(headerName, "zh-cn");
break;
case ZHT:
ret.put(headerName, "zh-hk");
break;
}
return ret;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package top.rongxiaoli.plugins.helldivers.config;

import net.mamoe.mirai.console.data.Value;
import net.mamoe.mirai.console.data.java.JavaAutoSavePluginConfig;
import net.mamoe.mirai.utils.MiraiLogger;
import top.rongxiaoli.ArisuBot;
import top.rongxiaoli.backend.interfaces.PluginBase.PluginConfigBase;

import java.util.HashMap;

public class HD2Config extends JavaAutoSavePluginConfig implements PluginConfigBase {
private final Value<String> XSuperClient = typedValue("X-Super-Client", createKType(String.class));
private final Value<String> XSuperContact = typedValue("X-Super-Contact", createKType(String.class));
public static final HD2Config INSTANCE = new HD2Config();
public static final MiraiLogger LOGGER = MiraiLogger.Factory.INSTANCE.create(HD2Config.class, "ArisuBot.HelldiversHelper.HD2Config");
public HD2Config() {
super("HD2Config");
}
public String getXSuperClient() {
return XSuperClient.get();
}
public String getXSuperContact() {
return XSuperContact.get();
}
public HashMap<String, String> getXSuperClientMap() {
HashMap<String,String> ret = new HashMap<>();
ret.put("X-Super-Client", getXSuperClient());
return ret;
}
public HashMap<String, String> getXSuperContactMap() {
HashMap<String,String> ret = new HashMap<>();
ret.put("X-Super-Contact", getXSuperContact());
return ret;
}
@Override
public void load() {
LOGGER.debug("HD2Config loading. ");
ArisuBot.INSTANCE.reloadPluginConfig(INSTANCE);
LOGGER.debug("HD2Config loaded. ");
}

@Override
public void reload() {
LOGGER.debug("HD2Config loading. ");
ArisuBot.INSTANCE.reloadPluginConfig(INSTANCE);
LOGGER.debug("HD2Config loaded. ");
}

@Override
public void shutdown() {
LOGGER.debug("HD2Config loading. ");
ArisuBot.INSTANCE.savePluginConfig(INSTANCE);
LOGGER.debug("HD2Config loaded. ");
}

@Override
public void saveData() {
LOGGER.debug("HD2Config loading. ");
ArisuBot.INSTANCE.savePluginData(INSTANCE);
LOGGER.debug("HD2Config loaded. ");
}
}

0 comments on commit a4faab2

Please sign in to comment.