From f250f40e5e0bcb676ecc30f1a20731e74ca9508f Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Sun, 26 Feb 2017 13:18:14 +0100 Subject: [PATCH] More JSON stuff. Still work in progress. --- .../telegram_backup/DownloadManager.java | 5 +- .../TelegramUpdateHandler.java | 46 +++++++++---------- .../AbstractMediaFileManager.java | 1 + .../mediafilemanager/DocumentFileManager.java | 1 + .../mediafilemanager/PhotoFileManager.java | 1 + 5 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java b/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java index 4dd0624..68cdcee 100644 --- a/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java +++ b/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java @@ -22,6 +22,7 @@ import de.fabianonline.telegram_backup.StickerConverter; import de.fabianonline.telegram_backup.DownloadProgressInterface; import de.fabianonline.telegram_backup.mediafilemanager.FileManagerFactory; import de.fabianonline.telegram_backup.mediafilemanager.AbstractMediaFileManager; +import de.fabianonline.telegram_backup.models.Message; import com.github.badoualy.telegram.api.TelegramClient; import com.github.badoualy.telegram.api.Kotlogram; @@ -263,10 +264,10 @@ public class DownloadManager { downloadMessages(ids); } - LinkedList messages = this.db.getMessagesWithMedia(); + LinkedList messages = this.db.getMessagesWithMedia(); logger.debug("Database returned {} messages with media", messages.size()); prog.onMediaDownloadStart(messages.size()); - for (TLMessage msg : messages) { + for (Message msg : messages) { AbstractMediaFileManager m = FileManagerFactory.getFileManager(msg); logger.trace("message {}, {}, {}, {}, {}", msg.getId(), diff --git a/src/main/java/de/fabianonline/telegram_backup/TelegramUpdateHandler.java b/src/main/java/de/fabianonline/telegram_backup/TelegramUpdateHandler.java index f502119..b06fa4e 100644 --- a/src/main/java/de/fabianonline/telegram_backup/TelegramUpdateHandler.java +++ b/src/main/java/de/fabianonline/telegram_backup/TelegramUpdateHandler.java @@ -1,16 +1,16 @@ /* Telegram_Backup * Copyright (C) 2016 Fabian Schlenz - * + * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ @@ -29,15 +29,16 @@ import de.fabianonline.telegram_backup.Utils; import de.fabianonline.telegram_backup.UserManager; import de.fabianonline.telegram_backup.mediafilemanager.AbstractMediaFileManager; import de.fabianonline.telegram_backup.mediafilemanager.FileManagerFactory; +import de.fabianonline.telegram_backup.models.Message; class TelegramUpdateHandler implements UpdateCallback { private UserManager user = null; private Database db = null; public boolean debug = false; private Gson gson = Utils.getGson(); - + public void activate() { this.user = UserManager.getInstance(); this.db = Database.getInstance();} - + public void onUpdates(TelegramClient c, TLUpdates u) { if (db==null) return; if (debug) System.out.println("onUpdates - " + u.getUpdates().size() + " Updates, " + u.getUsers().size() + " Users, " + u.getChats().size() + " Chats"); @@ -48,7 +49,7 @@ class TelegramUpdateHandler implements UpdateCallback { db.saveUsers(u.getUsers(), gson); db.saveChats(u.getChats(), gson); } - + public void onUpdatesCombined(TelegramClient c, TLUpdatesCombined u) { if (db==null) return; if (debug) System.out.println("onUpdatesCombined"); @@ -58,14 +59,14 @@ class TelegramUpdateHandler implements UpdateCallback { db.saveUsers(u.getUsers(), gson); db.saveChats(u.getChats(), gson); } - + public void onUpdateShort(TelegramClient c, TLUpdateShort u) { if (db==null) return; if (debug) System.out.println("onUpdateShort"); processUpdate(u.getUpdate(), c); if (debug) System.out.println(" " + u.getUpdate().getClass().getName()); } - + public void onShortChatMessage(TelegramClient c, TLUpdateShortChatMessage m) { if (db==null) return; if (debug) System.out.println("onShortChatMessage - " + m.getMessage()); @@ -93,7 +94,7 @@ class TelegramUpdateHandler implements UpdateCallback { db.saveMessages(vector, Kotlogram.API_LAYER, gson); System.out.print('.'); } - + public void onShortMessage(TelegramClient c, TLUpdateShortMessage m) { if (db==null) return; if (debug) System.out.println("onShortMessage - " + m.getOut() + " - " + m.getUserId() + " - " + m.getMessage()); @@ -129,27 +130,24 @@ class TelegramUpdateHandler implements UpdateCallback { db.saveMessages(vector, Kotlogram.API_LAYER, gson); System.out.print('.'); } - + public void onShortSentMessage(TelegramClient c, TLUpdateShortSentMessage m) { if (db==null) return; System.out.println("onShortSentMessage"); } public void onUpdateTooLong(TelegramClient c) { if (db==null) return; System.out.println("onUpdateTooLong"); } - + private void processUpdate(TLAbsUpdate update, TelegramClient client) { if (update instanceof TLUpdateNewMessage) { TLAbsMessage abs_msg = ((TLUpdateNewMessage)update).getMessage(); - TLVector vector = new TLVector(TLAbsMessage.class); - vector.add(abs_msg); - db.saveMessages(vector, Kotlogram.API_LAYER, gson); + Message msg = new Message(abs_msg); + msg.save(); System.out.print('.'); - if (abs_msg instanceof TLMessage) { - AbstractMediaFileManager fm = FileManagerFactory.getFileManager((TLMessage)abs_msg, user, client); - if (fm != null && !fm.isEmpty() && !fm.isDownloaded()) { - try { - fm.download(); - } catch (Exception e) { - System.out.println("We got an exception while downloading media, but we're going to ignore it."); - System.out.println("Here it is anyway:"); - e.printStackTrace(); - } + AbstractMediaFileManager fm = FileManagerFactory.getFileManager(msg); + if (fm != null && !fm.isEmpty() && !fm.isDownloaded()) { + try { + fm.download(client); + } catch (Exception e) { + System.out.println("We got an exception while downloading media, but we're going to ignore it."); + System.out.println("Here it is anyway:"); + e.printStackTrace(); } } } else { diff --git a/src/main/java/de/fabianonline/telegram_backup/mediafilemanager/AbstractMediaFileManager.java b/src/main/java/de/fabianonline/telegram_backup/mediafilemanager/AbstractMediaFileManager.java index 82a94f8..ee38996 100644 --- a/src/main/java/de/fabianonline/telegram_backup/mediafilemanager/AbstractMediaFileManager.java +++ b/src/main/java/de/fabianonline/telegram_backup/mediafilemanager/AbstractMediaFileManager.java @@ -27,6 +27,7 @@ import de.fabianonline.telegram_backup.models.Message; import com.google.gson.JsonObject; import com.github.badoualy.telegram.api.TelegramClient; +import com.github.badoualy.telegram.tl.exception.RpcErrorException; import java.io.IOException; import java.io.File; diff --git a/src/main/java/de/fabianonline/telegram_backup/mediafilemanager/DocumentFileManager.java b/src/main/java/de/fabianonline/telegram_backup/mediafilemanager/DocumentFileManager.java index f863e04..d6146e3 100644 --- a/src/main/java/de/fabianonline/telegram_backup/mediafilemanager/DocumentFileManager.java +++ b/src/main/java/de/fabianonline/telegram_backup/mediafilemanager/DocumentFileManager.java @@ -27,6 +27,7 @@ import com.google.gson.JsonObject; import com.google.gson.JsonElement; import com.github.badoualy.telegram.api.TelegramClient; +import com.github.badoualy.telegram.tl.exception.RpcErrorException; import java.io.IOException; import java.io.File; diff --git a/src/main/java/de/fabianonline/telegram_backup/mediafilemanager/PhotoFileManager.java b/src/main/java/de/fabianonline/telegram_backup/mediafilemanager/PhotoFileManager.java index 2471a83..ef29a12 100644 --- a/src/main/java/de/fabianonline/telegram_backup/mediafilemanager/PhotoFileManager.java +++ b/src/main/java/de/fabianonline/telegram_backup/mediafilemanager/PhotoFileManager.java @@ -27,6 +27,7 @@ import com.google.gson.JsonObject; import com.google.gson.JsonElement; import com.github.badoualy.telegram.api.TelegramClient; +import com.github.badoualy.telegram.tl.exception.RpcErrorException; import java.io.IOException; import java.io.File;