A catalog app for the LihonT mod: avatar frames and chat bubbles made by members. Browse, add with the plus button, author profiles, publishing with a check before anything is shown. Signs in through the TikTok that carries the mod, which hands over only the public account data to an app signed with the same key. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
91 lines
3.9 KiB
Java
91 lines
3.9 KiB
Java
package lihonstore.app;
|
||
|
||
import org.json.JSONObject;
|
||
|
||
import java.io.ByteArrayOutputStream;
|
||
import java.io.IOException;
|
||
import java.io.InputStream;
|
||
import java.io.OutputStream;
|
||
import java.net.HttpURLConnection;
|
||
import java.net.URL;
|
||
import java.nio.charset.StandardCharsets;
|
||
|
||
/** The LihonT service: JSON posts to /members/v1/<operation> and the approved pictures from /members/v1/art/<id>.png. */
|
||
final class Api {
|
||
static final String BASE = "https://lihont.space/members/v1/";
|
||
private static final int ART_LIMIT = 200 * 1024;
|
||
|
||
private Api() {}
|
||
|
||
/** A failed call: the message is the service's own (or a short note about the network). */
|
||
static final class Failure extends Exception {
|
||
Failure(String message) {
|
||
super(message);
|
||
}
|
||
}
|
||
|
||
static JSONObject post(String operation, JSONObject body) throws Failure {
|
||
HttpURLConnection connection = null;
|
||
try {
|
||
connection = (HttpURLConnection) new URL(BASE + operation).openConnection();
|
||
connection.setConnectTimeout(6000);
|
||
connection.setReadTimeout(30000);
|
||
connection.setInstanceFollowRedirects(false);
|
||
connection.setRequestMethod("POST");
|
||
connection.setDoOutput(true);
|
||
connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
|
||
connection.setRequestProperty("User-Agent", "LihonT-Store/1");
|
||
byte[] data = body.toString().getBytes(StandardCharsets.UTF_8);
|
||
connection.setFixedLengthStreamingMode(data.length);
|
||
try (OutputStream out = connection.getOutputStream()) {
|
||
out.write(data);
|
||
}
|
||
int status = connection.getResponseCode();
|
||
InputStream stream = status < 400 ? connection.getInputStream() : connection.getErrorStream();
|
||
String text = stream == null ? "" : new String(readAll(stream, 1 << 20), StandardCharsets.UTF_8);
|
||
JSONObject json = text.length() == 0 ? new JSONObject() : new JSONObject(text);
|
||
if (status >= 400) throw new Failure(Texts.error(json.optString("error", "")));
|
||
return json;
|
||
} catch (Failure failure) {
|
||
throw failure;
|
||
} catch (Exception unreachable) {
|
||
android.util.Log.w("LIHONSTORE", "post " + operation + " failed: " + unreachable);
|
||
throw new Failure("Нет связи с сервером");
|
||
} finally {
|
||
if (connection != null) connection.disconnect();
|
||
}
|
||
}
|
||
|
||
/** The picture of an approved item. */
|
||
static byte[] art(String id) throws Failure {
|
||
HttpURLConnection connection = null;
|
||
try {
|
||
connection = (HttpURLConnection) new URL(BASE + "art/" + id + ".png").openConnection();
|
||
connection.setConnectTimeout(6000);
|
||
connection.setReadTimeout(20000);
|
||
connection.setInstanceFollowRedirects(false);
|
||
connection.setRequestProperty("User-Agent", "LihonT-Store/1");
|
||
if (connection.getResponseCode() != 200) throw new Failure("Картинки нет");
|
||
return readAll(connection.getInputStream(), ART_LIMIT);
|
||
} catch (Failure failure) {
|
||
throw failure;
|
||
} catch (Exception unreachable) {
|
||
throw new Failure("Нет связи с сервером");
|
||
} finally {
|
||
if (connection != null) connection.disconnect();
|
||
}
|
||
}
|
||
|
||
private static byte[] readAll(InputStream in, int limit) throws IOException {
|
||
try (InputStream source = in; ByteArrayOutputStream out = new ByteArrayOutputStream()) {
|
||
byte[] buffer = new byte[8192];
|
||
int count;
|
||
while ((count = source.read(buffer)) >= 0) {
|
||
out.write(buffer, 0, count);
|
||
if (out.size() > limit) throw new IOException("too large");
|
||
}
|
||
return out.toByteArray();
|
||
}
|
||
}
|
||
}
|