def __on_download(self, folder, f): if (f.mimetype == f.DIRECTORY): dlg = FileDialog(FileDialog.TYPE_FOLDER, "Save To") dlg.run() destination = dlg.get_filename() if (destination): self.call_service(msgs.DOWNLOADER_SVC_GET_RECURSIVE, f, destination) else: dlg = FileDialog(FileDialog.TYPE_SAVE, "Save File") name = File.make_safe_name(f.name) if (os.path.splitext(f.name)[1]): ext = "" else: ext = os.path.splitext(f.resource)[1] dlg.set_filename(name + ext) dlg.run() destination = dlg.get_filename() if (destination): self.call_service(msgs.DOWNLOADER_SVC_GET, f.resource, destination)
def __on_download(self, folder, f): url = self.__get_video(f) if (not url): return dlg = FileDialog(FileDialog.TYPE_SAVE, "Save Video") name = File.make_safe_name(f.name) ext = mimetypes.mimetype_to_ext(f.mimetype) dlg.set_filename(name + ext) dlg.run() destination = dlg.get_filename() if (destination): self.call_service(msgs.DOWNLOADER_SVC_GET, url, destination)
def __retrieve_file(self, download_id, base_destination, queue): """ Retrieves a queue of files recursively. """ def on_data(data, amount, total, destname, destfile): self.__report_progress(download_id, destname, amount, 0) if (data == ""): # continue downloading next file in queue self.__retrieve_file(download_id, base_destination, queue) elif (data == None): # abort downloading try: os.unlink(destfile) except: pass self.emit_message(msgs.UI_ACT_SHOW_INFO, u"Download aborted") def child_collector(f, newdir, collection): if (f): collection.append(f) else: for c in collection: queue.append((c, newdir)) self.__retrieve_file(download_id, base_destination, queue) return True # queue consists of (f, destination) tuples with destination being a local directory if (queue): f, destination = queue.pop(0) else: # nothing to pop, we're done self.emit_message(msgs.DOWNLOADER_EV_FINISHED, download_id) self.emit_message(msgs.UI_ACT_SHOW_INFO, u"Finished downloading \xbb%s\xab" \ % os.path.basename(base_destination)) if (download_id in self.__downloaders): del self.__downloaders[download_id] # add to file index self.emit_message(msgs.FILEINDEX_ACT_SCAN_FOLDER, base_destination) return # if directory, recurse if (f.mimetype == f.DIRECTORY): # make directory for this name = File.make_safe_name(f.name) newdir = os.path.join(destination, name) try: os.mkdir(newdir) except: pass if (os.path.exists(newdir)): f.get_contents(0, 0, child_collector, newdir, []) if (not base_destination): base_destination = newdir # if file, download else: url = f.resource name = File.make_safe_name(f.name) ext = mimetypes.mimetype_to_ext(f.mimetype) destname = name + ext destfile = os.path.join(destination, destname) dloader = FileDownloader(url, destfile, on_data, destname, destfile) self.__downloaders[download_id] = dloader