io.github.chikyukido.nyaapplet.api.ApiUrls Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of NyaApplet Show documentation
Show all versions of NyaApplet Show documentation
An applet that can send in anime pics from different sources.
package io.github.chikyukido.nyaapplet.api;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@Component
public class ApiUrls {
public final HashMap> getUrlsForType = new HashMap<>();
public final HashMap> getNsfwUrlsForType = new HashMap<>();
ApiUrls() {
addNekosBestTypes();
addWaifuPicsTypes();
addNekosLifeTypes();
}
/**
* Adds the endpoints to the hashmaps
*/
private void addNekosBestTypes() {
try {
URL url = new URL("https://nekos.best/api/v2/endpoints");
Gson gson = new Gson();
JsonObject json = gson.fromJson((IOUtils.toString(url.openStream(), StandardCharsets.UTF_8)), JsonObject.class);
json.asMap().keySet().forEach(s -> addSfwType(s, "https://nekos.best/api/v2/" + s));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Adds the endpoints to the hashmaps
*/
private void addWaifuPicsTypes() {
try {
URL url = new URL("https://api.waifu.pics/endpoints");
Gson gson = new Gson();
JsonObject json = gson.fromJson((IOUtils.toString(url.openStream(), StandardCharsets.UTF_8)), JsonObject.class);
JsonArray sfwType = json.getAsJsonArray("sfw");
JsonArray nsfwType = json.getAsJsonArray("nsfw");
sfwType.forEach(jsonElement -> addSfwType(jsonElement.getAsString(), "https://api.waifu.pics/sfw/" + jsonElement.getAsString()));
nsfwType.forEach(jsonElement -> addNsfwType(jsonElement.getAsString(), "https://api.waifu.pics/nsfw/" + jsonElement.getAsString()));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void addNekosLifeTypes() {
try {
URL url = new URL("https://raw.githubusercontent.com/Nekos-life/nekos-dot-life/master/endpoints.json");
Gson gson = new Gson();
JsonObject json = gson.fromJson((IOUtils.toString(url.openStream(), StandardCharsets.UTF_8)), JsonObject.class);
json.asMap().values().stream()
.map(JsonElement::getAsString)
.filter(s -> s.contains("img"))
.map(s -> s.replace("/img/", ""))
.forEach(s -> addSfwType(s,
"https://nekos.life/api/v2/img/" + s));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void addNsfwType(String type, String url) {
if (!getNsfwUrlsForType.containsKey(type)) {
getNsfwUrlsForType.put(type, new ArrayList<>());
}
getNsfwUrlsForType.get(type).add(url);
}
private void addSfwType(String type, String url) {
if (!getUrlsForType.containsKey(type)) {
getUrlsForType.put(type, new ArrayList<>());
}
getUrlsForType.get(type).add(url);
}
}