# coding=utf-8 from QMultiSourceFTP import QMultiSourceFtp from PyQt4.QtCore import QUrl from PyQt4.QtGui import QApplication def download_termine(b): print "Download finish" app.exit() if __name__ == "__main__": app = QApplication([]) download = QMultiSourceFtp() url = QUrl("ftp://localhost:2221/dossier1/c_un_test.mp3") url2 = QUrl("ftp://localhost:2221/dossier2/c_un_test.mp3") url3 = QUrl("ftp://localhost:2221/dossier3/c_un_test.mp3") url4 = QUrl("ftp://localhost:2221/dossier4/c_un_test.mp3") out_file = "c_un_test.mp3" urls = [url, url2, url3, url4] # Signaux download.done.connect(download_termine) download.get(urls, out_file) app.exec_()
class DownloadMultipleFtp(Download): progressModified = pyqtSignal(object) stateChanged = pyqtSignal(object) downloadFinished = pyqtSignal(object) speedModified = pyqtSignal(object) resultsReceived = pyqtSignal(list) def __init__(self, file_share, local_path, date, state): Download.__init__(self, file_share, local_path, date, state) self._urls = [] self._ftp = QMultiSourceFtp(self) self._is_downloading = False self._prev_state = -1 self.read_bytes = QFile(self.local_path).size() # Timer2 self.timer = QTimer() self.timer2 = QTimer() self.timer2.start(500) self.timer2.timeout.connect(self.update_speed) # Signaux self._ftp.dataTransferProgress.connect(self.update_progress) self._ftp.done.connect(self.download_finished) self._ftp.stateChanged.connect(self.state_changed) def start_download(self): self.ask_for_URLs() self.timer.start(1000 * 60 * 2) self.timer.timeout.connect(self.ask_for_URLs) self._urls.append(QUrl(self.file_share.url)) print "Starting download!" self._ftp.get(self._urls, self.local_path) def resume(self): self.ask_for_URLs() self.timer.start(1000 * 60 * 2) self.timer.timeout.connect(self.ask_for_URLs) # Signaux self._ftp.dataTransferProgress.connect(self.update_progress) self._ftp.done.connect(self.download_finished) self._ftp.stateChanged.connect(self.state_changed) print "Resuming download!" self._ftp.get(self._urls, self.local_path, True) def ask_for_URLs(self): """ 1) Envoie une recherche en background de fichiers similaires 2) Lorsque les urls sont arrivées, set_URLs met à jour les urls puis appelle manage_download 3) _ftp.Manage_download ajoute des urls pour aider au download""" print "Asking for urls" search = Search(self.file_share.name, self.file_share.protocol) # Signal self.resultsReceived.connect(self.set_URLs) search.do_search(self.resultsReceived.emit) def set_URLs(self, results): new_urls = [] for result in results: if result.url not in self._urls: new_urls.append(QUrl(result.url)) self._urls.append(QUrl(result.url)) self._ftp.manage_download(new_urls) def _stop_all(self): self._speed = 0 self.timer.stop() self.timer2.stop() try: self._ftp.done.disconnect(self.download_finished) self._ftp.stateChanged.disconnect(self.state_changed) self._ftp.dataTransferProgress.disconnect(self.update_progress) except TypeError: pass def pause(self): self._state = 5 self.stop() def stop(self): try: self._ftp.stop() except TypeError: pass self._stop_all() def state_changed(self, state): if state == 1 or state == 2: self._state = 1 elif state == 3 or state == 4: self._state = 3 self.stateChanged.emit(self) def download_finished(self, _): print "finished !" self._stop_all() self._state = 4 self.stateChanged.emit(self) self.downloadFinished.emit(self) def update_speed(self): delta = time.time() - self._last_time self._speed = float(self.read_bytes - self._last_size) / delta self.last_time = time.time() self.last_size = self.read_bytes self.speedModified.emit(self) def update_progress(self, read_bytes, total_bytes): self.read_bytes = read_bytes self.progressModified.emit(self)