def initialize(): Events.on(Events.EVENT_MD5_START, EventHandler.print_md5_start) Events.on(Events.EVENT_MD5_END, EventHandler.print_md5_end) Events.on(Events.EVENT_DOWNLOAD_START, EventHandler.print_download_start) Events.on(Events.EVENT_DOWNLOAD_END, EventHandler.print_download_end) Events.on(Events.EVENT_PROGRESS, EventHandler.print_progress)
def download_file(self): """ Downloads a file from the location specified in the provided DownloadStruct. """ self.__create_directory() if not ConfigData.resume_downloads: self.remove() Events.trigger(Events.EVENT_DOWNLOAD_START, self.filename) if ConfigData.resume_downloads and self.local_file_size > 0: self.__resume_download() else: self.__start_download() Events.trigger(Events.EVENT_DOWNLOAD_END, self.filename)
def __download_file(self, web_request, mode, read_bytes=0): current_percentage = 0 with open(self.full_filename, mode) as f: # For a download that's resumed the content-length will be the remaining bytes, not the total. # total_length = int(web_request.headers.get("content-length")) total_length = self.humble_file_size chunk_size = ConfigData.chunk_size for chunk in web_request.iter_content(chunk_size=chunk_size): read_bytes += chunk_size read_bytes = min(total_length, read_bytes) current_percentage = Events.check_percent( read_bytes, total_length, current_percentage) if chunk: f.write(chunk) f.flush()