Struggling with handling downloads from a custom browser made with PyQt6

38 Views Asked by At

I am struggling to implement downloads handling feature in my custom browser made with PyQt6. There are two download attempts and two errors I encountered while not changing any code in between testing those two downloads.

Here I make a class:

class DownloadManagerWidget(QWidget):
    def downloadRequested(self, download: QWebEngineDownloadRequest):
        assert download and download.state() == QWebEngineDownloadRequest.DownloadRequested

        # Prompt the user for a file name
        path, _ = QFileDialog.getSaveFileName(self, "Save as", QDir(download.downloadDirectory()).filePath(download.downloadFileName()))
        if path.isEmpty():
            return

        # Set download directory and file name
        download.setDownloadDirectory(QFileInfo(path).path())
        download.setDownloadFileName(QFileInfo(path).fileName())
        download.accept()

        # Add the download to the download manager
        self.add(DownloadWidget(download))

        # Show the download manager
        self.show()

This is the related download codes located in __init__ of class MainWindow(QMainWindow):

self.download_manager_widget = DownloadManagerWidget()
self.download_manager_widget.setAttribute(Qt.WidgetAttribute.WA_QuitOnClose, False)
QWebEngineProfile.defaultProfile().downloadRequested.connect(self.download_manager_widget.downloadRequested)

Tried downloading a file from download.cnet.com, I got this error:

js: Access to XMLHttpRequest at 'https://684d0d47.akstat.io/' from origin 'https://download.cnet.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Then, I tried another (downloading Chrome if I'm not mistaken):

Traceback (most recent call last): File "D:\mybrowser.py", line 14, in downloadRequested assert download and download.state() == QWebEngineDownloadRequest.DownloadRequested ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: type object 'QWebEngineDownloadRequest' has no attribute 'DownloadRequested'

I'm confused. Can you guide me in figuring out what I should do? Based on CORS policy error, I thought there is no error with the code but then when AttributeError popped up, doesn't that imply errors within the code?

0

There are 0 best solutions below