diff --git a/src/nicelee/ui/item/DownloadInfoPanel.java b/src/nicelee/ui/item/DownloadInfoPanel.java index 0eca7230..960da19b 100644 --- a/src/nicelee/ui/item/DownloadInfoPanel.java +++ b/src/nicelee/ui/item/DownloadInfoPanel.java @@ -11,16 +11,15 @@ import javax.swing.JButton; import javax.swing.JLabel; import nicelee.ui.item.JOptionPane; +import nicelee.ui.thread.DownloadRunnableInternal; + import javax.swing.JPanel; import nicelee.bilibili.INeedAV; import nicelee.bilibili.downloaders.Downloader; import nicelee.bilibili.enums.StatusEnum; import nicelee.bilibili.model.ClipInfo; -import nicelee.bilibili.parsers.InputParser; -import nicelee.bilibili.util.CmdUtil; import nicelee.bilibili.util.Logger; -import nicelee.bilibili.util.RepoUtil; import nicelee.ui.Global; import nicelee.ui.TabDownload; @@ -246,44 +245,12 @@ public void stopTask() { */ public void continueTask() { stopOnQueue = false; - String record = avid_qn + "-p" + page; Downloader downloader = iNeedAV.getDownloader(); - final DownloadInfoPanel dp = this; // 如果正在下载 或 下载完毕,则不需要下载 StatusEnum status = downloader.currentStatus(); if (status != StatusEnum.DOWNLOADING && status != StatusEnum.SUCCESS && status != StatusEnum.PROCESSING) { downloader.startTask(); - Global.downLoadThreadPool.execute(new Runnable() { - @Override - public void run() { - if(downloader.currentStatus() == StatusEnum.NONE && dp.stopOnQueue) { - Logger.println("已经删除等待队列,无需再下载"); - return; - } - if (downloader.currentStatus() == StatusEnum.STOP) { - Logger.println("已经人工停止,无需再下载"); - return; - } - if(Global.reloadDownloadUrl && !avid.startsWith("h")){ - InputParser parser = iNeedAV.getInputParser(avid); - url = parser.getVideoLink(avid, cid, realqn, Global.downloadFormat); //该步含网络查询, 可能较为耗时 - if(realqn != parser.getVideoLinkQN()) { - Logger.println("清晰度链接已经改变,无法再重新下载"); - iNeedAV.getUtil().stopDownloadAsFail(); - return; - } - } - Logger.println("[重试]预期下载清晰度:" + qn + "实际清晰度:" + realqn); - // 开始下载 - if (downloader.download(url, avid, realqn, page)) { - // 下载成功后保存到仓库 - if (Global.saveToRepo) { - RepoUtil.appendAndSave(record); - } - CmdUtil.convertOrAppendCmdToRenameBat(avid_qn, formattedTitle, page); - } - } - }); + Global.downLoadThreadPool.execute(new DownloadRunnableInternal(this, System.currentTimeMillis(), true)); } } @@ -385,6 +352,10 @@ public void setAvid(String avid) { this.avid = avid; } + public String getCid() { + return cid; + } + public ClipInfo getClipInfo() { return clipInfo; } diff --git a/src/nicelee/ui/thread/DownloadRunnable.java b/src/nicelee/ui/thread/DownloadRunnable.java index 17e65eba..9547b345 100644 --- a/src/nicelee/ui/thread/DownloadRunnable.java +++ b/src/nicelee/ui/thread/DownloadRunnable.java @@ -4,15 +4,11 @@ import javax.swing.JPanel; -import nicelee.bilibili.API; import nicelee.bilibili.INeedAV; -import nicelee.bilibili.enums.StatusEnum; import nicelee.bilibili.exceptions.BilibiliError; import nicelee.bilibili.model.ClipInfo; import nicelee.bilibili.model.VideoInfo; -import nicelee.bilibili.parsers.InputParser; import nicelee.bilibili.util.CmdUtil; -import nicelee.bilibili.util.Logger; import nicelee.bilibili.util.RepoUtil; import nicelee.bilibili.util.ResourcesUtil; import nicelee.ui.Global; @@ -103,10 +99,8 @@ public void download() { urlQuery = clip.getLinks().get(0); realQN = 0; } - long urlTimestamp = System.currentTimeMillis(); // 生成格式化名称 - String formattedTitle = CmdUtil.genFormatedName( - avInfo,clip,realQN); + String formattedTitle = CmdUtil.genFormatedName(avInfo,clip,realQN); String avid_qn = avid + "-" + realQN; this.record = avid_qn + "-p" + page; //如果清晰度不符合预期,再判断一次记录 @@ -132,55 +126,7 @@ public void download() { JPanel jpContent = Global.downloadTab.getJpContent(); jpContent.add(downPanel); jpContent.setPreferredSize(new Dimension(1100, 128 * Global.downloadTaskList.size())); - Global.downLoadThreadPool.execute(new Runnable() { - @Override - public void run() { - try { - if(iNeedAV.getDownloader().currentStatus() == StatusEnum.NONE && downPanel.stopOnQueue) { - Logger.println("已经删除等待队列,无需再下载"); - return; - } - if(iNeedAV.getDownloader().currentStatus() == StatusEnum.STOP) { - Logger.println("已经人工停止,无需再下载"); - return; - } - long currentTime = System.currentTimeMillis(); - long deltaTime = currentTime - urlTimestamp; - String validUrl = urlQuery; - if(deltaTime > Global.urlValidPeriod && !avid.startsWith("h")) { - Logger.printf("下载url距离上次查询已经过了超过%d min,重新查询下载链接", Global.urlValidPeriod / 60000L); - InputParser parser = iNeedAV.getInputParser(avid); - validUrl = parser.getVideoLink(avid, cid, qn, Global.downloadFormat); - downPanel.url = validUrl; - if(realQN != parser.getVideoLinkQN()) { - Logger.println("清晰度链接已经改变,无法再重新下载"); - iNeedAV.getUtil().stopDownloadAsFail(); - return; - } - } - // 开始下载 - if(iNeedAV.downloadClip(validUrl, avid, iNeedAV.getInputParser(avid).getVideoLinkQN(), page)) { - // 下载成功后保存到仓库 - if(Global.saveToRepo) { - RepoUtil.appendAndSave(record); - } - if(Global.thumbUpAfterDownloaded && Global.isLogin && avid.startsWith("BV")) { - API.like(avid); - } - CmdUtil.convertOrAppendCmdToRenameBat(avid_qn, formattedTitle, page); - } - } catch (Exception e) { - e.printStackTrace(); - } - if(Global.sleepAfterDownloadComplete > 0) { - try { - Thread.sleep(Global.sleepAfterDownloadComplete); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - } - }); + Global.downLoadThreadPool.execute(new DownloadRunnableInternal(downPanel, System.currentTimeMillis(), false)); if(Global.sleepAfterDownloadQuery > 0) { try { Thread.sleep(Global.sleepAfterDownloadQuery); diff --git a/src/nicelee/ui/thread/DownloadRunnableInternal.java b/src/nicelee/ui/thread/DownloadRunnableInternal.java new file mode 100644 index 00000000..aaac72a6 --- /dev/null +++ b/src/nicelee/ui/thread/DownloadRunnableInternal.java @@ -0,0 +1,114 @@ +package nicelee.ui.thread; + +import nicelee.bilibili.API; +import nicelee.bilibili.INeedAV; +import nicelee.bilibili.downloaders.Downloader; +import nicelee.bilibili.enums.StatusEnum; +import nicelee.bilibili.parsers.InputParser; +import nicelee.bilibili.util.CmdUtil; +import nicelee.bilibili.util.Logger; +import nicelee.bilibili.util.RepoUtil; +import nicelee.ui.Global; +import nicelee.ui.item.DownloadInfoPanel; + +public class DownloadRunnableInternal implements Runnable { + + final DownloadInfoPanel downPanel; + final long urlTimestamp; + final boolean invokeByContinueTask; + // 下面这些值都可以从 downPanel 直接或者间接获取 + final String record; + final INeedAV iNeedAV; + final Downloader downloader; + final String urlQuery, avid, cid, avid_qn, formattedTitle; + final int qn, realQN, page; + + /** + *

一个有效的下载任务和下载面板的控件downPanel绑定。

+ *

为了防止该任务在线程池的队列里面等太久,需要urlTimestamp来标定url的有效性,以判断是否重新查询url

+ * + * @param downPanel 下载任务绑定的UI控件,相关信息可以从这里获取 + * @param urlTimestamp 当前下载链接的获取时间 + * @param invokeByContinueTask 该线程是否是 “继续任务”, 而不是 “开始任务” + */ + public DownloadRunnableInternal(DownloadInfoPanel downPanel, long urlTimestamp, boolean invokeByContinueTask) { + this.downPanel = downPanel; + this.urlTimestamp = urlTimestamp; + this.invokeByContinueTask = invokeByContinueTask; + iNeedAV = downPanel.iNeedAV; + downloader = iNeedAV.getDownloader(); + urlQuery = downPanel.url; + avid = downPanel.getAvid(); + cid = downPanel.getCid(); + avid_qn = downPanel.avid_qn; + formattedTitle = downPanel.formattedTitle; + qn = downPanel.getQn(); + realQN = downPanel.getRealqn(); + page = downPanel.getClipInfo().getPage(); + record = avid_qn + "-p" + page; + } + + @Override + public void run() { + try { + if (downloader.currentStatus() == StatusEnum.NONE && downPanel.stopOnQueue) { + Logger.println("已经删除等待队列,无需再下载"); + return; + } + if (downloader.currentStatus() == StatusEnum.STOP) { + Logger.println("已经人工停止,无需再下载"); + return; + } + // 判断是不是需要重新获取url + String validUrl = urlQuery; + if (!avid.startsWith("h")) { // 不是图片类型(该类型没啥好重新获取的) + boolean shouldReQuery = false; + // 如果是重新尝试下载,是否需要重新获取链接 + if (invokeByContinueTask && Global.reloadDownloadUrl) { + Logger.printf("重试时,重新查询下载链接"); + shouldReQuery = true; + } else { + // 在线程池里等待的时间是否超过了urlValidPeriod + long currentTime = System.currentTimeMillis(); + long deltaTime = currentTime - urlTimestamp; + if (deltaTime > Global.urlValidPeriod) { + Logger.printf("下载url距离上次查询已经过了超过%d min,重新查询下载链接", Global.urlValidPeriod / 60000L); + shouldReQuery = true; + } + } + if (shouldReQuery) {// 重新查询url + InputParser parser = iNeedAV.getInputParser(avid); + validUrl = parser.getVideoLink(avid, cid, qn, Global.downloadFormat); + downPanel.url = validUrl; + if (realQN != parser.getVideoLinkQN()) { + Logger.println("清晰度链接已经改变,无法再重新下载"); + iNeedAV.getUtil().stopDownloadAsFail(); + return; + } + } + } + // 开始下载 + if (iNeedAV.downloadClip(validUrl, avid, iNeedAV.getInputParser(avid).getVideoLinkQN(), page)) { + // 下载成功后保存到仓库 + if (Global.saveToRepo) { + RepoUtil.appendAndSave(record); + } + if (Global.thumbUpAfterDownloaded && Global.isLogin && avid.startsWith("BV")) { + API.like(avid); + } + CmdUtil.convertOrAppendCmdToRenameBat(avid_qn, formattedTitle, page); + } + } catch (Exception e) { + e.printStackTrace(); + } + if (Global.sleepAfterDownloadComplete > 0) { + try { + Thread.sleep(Global.sleepAfterDownloadComplete); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + } + +}