Commit aed8f070 authored by John's avatar John
Browse files

fix progress bar

parent 863f36b1
Loading
Loading
Loading
Loading
+87 −9
Original line number Diff line number Diff line
from gallery_dl.job import DownloadJob
from gallery_dl import config
from gallery_dl import config, util
from qtpy.QtCore import Signal, QThread
from utils.logger import logger as LOGGER
from ui.misc import ProgramConfig
@@ -7,9 +7,10 @@ from ui.imgtrans_proj import ProjImgTrans
from ui.constants import DOWNLOAD_PATH
import os


class SourceDownload(QThread):
    open_downloaded_proj = Signal(str)
    update_progress_bar = Signal(int)
    open_downloaded_proj = Signal(str)
    finished_downloading = Signal()

    def __init__(self, config: ProgramConfig, imgtrans_proj: ProjImgTrans, *args, **kwargs):
@@ -30,7 +31,8 @@ class SourceDownload(QThread):
    def FetchImages(self):
        config.load()
        config.set((), "skip", False)
        job = DownloadJob(self.url)
        job = SourceJob(self.url)
        job.update_progress_bar_job.connect(self.update_progress_bar.emit)
        job.run()

    def FindNewestFolderAndSetPath(self):
@@ -62,12 +64,6 @@ class SourceDownload(QThread):
        self.open_downloaded_proj.emit(proj_path)

    def _SyncSourceDownload(self):
        #  TODO keep track of downloaded page
        import time
        # for i in range(100):
        #     self.update_progress_bar.emit(i)
        #     LOGGER.info(i)
        #     time.sleep(0.05)
        self.url = self.config_pnl.src_link_flag
        if self.url:
            LOGGER.info(f'Url set to {self.url}')
@@ -86,3 +82,85 @@ class SourceDownload(QThread):

    def run(self):
        self._SyncSourceDownload()


class SourceJob(DownloadJob, QThread):
    update_progress_bar_job = Signal(int)

    def __init__(self, url):
        QThread.__init__(self)
        DownloadJob.__init__(self, url)
        self.progress = 0

    def handle_url(self, url, kwdict):
        """Download the resource specified in 'url'"""
        hooks = self.hooks
        pathfmt = self.pathfmt
        archive = self.archive

        progress_chunk = round(100 / kwdict['count'])

        # prepare download
        pathfmt.set_filename(kwdict)

        if "prepare" in hooks:
            for callback in hooks["prepare"]:
                callback(pathfmt)

        if archive and archive.check(kwdict):
            pathfmt.fix_extension()
            self.handle_skip()
            return

        if pathfmt.extension and not self.metadata_http:
            pathfmt.build_path()

            if pathfmt.exists():
                if archive:
                    archive.add(kwdict)
                self.handle_skip()
                return

        if self.sleep:
            self.extractor.sleep(self.sleep(), "download")

        # download from URL
        if not self.download(url):

            # use fallback URLs if available/enabled
            fallback = kwdict.get("_fallback", ()) if self.fallback else ()
            for num, url in enumerate(fallback, 1):
                util.remove_file(pathfmt.temppath)
                self.log.info("Trying fallback URL #%d", num)
                if self.download(url):
                    break
            else:
                # download failed
                self.status |= 4
                self.log.error("Failed to download %s",
                               pathfmt.filename or url)
                return

        if not pathfmt.temppath:
            if archive:
                archive.add(kwdict)
            self.handle_skip()
            return

        # run post processors
        if "file" in hooks:
            for callback in hooks["file"]:
                callback(pathfmt)

        # download succeeded
        self.progress += progress_chunk
        self.update_progress_bar_job.emit(self.progress)
        pathfmt.finalize()
        self.out.success(pathfmt.path)
        self._skipcnt = 0
        if archive:
            archive.add(kwdict)
        if "after" in hooks:
            for callback in hooks["after"]:
                callback(pathfmt)