From da72ae75f5ae6d2ebc6abd63b3d44977697fda60 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Tue, 21 Feb 2017 14:07:16 +0100 Subject: [PATCH] Added JSON-based models. --- .../telegram_backup/DownloadManager.java | 16 +++++++++++++--- .../de/fabianonline/telegram_backup/Utils.java | 15 +++++++++++++++ .../telegram_backup/models/Message_c09be45f.java | 15 +++++++++++++++ .../telegram_backup/models/Peer.java | 4 ++++ 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 src/main/java/de/fabianonline/telegram_backup/models/Message_c09be45f.java create mode 100644 src/main/java/de/fabianonline/telegram_backup/models/Peer.java diff --git a/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java b/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java index 9fcb8bc..3c185ec 100644 --- a/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java +++ b/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java @@ -35,6 +35,8 @@ import com.github.badoualy.telegram.tl.exception.RpcErrorException; import com.github.badoualy.telegram.tl.api.request.TLRequestUploadGetFile; import org.slf4j.LoggerFactory; import org.slf4j.Logger; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import java.io.IOException; import java.io.File; @@ -206,10 +208,18 @@ public class DownloadManager { if (response.getMessages().size() != vector.size()) { CommandLineController.show_error("Requested " + vector.size() + " messages, but got " + response.getMessages().size() + ". That is unexpected. Quitting."); } + + //ObjectMapper om = new ObjectMapper(); + //String json = om.writerWithDefaultPrettyPrinter().writeValueAsString(response.getMessages().get(1)); + Gson gson = Utils.getGson(); + String json = gson.toJson(response.getMessages()); + System.out.println(json); + //System.exit(1); + prog.onMessageDownloaded(response.getMessages().size()); - db.saveMessages(response.getMessages(), Kotlogram.API_LAYER); - db.saveChats(response.getChats()); - db.saveUsers(response.getUsers()); + //db.saveMessages(response.getMessages(), Kotlogram.API_LAYER); + //db.saveChats(response.getChats()); + //db.saveUsers(response.getUsers()); logger.trace("Sleeping"); try { TimeUnit.MILLISECONDS.sleep(Config.DELAY_AFTER_GET_MESSAGES); diff --git a/src/main/java/de/fabianonline/telegram_backup/Utils.java b/src/main/java/de/fabianonline/telegram_backup/Utils.java index c81b350..67d621e 100644 --- a/src/main/java/de/fabianonline/telegram_backup/Utils.java +++ b/src/main/java/de/fabianonline/telegram_backup/Utils.java @@ -26,6 +26,8 @@ import org.apache.commons.io.IOUtils; import de.fabianonline.telegram_backup.Version; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.github.badoualy.telegram.tl.core.TLBytes; +import java.lang.reflect.Type; public class Utils { public static final int VERSIONS_EQUAL = 0; @@ -174,4 +176,17 @@ public class Utils { if (!CommandLineOptions.cmd_anonymize) return str; return str.replaceAll("[0-9]", "1").replaceAll("[A-Z]", "A").replaceAll("[a-z]", "a") + " (ANONYMIZED)"; } + + public static Gson getGson() { + return new GsonBuilder() + .registerTypeAdapter(TLBytes.class, new TLBytesSerializer()) + .setPrettyPrinting() + .create(); + } +} + +class TLBytesSerializer implements JsonSerializer { + public JsonElement serialize(TLBytes bytes, Type typeOfSrc, JsonSerializationContext ctx) { + return new JsonNull(); + } } diff --git a/src/main/java/de/fabianonline/telegram_backup/models/Message_c09be45f.java b/src/main/java/de/fabianonline/telegram_backup/models/Message_c09be45f.java new file mode 100644 index 0000000..7bfc333 --- /dev/null +++ b/src/main/java/de/fabianonline/telegram_backup/models/Message_c09be45f.java @@ -0,0 +1,15 @@ +public class Message_c09be45f { + int flags; + boolean out; + boolean mentioned; + boolean mediaUnread; + boolean silent; + boolean post; + int fromId; + Peer toId; + String _constructor; + int date; + String message; + int id; + JsonElement media; +} diff --git a/src/main/java/de/fabianonline/telegram_backup/models/Peer.java b/src/main/java/de/fabianonline/telegram_backup/models/Peer.java new file mode 100644 index 0000000..6cf4371 --- /dev/null +++ b/src/main/java/de/fabianonline/telegram_backup/models/Peer.java @@ -0,0 +1,4 @@ +class Peer { + int userId; + String _constructor; +}