Skip to content

Commit

Permalink
Fixed concurrent downloads updating the same progress message
Browse files Browse the repository at this point in the history
  • Loading branch information
Flameish committed Apr 19, 2021
1 parent 89f11ac commit 8089701
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
11 changes: 4 additions & 7 deletions src/main/java/bots/Telegram.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,7 +49,6 @@ public class Telegram {
"Example:\n" +
" -link http://novelhost.com/novel/ -chapters 5 10 -getImages";
private final ConcurrentHashMap<Integer, Novel[]> 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());
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/grabber/Novel.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class Novel {
public String nextChapterURL;
public String filename;
public long telegramChatId;
public int telegramProgressMsgId;

/**
* Main novel download handling object.
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 8089701

Please sign in to comment.