From 80897011acfc7f32fb9e3adb4b840e515496c0ff Mon Sep 17 00:00:00 2001 From: Flameish Date: Mon, 19 Apr 2021 16:32:07 +0200 Subject: [PATCH] Fixed concurrent downloads updating the same progress message --- src/main/java/bots/Telegram.java | 11 ++++------- src/main/java/grabber/Novel.java | 5 +++-- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/main/java/bots/Telegram.java b/src/main/java/bots/Telegram.java index e94603e..3e83e6e 100644 --- a/src/main/java/bots/Telegram.java +++ b/src/main/java/bots/Telegram.java @@ -13,7 +13,6 @@ import grabber.Novel; import grabber.sources.Source; import system.Config; -import system.init; import java.io.*; import java.nio.file.Files; @@ -50,7 +49,6 @@ public class Telegram { "Example:\n" + " -link http://novelhost.com/novel/ -chapters 5 10 -getImages"; private final ConcurrentHashMap currentlyDownloading = new ConcurrentHashMap<>(); - private final ConcurrentHashMap downloadMsgIds = new ConcurrentHashMap<>(); private final ExecutorService executor = Executors.newFixedThreadPool(10); private final ConcurrentHashMap userChapterCount = new ConcurrentHashMap<>(); private LocalDate yesterday = LocalDate.now(ZoneId.systemDefault()); @@ -271,8 +269,8 @@ private void downloadNovel(long chatId, int userId, String messageTxt, int downl } // Send confirmation message and store messageId to update progress novelly.execute(new SendMessage(chatId, "Downloading: "+novel.metadata.getTitle())); - int messageId = novelly.execute(new SendMessage(chatId, "Progress: ")).message().messageId(); - downloadMsgIds.put(chatId, messageId); + novel.telegramProgressMsgId = novelly.execute(new SendMessage(chatId, "Progress: ")).message().messageId(); + novel.downloadChapters(); novel.output(); @@ -297,9 +295,8 @@ private void downloadNovel(long chatId, int userId, String messageTxt, int downl } } - public void updateProgress(long chatId, int currChapter, int lastChapter) { - int messageId = (int) downloadMsgIds.get(chatId); - novelly.execute(new EditMessageText(chatId, messageId, "Progress: "+(currChapter+1)+"/"+lastChapter)); + public void updateProgress(long chatId, int msgId,int currChapter, int lastChapter) { + novelly.execute(new EditMessageText(chatId, msgId, "Progress: "+(currChapter+1)+"/"+lastChapter)); } public static void log(String msg) { diff --git a/src/main/java/grabber/Novel.java b/src/main/java/grabber/Novel.java index 7cbc963..e1ef85d 100644 --- a/src/main/java/grabber/Novel.java +++ b/src/main/java/grabber/Novel.java @@ -45,6 +45,7 @@ public class Novel { public String nextChapterURL; public String filename; public long telegramChatId; + public int telegramProgressMsgId; /** * Main novel download handling object. @@ -119,7 +120,7 @@ public void downloadChapters() throws InterruptedException{ init.gui.updateProgress(window); } if((telegramChatId) != 0 && (i % 10 == 0 || i == lastChapter-1)) { - init.telegramBot.updateProgress(telegramChatId, i, lastChapter); + init.telegramBot.updateProgress(telegramChatId, telegramProgressMsgId, i, lastChapter); } GrabberUtils.sleep(waitTime); } @@ -185,7 +186,7 @@ public void retry() throws InterruptedException { init.gui.updateProgress(window); } if((telegramChatId) != 0 && (i % 10 == 0 || i == failedChapters.size()-1)) { - init.telegramBot.updateProgress(telegramChatId, i, failedChapters.size()); + init.telegramBot.updateProgress(telegramChatId, telegramProgressMsgId, i, failedChapters.size()); } // replace with actual interrupted if(killTask) {