summaryrefslogtreecommitdiff
path: root/qutebrowser/api/downloads.py
blob: 85f5b8b9101b99eb2eca1e47a9ec23486bb6e03c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:

# Copyright 2018-2021 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser.  If not, see <https://www.gnu.org/licenses/>.


"""APIs related to downloading files."""


import io

from qutebrowser.qt.core import QObject, pyqtSignal, pyqtSlot, QUrl

from qutebrowser.browser import downloads, qtnetworkdownloads
from qutebrowser.utils import objreg


UnsupportedAttribute = downloads.UnsupportedAttribute


class TempDownload(QObject):

    """A download of some data into a file object."""

    finished = pyqtSignal()

    def __init__(self, item: qtnetworkdownloads.DownloadItem) -> None:
        super().__init__()
        self._item = item
        self._item.finished.connect(self._on_download_finished)
        self.successful = False
        self.fileobj = item.fileobj

    @pyqtSlot()
    def _on_download_finished(self) -> None:
        self.successful = self._item.successful
        self.finished.emit()


def download_temp(url: QUrl) -> TempDownload:
    """Download the given URL into a file object.

    The download is not saved to disk.

    Returns a ``TempDownload`` object, which triggers a ``finished`` signal
    when the download has finished::

        dl = downloads.download_temp(QUrl("https://www.example.com/"))
        dl.finished.connect(functools.partial(on_download_finished, dl))

    After the download has finished, its ``successful`` attribute can be
    checked to make sure it finished successfully. If so, its contents can be
    read by accessing the ``fileobj`` attribute::

        def on_download_finished(download: downloads.TempDownload) -> None:
            if download.successful:
                print(download.fileobj.read())
                download.fileobj.close()
    """
    fobj = io.BytesIO()
    fobj.name = 'temporary: ' + url.host()
    target = downloads.FileObjDownloadTarget(fobj)
    download_manager = objreg.get('qtnetwork-download-manager')
    # cache=False is set as a WORKAROUND for MS Defender thinking we're a trojan
    # downloader when caching the hostblock list...
    return download_manager.get(url, target=target, auto_remove=True, cache=False)