summaryrefslogtreecommitdiff
path: root/qutebrowser/misc/pastebin.py
blob: f24af9ead22e89b093479ce107eb9e2fdf573de1 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:

# Copyright 2014-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/>.

"""Client for the pastebin."""

import urllib.parse
from qutebrowser.qt import QtCore


class PastebinClient(QtCore.QObject):

    """A client for Stikked pastebins using HTTPClient.

    Attributes:
        _client: The HTTPClient used.

    Class attributes:
        API_URL: The base API URL.

    Signals:
        success: Emitted when the paste succeeded.
                 arg: The URL of the paste, as string.
        error: Emitted when the paste failed.
               arg: The error message, as string.
    """

    API_URL = 'https://crashes.qutebrowser.org/api/'
    MISC_API_URL = 'https://paste.the-compiler.org/api/'
    success = QtCore.pyqtSignal(str)
    error = QtCore.pyqtSignal(str)

    def __init__(self, client, parent=None, api_url=API_URL):
        """Constructor.

        Args:
            client: The HTTPClient to use. Will be reparented.
            api_url: The Stikked pastebin endpoint to use.
        """
        super().__init__(parent)
        client.setParent(self)
        client.error.connect(self.error)
        client.success.connect(self.on_client_success)
        self._client = client
        self._api_url = api_url

    def paste(self, name, title, text, parent=None, private=False):
        """Paste the text into a pastebin and return the URL.

        Args:
            name: The username to post as.
            title: The post title.
            text: The text to post.
            parent: The parent paste to reply to.
            private: Whether to paste privately.
        """
        data = {
            'text': text,
            'title': title,
            'name': name,
            'apikey': 'ihatespam',
        }
        if parent is not None:
            data['reply'] = parent
        if private:
            data['private'] = '1'

        url = QtCore.QUrl(urllib.parse.urljoin(self._api_url, 'create'))
        self._client.post(url, data)

    @QtCore.pyqtSlot(str)
    def on_client_success(self, data):
        """Process the data and finish when the client finished.

        Args:
            data: A string with the received data.
        """
        if data.startswith('http://') or data.startswith('https://'):
            self.success.emit(data)
        else:
            self.error.emit("Invalid data received in reply!")