-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce565aa
commit a4faab2
Showing
6 changed files
with
159 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 5 additions & 1 deletion
6
src/main/java/top/rongxiaoli/plugins/helldivers/backend/Constants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/top/rongxiaoli/plugins/helldivers/backend/apifetch/APIChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
27 changes: 23 additions & 4 deletions
27
src/main/java/top/rongxiaoli/plugins/helldivers/backend/apifetch/NewsFeedHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/top/rongxiaoli/plugins/helldivers/backend/datatype/Language.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/top/rongxiaoli/plugins/helldivers/config/HD2Config.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. "); | ||
} | ||
} |