Commit 0cba7d35 authored by Purus Cor's avatar Purus Cor
Browse files

Update food api

parent 21bf745b
......@@ -28,6 +28,7 @@ package haven;
import haven.res.ui.tt.q.qbuff.QBuff;
import integrations.food.FoodService;
import integrations.food.IconService;
import java.awt.*;
import java.awt.image.BufferedImage;
......@@ -180,6 +181,7 @@ public class GItem extends AWidget implements ItemInfo.SpriteOwner, GSprite.Owne
dropItMaybe();
postProcessed = true;
}
IconService.checkIcon(info(), spr);
} catch (Loading l) {
}
}
......
......@@ -7,7 +7,6 @@ import haven.resutil.FoodInfo;
import org.json.JSONArray;
import org.json.JSONObject;
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
......@@ -24,13 +23,13 @@ import java.util.*;
import java.util.concurrent.*;
public class FoodService {
private static final String API_ENDPOINT = "https://vatsul.com/foodproxy/food-api/";
private static final String FOOD_DATA_URL = "https://vatsul.com/foodproxy/food-info/data/food-info.json";
public static final String API_ENDPOINT = "http://hnhfood.vatsul.com/api/";
private static final String FOOD_DATA_URL = "http://hnhfood.vatsul.com/api/data/food-info.json";
private static final File FOOD_DATA_CACHE_FILE = new File("food_data.json");
private static final Map<String, ParsedFoodInfo> cachedItems = new ConcurrentHashMap<>();
private static final Queue<HashedFoodInfo> sendQueue = new ConcurrentLinkedQueue<>();
private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
public static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
static {
if (!Resource.language.equals("en")) {
......@@ -68,7 +67,7 @@ public class FoodService {
}
if (System.currentTimeMillis() - lastModified > TimeUnit.MINUTES.toMillis(30)) {
try {
HttpsURLConnection connection = (HttpsURLConnection) new URL(FOOD_DATA_URL).openConnection();
HttpURLConnection connection = (HttpURLConnection) new URL(FOOD_DATA_URL).openConnection();
connection.setRequestProperty("User-Agent", "H&H Client");
connection.setRequestProperty("Cache-Control", "no-cache");
StringBuilder stringBuilder = new StringBuilder();
......@@ -77,8 +76,9 @@ public class FoodService {
} finally {
connection.disconnect();
}
String content = stringBuilder.toString();
Files.write(FOOD_DATA_CACHE_FILE.toPath(), Collections.singleton(content), StandardCharsets.UTF_8, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
String content = stringBuilder.toString();
Files.write(FOOD_DATA_CACHE_FILE.toPath(), Collections.singleton(content), StandardCharsets.UTF_8, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
JSONObject object = new JSONObject(content);
object.keySet().forEach(key -> cachedItems.put(key, new ParsedFoodInfo()));
System.out.println("Updated food data file: " + cachedItems.size() + " entries");
......@@ -132,7 +132,11 @@ public class FoodService {
String name = (String) info.getClass().getField("name").get(info);
Double value = (Double) info.getClass().getField("val").get(info);
parsedFoodInfo.ingredients.add(new FoodIngredient(name, (int) (value * 100)));
}
} else if(info.getClass().getName().equals("Smoke")) {
String name = (String) info.getClass().getField("name").get(info);
Double value = (Double) info.getClass().getField("val").get(info);
parsedFoodInfo.ingredients.add(new FoodIngredient(name, (int) (value * 100)));
}
}
checkAndSend(parsedFoodInfo);
......@@ -171,8 +175,8 @@ public class FoodService {
if (!toSend.isEmpty()) {
try {
HttpsURLConnection connection =
(HttpsURLConnection) new URL(API_ENDPOINT + "/food").openConnection();
HttpURLConnection connection =
(HttpURLConnection) new URL(API_ENDPOINT + "food").openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("User-Agent", "H&H Client");
......
package integrations.food;
import haven.GSprite;
import haven.ItemInfo;
import haven.resutil.FoodInfo;
import org.json.JSONArray;
import org.json.JSONObject;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
public class IconService {
private static HashSet<String> sentIcons = new HashSet<>();
static {
getSentIcons();
}
public static void getSentIcons() {
try {
HttpURLConnection conn = (HttpURLConnection) new URL(FoodService.API_ENDPOINT + "data/icons.json").openConnection();
conn.setRequestProperty("User-Agent", "H&H Client");
conn.setRequestProperty("Cache-Control", "no-cache");
Scanner scan = new Scanner(conn.getInputStream());
StringBuilder sb = new StringBuilder();
while(scan.hasNextLine())
sb.append(scan.nextLine());
JSONArray arr = new JSONArray(sb.toString());
arr.forEach((s) -> sentIcons.add(s.toString()));
} catch(IOException e) {
e.printStackTrace();
}
}
public static void checkIcon(List<ItemInfo> infos, GSprite sprite) {
if(ItemInfo.find(FoodInfo.class, infos) == null)
return;
String name = null;
for(ItemInfo info : infos) {
if (info instanceof ItemInfo.Name) {
name = ((ItemInfo.Name) info).str.text;
}
}
if(name != null && !sentIcons.contains(name)) {
try {
BufferedImage img = ((GSprite.ImageSprite)sprite).image();
if(img != null)
sendIcon(name, img);
} catch(Exception e) {
e.printStackTrace();
}
sentIcons.add(name);
}
}
private static void sendIcon(String name, BufferedImage icon) {
Runnable run = new Runnable() {
@Override
public void run() {
try {
System.out.println(new URL(FoodService.API_ENDPOINT + "icon"));
HttpURLConnection conn = (HttpURLConnection) new URL(FoodService.API_ENDPOINT + "icon").openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("User-Agent", "H&H Client");
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
conn.setDoOutput(true);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(icon, "png", os);
os.flush();
JSONObject obj = new JSONObject();
obj.put("name", name);
obj.put("icon", new String(Base64.getEncoder().encode(os.toByteArray()), StandardCharsets.UTF_8));
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.write(obj.toString().getBytes(StandardCharsets.UTF_8));
dos.close();
System.out.println("Sent a new food icon with a response code: " + conn.getResponseCode());
} catch(IOException e) {
e.printStackTrace();
}
}
};
FoodService.scheduler.execute(run);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment