Beispiel #1
0
 def get(self, url, local_path):
     print "url", url
     print "local_path", local_path
     self.url = url
     self.local_path = local_path
     self.mb = DownloadMessageBox(self.parent())
     self.mb.buttonClicked.connect(self.handleCancel)
     self.mb.rejected.connect(self.handleReject)
     mgr = QNetworkAccessManager(self)
     mgr.finished.connect(self.handleFinished)
     self.reply = mgr.get(QNetworkRequest(QUrl(url)))
     self.reply.downloadProgress.connect(self.mb.updateProgress)
     self.mb.exec_()
Beispiel #2
0
class DownloadManager(QObject):
    dl_finished = Signal(str, str)

    def __init__(self):
        QObject.__init__(self)
        self.manager = QNetworkAccessManager()
        self.current_downloads = {}

        self.manager.finished.connect(self.download_finished)

    def do_download(self, dl_url):
        url = QUrl(dl_url)
        req = QNetworkRequest(url)
        reply = self.manager.get(req)
        self.current_downloads[reply] = dl_url

    def get_file_name(self, url):
        path = url.path()
        basename = QFileInfo(path).fileName()
        if not basename:
            basename = "download"
        if QFile.exists(basename):
            print "aaaaaaaaaaaaaaaah"
            return
        return basename

    def save_to_disk(self, filename, data):
        f = QFile(filename)
        if not f.open(QIODevice.WriteOnly):
            print "could not open %s for writing" % filename
            return False
        f.write(data.readAll())
        f.close()
        return True

    def execute(self):
        print "downloader on the run"
        dls = [
            "http://www.pyside.org/docs/pyside/_static/pysidelogo.png",
            "https://www.google.com/intl/en_com/images/srpr/logo3w.png"
        ]
        for dl in dls:
            self.do_download(dl)

    def download_finished(self, reply):
        url = reply.url()
        if reply.error():
            print "Dowload of %s failed" % url.toEncoded()
            return
        else:
            #filename = self.save_file_name(url)
            f = QTemporaryFile()
            if f.open():
                filename = f.fileName()
            else:
                print "Creation of tempfile for %s failed" % url.toEncoded()
                return

        if self.save_to_disk(filename, reply):
            print "Download of %s succeded (saved to %s)\n" % (url.toEncoded(),
                                                               filename)
            url = self.current_downloads.pop(reply)
            reply.deleteLater()
            self.dl_finished.emit(url, filename)

    def lastlog(self, url, filename):
        print url, "to", filename, "ok"
        if len(self.current_downloads) == 0:
            print "bye"
            QCoreApplication.instance().quit()