diff --git a/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java b/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java index 047c617..eb4256f 100644 --- a/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java +++ b/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java @@ -167,6 +167,7 @@ public class DownloadManager { private void downloadMessages(List ids) throws RpcErrorException, IOException { prog.onMessageDownloadStart(ids.size()); + boolean has_seen_flood_wait_message = false; logger.debug("Entering download loop"); while (ids.size()>0) { @@ -179,7 +180,30 @@ public class DownloadManager { logger.trace("vector.size(): {}", vector.size()); logger.trace("ids.size(): {}", ids.size()); - TLAbsMessages response = client.messagesGetMessages(vector); + TLAbsMessages response; + int tries = 0; + while(true) { + logger.trace("Trying getMessages(), tries={}", tries); + if (tries>=5) { + CommandLineController.show_error("Couldn't getMessages after 5 tries. Quitting."); + } + tries++; + try { + response = client.messagesGetMessages(vector); + break; + } catch (RpcErrorException e) { + if (e.getCode()==420) { // FLOOD_WAIT + Utils.obeyFloodWaitException(e, has_seen_flood_wait_message); + has_seen_flood_wait_message = true; + } else { + throw e; + } + } + } + logger.trace("response.getMessages().size(): {}", response.getMessages().size()); + if (response.getMessages().size() != vector.size()) { + CommandLineController.show_error("Requested " + vector.size() + " messages, but got " + response.getMessages().size() + ". That is unexpected. Quitting."); + } prog.onMessageDownloaded(response.getMessages().size()); db.saveMessages(response.getMessages(), Kotlogram.API_LAYER); db.saveChats(response.getChats()); diff --git a/src/main/java/de/fabianonline/telegram_backup/Utils.java b/src/main/java/de/fabianonline/telegram_backup/Utils.java index 67d96bb..c81b350 100644 --- a/src/main/java/de/fabianonline/telegram_backup/Utils.java +++ b/src/main/java/de/fabianonline/telegram_backup/Utils.java @@ -47,21 +47,27 @@ public class Utils { } static void obeyFloodWaitException(RpcErrorException e) throws RpcErrorException { + obeyFloodWaitException(e, false); + } + + static void obeyFloodWaitException(RpcErrorException e, boolean silent) throws RpcErrorException { if (e==null || e.getCode()!=420) return; int delay = e.getTagInteger(); - System.out.println(""); - System.out.println( - "Telegram complained about us (okay, me) making too many requests in too short time by\n" + - "sending us \"" + e.getTag() + "\" as an error. So we now have to wait a bit. Telegram\n" + - "asked us to wait for " + delay + " seconds.\n" + - "\n" + - "So I'm going to do just that for now. If you don't want to wait, you can quit by pressing\n" + - "Ctrl+C. You can restart me at any time and I will just continue to download your\n" + - "messages and media. But be advised that just restarting me is not going to change\n" + - "the fact that Telegram won't talk to me until then."); + if(!silent) { + System.out.println(""); + System.out.println( + "Telegram complained about us (okay, me) making too many requests in too short time by\n" + + "sending us \"" + e.getTag() + "\" as an error. So we now have to wait a bit. Telegram\n" + + "asked us to wait for " + delay + " seconds.\n" + + "\n" + + "So I'm going to do just that for now. If you don't want to wait, you can quit by pressing\n" + + "Ctrl+C. You can restart me at any time and I will just continue to download your\n" + + "messages and media. But be advised that just restarting me is not going to change\n" + + "the fact that Telegram won't talk to me until then."); + System.out.println(""); + } try { TimeUnit.SECONDS.sleep(delay + 1); } catch(InterruptedException e2) {} - System.out.println(""); } static Version getNewestVersion() {