1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-11-22 16:56:16 +00:00

More JSON stuff. Still work in progress.

This commit is contained in:
Fabian Schlenz 2017-02-26 13:18:14 +01:00
parent ef03deb31e
commit f250f40e5e
5 changed files with 28 additions and 26 deletions

View File

@ -22,6 +22,7 @@ import de.fabianonline.telegram_backup.StickerConverter;
import de.fabianonline.telegram_backup.DownloadProgressInterface; import de.fabianonline.telegram_backup.DownloadProgressInterface;
import de.fabianonline.telegram_backup.mediafilemanager.FileManagerFactory; import de.fabianonline.telegram_backup.mediafilemanager.FileManagerFactory;
import de.fabianonline.telegram_backup.mediafilemanager.AbstractMediaFileManager; 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.TelegramClient;
import com.github.badoualy.telegram.api.Kotlogram; import com.github.badoualy.telegram.api.Kotlogram;
@ -263,10 +264,10 @@ public class DownloadManager {
downloadMessages(ids); downloadMessages(ids);
} }
LinkedList<TLMessage> messages = this.db.getMessagesWithMedia(); LinkedList<Message> messages = this.db.getMessagesWithMedia();
logger.debug("Database returned {} messages with media", messages.size()); logger.debug("Database returned {} messages with media", messages.size());
prog.onMediaDownloadStart(messages.size()); prog.onMediaDownloadStart(messages.size());
for (TLMessage msg : messages) { for (Message msg : messages) {
AbstractMediaFileManager m = FileManagerFactory.getFileManager(msg); AbstractMediaFileManager m = FileManagerFactory.getFileManager(msg);
logger.trace("message {}, {}, {}, {}, {}", logger.trace("message {}, {}, {}, {}, {}",
msg.getId(), msg.getId(),

View File

@ -1,16 +1,16 @@
/* Telegram_Backup /* Telegram_Backup
* Copyright (C) 2016 Fabian Schlenz * Copyright (C) 2016 Fabian Schlenz
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */ * along with this program. If not, see <http://www.gnu.org/licenses/>. */
@ -29,15 +29,16 @@ import de.fabianonline.telegram_backup.Utils;
import de.fabianonline.telegram_backup.UserManager; import de.fabianonline.telegram_backup.UserManager;
import de.fabianonline.telegram_backup.mediafilemanager.AbstractMediaFileManager; import de.fabianonline.telegram_backup.mediafilemanager.AbstractMediaFileManager;
import de.fabianonline.telegram_backup.mediafilemanager.FileManagerFactory; import de.fabianonline.telegram_backup.mediafilemanager.FileManagerFactory;
import de.fabianonline.telegram_backup.models.Message;
class TelegramUpdateHandler implements UpdateCallback { class TelegramUpdateHandler implements UpdateCallback {
private UserManager user = null; private UserManager user = null;
private Database db = null; private Database db = null;
public boolean debug = false; public boolean debug = false;
private Gson gson = Utils.getGson(); private Gson gson = Utils.getGson();
public void activate() { this.user = UserManager.getInstance(); this.db = Database.getInstance();} public void activate() { this.user = UserManager.getInstance(); this.db = Database.getInstance();}
public void onUpdates(TelegramClient c, TLUpdates u) { public void onUpdates(TelegramClient c, TLUpdates u) {
if (db==null) return; if (db==null) return;
if (debug) System.out.println("onUpdates - " + u.getUpdates().size() + " Updates, " + u.getUsers().size() + " Users, " + u.getChats().size() + " Chats"); 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.saveUsers(u.getUsers(), gson);
db.saveChats(u.getChats(), gson); db.saveChats(u.getChats(), gson);
} }
public void onUpdatesCombined(TelegramClient c, TLUpdatesCombined u) { public void onUpdatesCombined(TelegramClient c, TLUpdatesCombined u) {
if (db==null) return; if (db==null) return;
if (debug) System.out.println("onUpdatesCombined"); if (debug) System.out.println("onUpdatesCombined");
@ -58,14 +59,14 @@ class TelegramUpdateHandler implements UpdateCallback {
db.saveUsers(u.getUsers(), gson); db.saveUsers(u.getUsers(), gson);
db.saveChats(u.getChats(), gson); db.saveChats(u.getChats(), gson);
} }
public void onUpdateShort(TelegramClient c, TLUpdateShort u) { public void onUpdateShort(TelegramClient c, TLUpdateShort u) {
if (db==null) return; if (db==null) return;
if (debug) System.out.println("onUpdateShort"); if (debug) System.out.println("onUpdateShort");
processUpdate(u.getUpdate(), c); processUpdate(u.getUpdate(), c);
if (debug) System.out.println(" " + u.getUpdate().getClass().getName()); if (debug) System.out.println(" " + u.getUpdate().getClass().getName());
} }
public void onShortChatMessage(TelegramClient c, TLUpdateShortChatMessage m) { public void onShortChatMessage(TelegramClient c, TLUpdateShortChatMessage m) {
if (db==null) return; if (db==null) return;
if (debug) System.out.println("onShortChatMessage - " + m.getMessage()); if (debug) System.out.println("onShortChatMessage - " + m.getMessage());
@ -93,7 +94,7 @@ class TelegramUpdateHandler implements UpdateCallback {
db.saveMessages(vector, Kotlogram.API_LAYER, gson); db.saveMessages(vector, Kotlogram.API_LAYER, gson);
System.out.print('.'); System.out.print('.');
} }
public void onShortMessage(TelegramClient c, TLUpdateShortMessage m) { public void onShortMessage(TelegramClient c, TLUpdateShortMessage m) {
if (db==null) return; if (db==null) return;
if (debug) System.out.println("onShortMessage - " + m.getOut() + " - " + m.getUserId() + " - " + m.getMessage()); 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); db.saveMessages(vector, Kotlogram.API_LAYER, gson);
System.out.print('.'); System.out.print('.');
} }
public void onShortSentMessage(TelegramClient c, TLUpdateShortSentMessage m) { if (db==null) return; System.out.println("onShortSentMessage"); } 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"); } public void onUpdateTooLong(TelegramClient c) { if (db==null) return; System.out.println("onUpdateTooLong"); }
private void processUpdate(TLAbsUpdate update, TelegramClient client) { private void processUpdate(TLAbsUpdate update, TelegramClient client) {
if (update instanceof TLUpdateNewMessage) { if (update instanceof TLUpdateNewMessage) {
TLAbsMessage abs_msg = ((TLUpdateNewMessage)update).getMessage(); TLAbsMessage abs_msg = ((TLUpdateNewMessage)update).getMessage();
TLVector<TLAbsMessage> vector = new TLVector<TLAbsMessage>(TLAbsMessage.class); Message msg = new Message(abs_msg);
vector.add(abs_msg); msg.save();
db.saveMessages(vector, Kotlogram.API_LAYER, gson);
System.out.print('.'); System.out.print('.');
if (abs_msg instanceof TLMessage) { AbstractMediaFileManager fm = FileManagerFactory.getFileManager(msg);
AbstractMediaFileManager fm = FileManagerFactory.getFileManager((TLMessage)abs_msg, user, client); if (fm != null && !fm.isEmpty() && !fm.isDownloaded()) {
if (fm != null && !fm.isEmpty() && !fm.isDownloaded()) { try {
try { fm.download(client);
fm.download(); } catch (Exception e) {
} catch (Exception e) { System.out.println("We got an exception while downloading media, but we're going to ignore it.");
System.out.println("We got an exception while downloading media, but we're going to ignore it."); System.out.println("Here it is anyway:");
System.out.println("Here it is anyway:"); e.printStackTrace();
e.printStackTrace();
}
} }
} }
} else { } else {

View File

@ -27,6 +27,7 @@ import de.fabianonline.telegram_backup.models.Message;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.github.badoualy.telegram.api.TelegramClient; import com.github.badoualy.telegram.api.TelegramClient;
import com.github.badoualy.telegram.tl.exception.RpcErrorException;
import java.io.IOException; import java.io.IOException;
import java.io.File; import java.io.File;

View File

@ -27,6 +27,7 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.github.badoualy.telegram.api.TelegramClient; import com.github.badoualy.telegram.api.TelegramClient;
import com.github.badoualy.telegram.tl.exception.RpcErrorException;
import java.io.IOException; import java.io.IOException;
import java.io.File; import java.io.File;

View File

@ -27,6 +27,7 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.github.badoualy.telegram.api.TelegramClient; import com.github.badoualy.telegram.api.TelegramClient;
import com.github.badoualy.telegram.tl.exception.RpcErrorException;
import java.io.IOException; import java.io.IOException;
import java.io.File; import java.io.File;