__init__.py 9.94 KB
Newer Older
1
# Author: echel0n <[email protected]>
echel0n's avatar
echel0n committed
2
# URL: https://sickrage.ca
3
#
echel0n's avatar
echel0n committed
4
# This file is part of SiCKRAGE.
5
#
echel0n's avatar
echel0n committed
6
# SiCKRAGE is free software: you can redistribute it and/or modify
7
8
9
10
# 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.
#
echel0n's avatar
echel0n committed
11
# SiCKRAGE is distributed in the hope that it will be useful,
12
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
#  GNU General Public License for more details.
15
16
#
# You should have received a copy of the GNU General Public License
echel0n's avatar
echel0n committed
17
# along with SiCKRAGE.  If not, see <http://www.gnu.org/licenses/>.
18

19

20
21
22
23
24
25
26
27
28
import re
import time
from base64 import b16encode, b32decode
from hashlib import sha1

from bencode3 import bdecode, BencodeError, bencode

import sickrage
from sickrage.core.websession import WebSession
29
from sickrage.search_providers import SearchProviderType
30

31
32
33
34
35
36
37
38
39
40
41
_clients = {
    'utorrent': 'uTorrentAPI',
    'transmission': 'TransmissionAPI',
    'deluge': 'DelugeAPI',
    'deluged': 'DelugeDAPI',
    'download_station': 'DownloadStationAPI',
    'rtorrent': 'rTorrentAPI',
    'qbittorrent': 'QBittorrentAPI',
    'mlnet': 'mlnetAPI',
    'putio': 'PutioAPI',
}
42

43

44
45
46
class GenericClient(object):
    def __init__(self, name, host=None, username=None, password=None):
        self.name = name
47
48
49
50
        self.username = sickrage.app.config.torrent.username if not username else username
        self.password = sickrage.app.config.torrent.password if not password else password
        self.host = sickrage.app.config.torrent.host if not host else host
        self.rpcurl = sickrage.app.config.torrent.rpc_url
51

52
        self.url = None
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
        self.auth = None
        self.last_time = time.time()

        self.session = WebSession(cache=False)

        self._response = None

    @property
    def response(self):
        return self._response

    @response.setter
    def response(self, value):
        self._response = value

    def _request(self, method='get', params=None, data=None, *args, **kwargs):
        if time.time() > self.last_time + 1800 or not self.auth:
            self.last_time = time.time()
            self._get_auth()

        sickrage.app.log.debug(
            '{name}: Requested a {method} connection to {url} with'
            ' params: {params} Data: {data}'.format(
                name=self.name,
                method=method.upper(),
78
                url=self.url,
79
80
81
82
83
84
85
86
87
                params=params,
                data=str(data)[0:99] + '...' if len(str(data)) > 102 else str(data)
            )
        )

        if not self.auth:
            sickrage.app.log.warning(self.name + ': Authentication Failed')
            return False

88
        self.response = self.session.request(method.upper(), self.url,
89
                                             params=params, data=data, auth=(self.username, self.password), timeout=120, verify=False, *args, **kwargs)
90

91
        if not self.response or not self.response.text:
92
93
            return False

94
95
96
97
98
99
        sickrage.app.log.debug('{name}: Response to {method} request is {response}'.format(
            name=self.name,
            method=method.upper(),
            response=self.response.text
        ))

100
101
102
103
104
105
106
107
108
        return True

    def _get_auth(self):
        """
        This should be overridden and should return the auth_id needed for the client
        """
        return None

    def test_authentication(self):
109
        # verify valid url
110
        self.response = self.session.get(self.url or self.host, timeout=120, verify=False)
111
        if self.response is None:
112
113
            return False, 'Error: Unable to connect to ' + self.name

114
115
116
117
118
119
        # verify auth
        if self._get_auth():
            return True, 'Success: Connected and Authenticated'
        return False, 'Error: Unable to get ' + self.name + ' Authentication, check your config!'


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
class TorrentClient(GenericClient):
    def _add_torrent_uri(self, result):
        """
        This should be overridden should return the True/False from the client
        when a torrent is added via url (magnet or .torrent link)
        """
        return False

    def _add_torrent_file(self, result):
        """
        This should be overridden should return the True/False from the client
        when a torrent is added via result.content (only .torrent file)
        """
        return False

    def _set_torrent_label(self, result):
        """
        This should be overridden should return the True/False from the client
        when a torrent is set with label
        """
        return True

    def _set_torrent_ratio(self, result):
        """
        This should be overridden should return the True/False from the client
        when a torrent is set with ratio
        """
        return True

    def _set_torrent_seed_time(self, result):
        """
        This should be overridden should return the True/False from the client
        when a torrent is set with a seed time
        """
        return True

    def _set_torrent_priority(self, result):
        """
        This should be overriden should return the True/False from the client
        when a torrent is set with result.priority (-1 = low, 0 = normal, 1 = high)
        """
        return True

    def _set_torrent_path(self, torrent_path):
        """
        This should be overridden should return the True/False from the client
        when a torrent is set with path
        """
        return True

    def _set_torrent_pause(self, result):
        """
        This should be overridden should return the True/False from the client
        when a torrent is set with pause
        """
        return True

    @staticmethod
    def _get_torrent_hash(result):
        if result.url.startswith('magnet'):
            result.hash = re.findall(r'urn:btih:([\w]{32,40})', result.url)[0]
            if len(result.hash) == 32:
                result.hash = b16encode(b32decode(result.hash)).lower()
        else:
            if not result.content:
                sickrage.app.log.warning('Torrent without content')
                raise Exception('Torrent without content')

            try:
                torrent_bdecode = bdecode(result.content)
            except BencodeError:
                sickrage.app.log.warning('Unable to bdecode torrent')
                sickrage.app.log.debug('Torrent bencoded data: %r' % result.content)
                raise

            try:
                info = torrent_bdecode["info"]
            except Exception:
                sickrage.app.log.warning('Unable to find info field in torrent')
                raise

            result.hash = sha1(bencode(info)).hexdigest()

        return result

    def send_torrent(self, result):

        r_code = False

        sickrage.app.log.debug('Calling ' + self.name + ' Client')

        try:
            if not self._get_auth():
                sickrage.app.log.warning(self.name + ': Authentication Failed')
                return r_code

            # Sets per provider seed ratio
            result.ratio = result.provider.seed_ratio

            # lazy fix for now, I'm sure we already do this somewhere else too
            result = self._get_torrent_hash(result)

            # convert to magnetic url if result has info hash and is not a private provider
223
            if sickrage.app.config.general.torrent_file_to_magnet:
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
                if result.hash and not result.provider.private and not result.url.startswith('magnet'):
                    result.url = "magnet:?xt=urn:btih:{}".format(result.hash)

            if result.url.startswith('magnet'):
                r_code = self._add_torrent_uri(result)
            else:
                r_code = self._add_torrent_file(result)

            if not r_code:
                sickrage.app.log.warning(self.name + ': Unable to send Torrent')
                return False

            if not self._set_torrent_pause(result):
                sickrage.app.log.warning(self.name + ': Unable to set the pause for Torrent')

            if not self._set_torrent_label(result):
                sickrage.app.log.warning(self.name + ': Unable to set the label for Torrent')

            if not self._set_torrent_ratio(result):
                sickrage.app.log.warning(self.name + ': Unable to set the ratio for Torrent')

            if not self._set_torrent_seed_time(result):
                sickrage.app.log.warning(self.name + ': Unable to set the seed time for Torrent')

            if not self._set_torrent_path(result):
                sickrage.app.log.warning(self.name + ': Unable to set the path for Torrent')

            if result.priority != 0 and not self._set_torrent_priority(result):
                sickrage.app.log.warning(self.name + ': Unable to set priority for Torrent')
        except Exception as e:
            sickrage.app.log.warning(self.name + ': Failed Sending Torrent')
            sickrage.app.log.debug(self.name + ': Exception raised when sending torrent: {}. Error: {}'.format(result, e))
            return r_code

        return r_code


class NZBClient(GenericClient):
    def _add_nzb_uri(self, result):
        """
        This should be overridden should return the True/False from the client
        when a torrent is added via url (magnet or .torrent link)
        """
        return False

    def _add_nzb_file(self, result):
        """
        This should be overridden should return the True/False from the client
        when a torrent is added via result.content (only .torrent file)
        """
        return False

    def send_nzb(self, result):
277
        if result.provider_type == SearchProviderType.NZB:
278
            return self._add_nzb_uri(result)
279
        elif result.provider_type == SearchProviderType.NZBDATA:
280
281
282
            return self._add_nzb_file(result)


283
def get_client_module(name, client_type):
284
    return __import__("{}.{}.{}".format(__name__, client_type, name.lower()), fromlist=list(_clients.keys()))
285

286

287
def get_client_instance(name, client_type):
288
    return getattr(get_client_module(name, client_type), _clients[name])