api.py 111 KB
Newer Older
echel0n's avatar
echel0n committed
1
# ##############################################################################
2
3
4
#  Author: echel0n <[email protected]>
#  URL: https://sickrage.ca/
#  Git: https://git.sickrage.ca/SiCKRAGE/sickrage.git
echel0n's avatar
echel0n committed
5
#  -
6
#  This file is part of SiCKRAGE.
echel0n's avatar
echel0n committed
7
#  -
8
9
10
11
#  SiCKRAGE 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.
echel0n's avatar
echel0n committed
12
#  -
13
14
15
16
#  SiCKRAGE 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.
echel0n's avatar
echel0n committed
17
#  -
18
19
#  You should have received a copy of the GNU General Public License
#  along with SiCKRAGE.  If not, see <http://www.gnu.org/licenses/>.
echel0n's avatar
echel0n committed
20
# ##############################################################################
21

22

echel0n's avatar
echel0n committed
23
24
import collections
import datetime
25
import os
Dustyn Gibson's avatar
Dustyn Gibson committed
26
import re
echel0n's avatar
echel0n committed
27
import threading
28
import time
29
import traceback
30
from urllib.parse import unquote_plus
Dustyn Gibson's avatar
Dustyn Gibson committed
31

32
from sqlalchemy import orm
33
34
35
from tornado.escape import json_encode, recursive_unicode
from tornado.web import RequestHandler

36
import sickrage
37
from sickrage.core.caches import image_cache
38
from sickrage.core.common import ARCHIVED, DOWNLOADED, IGNORED, \
39
40
41
    Overview, Quality, SKIPPED, SNATCHED, SNATCHED_PROPER, UNAIRED, UNKNOWN, \
    WANTED, dateFormat, dateTimeFormat, get_quality_string, statusStrings, \
    timeFormat
echel0n's avatar
echel0n committed
42
43
from sickrage.core.databases.cache import CacheDB
from sickrage.core.databases.main import MainDB
44
45
from sickrage.core.exceptions import CantUpdateShowException, CantRemoveShowException, CantRefreshShowException, \
    EpisodeNotFoundException
echel0n's avatar
echel0n committed
46
47
from sickrage.core.helpers import chmod_as_parent, make_dir, \
    pretty_file_size, sanitize_file_name, srdatetime, try_int, read_file_buffered, backup_app_data
48
49
50
51
52
53
from sickrage.core.media.banner import Banner
from sickrage.core.media.fanart import FanArt
from sickrage.core.media.network import Network
from sickrage.core.media.poster import Poster
from sickrage.core.queues.search import BacklogQueueItem, ManualSearchQueueItem
from sickrage.core.tv.show.coming_episodes import ComingEpisodes
echel0n's avatar
echel0n committed
54
from sickrage.core.tv.show.helpers import find_show, get_show_list
55
from sickrage.core.tv.show.history import History
56
from sickrage.indexers import IndexerApi
57
from sickrage.indexers.exceptions import indexer_error, \
58
    indexer_showincomplete, indexer_shownotfound
echel0n's avatar
echel0n committed
59
60
from sickrage.indexers.helpers import map_indexers
from sickrage.indexers.ui import AllShowsUI
61
from sickrage.subtitles import Subtitles
62

63
indexer_ids = ["indexerid", "tvdbid"]
64

echel0n's avatar
echel0n committed
65
66
67
68
69
RESULT_SUCCESS = 10  # only use inside the run methods
RESULT_FAILURE = 20  # only use inside the run methods
RESULT_TIMEOUT = 30  # not used yet :(
RESULT_ERROR = 40  # only use outside of the run methods !
RESULT_FATAL = 50  # only use in Api.default() ! this is the "we encountered an internal error" error
Gaëtan Muller's avatar
Gaëtan Muller committed
70
RESULT_DENIED = 60  # only use in Api.default() ! this is the access denied error
71

Gaëtan Muller's avatar
Gaëtan Muller committed
72
73
74
75
76
77
78
result_type_map = {
    RESULT_SUCCESS: "success",
    RESULT_FAILURE: "failure",
    RESULT_TIMEOUT: "timeout",
    RESULT_ERROR: "error",
    RESULT_FATAL: "fatal",
    RESULT_DENIED: "denied",
echel0n's avatar
echel0n committed
79
}
80

81
82
83
84
85
86
87
best_quality_list = [
    "sdtv", "sddvd", "hdtv", "rawhdtv", "fullhdtv", "hdwebdl", "fullhdwebdl", "hdbluray", "fullhdbluray",
    "udh4ktv", "uhd4kbluray", "udh4kwebdl", "udh8ktv", "uhd8kbluray", "udh8kwebdl"
]

any_quality_list = best_quality_list + ["unknown"]

88

89
# basically everything except RESULT_SUCCESS / success is bad
echel0n's avatar
echel0n committed
90
class ApiHandler(RequestHandler):
91
    """ api class that returns json results """
92
    version = 5  # use an int since float-point is unpredictable
echel0n's avatar
echel0n committed
93

echel0n's avatar
echel0n committed
94
    async def prepare(self, *args, **kwargs):
echel0n's avatar
echel0n committed
95
        threading.currentThread().setName("API")
96

97
98
        # set the output callback
        # default json
99
        output_callback_dict = {
100
            'default': self._out_as_json,
Gaëtan Muller's avatar
Gaëtan Muller committed
101
            'image': self._out_as_image,
echel0n's avatar
echel0n committed
102
        }
103

echel0n's avatar
echel0n committed
104
        if sickrage.app.config.api_key == self.path_args[0]:
105
106
            access_msg = "IP:{} - ACCESS GRANTED".format(self.request.remote_ip)
            sickrage.app.log.debug(access_msg)
echel0n's avatar
echel0n committed
107
108
109
110
111
112
113
114
115
116
117
118

            # set the original call_dispatcher as the local _call_dispatcher
            _call_dispatcher = self.call_dispatcher

            # if profile was set wrap "_call_dispatcher" in the profile function
            if 'profile' in self.request.arguments:
                from profilehooks import profile

                _call_dispatcher = profile(_call_dispatcher, immediate=True)
                del self.request.arguments["profile"]

            try:
echel0n's avatar
echel0n committed
119
                out_dict = await self.route(_call_dispatcher, **self.request.arguments)
echel0n's avatar
echel0n committed
120
121
            except Exception as e:
                sickrage.app.log.error(str(e))
122
123
                error_data = {"error_msg": e, "request arguments": self.request.arguments}
                out_dict = _responds(RESULT_FATAL, error_data, "SiCKRAGE encountered an internal error! Please report to the Devs")
echel0n's avatar
echel0n committed
124
        else:
125
126
            access_msg = "IP:{} - ACCESS DENIED".format(self.request.remote_ip)
            sickrage.app.log.debug(access_msg)
echel0n's avatar
echel0n committed
127

128
129
            error_data = {"error_msg": access_msg, "request arguments": self.request.arguments}
            out_dict = _responds(RESULT_DENIED, error_data, access_msg)
130

131
132
133
        output_callback = output_callback_dict['default']
        if 'outputType' in out_dict:
            output_callback = output_callback_dict[out_dict['outputType']]
134

135
        await self.finish(output_callback(out_dict))
136

echel0n's avatar
echel0n committed
137
    async def route(self, function, **kwargs):
echel0n's avatar
echel0n committed
138
139
140
141
142
        kwargs = recursive_unicode(kwargs)
        for arg, value in kwargs.items():
            if len(value) == 1:
                kwargs[arg] = value[0]

echel0n's avatar
echel0n committed
143
        return await function(**kwargs)
144

145
    def _out_as_image(self, _dict):
echel0n's avatar
echel0n committed
146
147
        self.set_header('Content-Type', _dict['image'].type)
        return _dict['image'].content
148

149
    def _out_as_json(self, _dict):
Alexandre Beloin's avatar
Alexandre Beloin committed
150
        self.set_header("Content-Type", "application/json;charset=UTF-8")
151
        try:
152
            out = json_encode(_dict)
153
            callback = self.get_argument('callback', None) or self.get_argument('jsonp', None)
154
            if callback is not None:
155
                out = callback + '(' + out + ');'  # wrap with JSONP call if requested
156
        except Exception as e:  # if we fail to generate the output fake an error
157
            sickrage.app.log.debug(traceback.format_exc())
echel0n's avatar
echel0n committed
158
            out = '{"result": "%s", "message": "error while composing output: %s"}' % (result_type_map[RESULT_ERROR], e)
159
        return out
160

echel0n's avatar
echel0n committed
161
162
    @property
    def api_calls(self):
163
164
        """
        :return: api calls
165
        :rtype: Union[dict, object]
166
        """
echel0n's avatar
echel0n committed
167
168
        return dict((cls._cmd, cls) for cls in ApiCall.__subclasses__() if '_cmd' in cls.__dict__)

echel0n's avatar
echel0n committed
169
    async def call_dispatcher(self, *args, **kwargs):
170
171
172
173
174
        """ calls the appropriate CMD class
            looks for a cmd in args and kwargs
            or calls the TVDBShorthandWrapper when the first args element is a number
            or returns an error that there is no such cmd
        """
175
        sickrage.app.log.debug("all params: '" + str(kwargs) + "'")
176

echel0n's avatar
echel0n committed
177
178
179
180
        cmds = []
        if args:
            cmds, args = args[0], args[1:]
        cmds = kwargs.pop("cmd", cmds)
181
182

        outDict = {}
echel0n's avatar
echel0n committed
183
        if not len(cmds):
184
            outDict = await CMD_SiCKRAGE(self.application, self.request, *args, **kwargs).run()
echel0n's avatar
echel0n committed
185
186
187
        else:
            cmds = cmds.split('|')

188
        multiCmds = bool(len(cmds) > 1)
echel0n's avatar
echel0n committed
189

190
191
192
193
194
195
        for cmd in cmds:
            curArgs, curKwargs = self.filter_params(cmd, *args, **kwargs)
            cmdIndex = None
            if len(cmd.split("_")) > 1:  # was a index used for this cmd ?
                cmd, cmdIndex = cmd.split("_")  # this gives us the clear cmd and the index

196
            sickrage.app.log.debug(cmd + ": current params " + str(curKwargs))
197
198
199
            if not (multiCmds and cmd in ('show.getbanner', 'show.getfanart', 'show.getnetworklogo',
                                          'show.getposter')):  # skip these cmd while chaining
                try:
echel0n's avatar
echel0n committed
200
201
202
                    # backport old sb calls
                    cmd = (cmd, 'sr' + cmd[2:])[cmd[:2] == 'sb']

203
                    if cmd in self.api_calls:
204
                        # call function and get response back
echel0n's avatar
echel0n committed
205
                        curOutDict = await self.api_calls[cmd](self.application, self.request, *curArgs, **curKwargs).run()
206
                    elif _is_int(cmd):
echel0n's avatar
echel0n committed
207
                        curOutDict = await TVDBShorthandWrapper(cmd, self.application, self.request, *curArgs, **curKwargs).run()
208
                    else:
209
                        curOutDict = _responds(RESULT_ERROR, "No such cmd: '" + cmd + "'")
210
                except InternalApiError as e:  # Api errors that we raised, they are harmless
echel0n's avatar
echel0n committed
211
                    curOutDict = _responds(RESULT_ERROR, msg=str(e))
212
213
214
215
216
217
218
            else:  # if someone chained one of the forbiden cmds they will get an error for this one cmd
                curOutDict = _responds(RESULT_ERROR, msg="The cmd '" + cmd + "' is not supported while chaining")

            if multiCmds:
                # note: if multiple same cmds are issued but one has not an index defined it will override all others
                # or the other way around, this depends on the order of the cmds
                # this is not a bug
echel0n's avatar
echel0n committed
219
                if cmdIndex:  # do we need a index dict for this cmd ?
220
                    if cmd not in outDict:
221
222
                        outDict[cmd] = {}
                    outDict[cmd][cmdIndex] = curOutDict
echel0n's avatar
echel0n committed
223
224
                else:
                    outDict[cmd] = curOutDict
225
226
            else:
                outDict = curOutDict
227

echel0n's avatar
echel0n committed
228
229
        if multiCmds:
            outDict = _responds(RESULT_SUCCESS, outDict)
230

echel0n's avatar
echel0n committed
231
        return outDict
232
233

    def filter_params(self, cmd, *args, **kwargs):
234
235
236
        """ return only params kwargs that are for cmd
            and rename them to a clean version (remove "<cmd>_")
            args are shared across all cmds
237

238
            all args and kwarks are lowerd
239

240
            cmd are separated by "|" e.g. &cmd=shows|future
241
            kwargs are namespaced with "." e.g. show.indexer_id=101501
242
            if a karg has no namespace asing it anyways (global)
243

244
            full e.g.
245
            /api?apikey=1234&cmd=show.seasonlist_asd|show.seasonlist_2&show.seasonlist_asd.indexer_id=101501&show.seasonlist_2.indexer_id=79488&sort=asc
246

247
248
            two calls of show.seasonlist
            one has the index "asd" the other one "2"
249
            the "indexerid" kwargs / params have the indexed cmd as a namspace
250
251
252
            and the kwarg / param "sort" is a used as a global
        """
        curArgs = []
253
254
255
        for arg in args[1:] or []:
            try:
                curArgs += [arg.lower()]
256
257
            except:
                continue
258

259
        curKwargs = {}
260
261
262
263
264
265
266
        for kwarg in kwargs or []:
            try:
                if kwarg.find(cmd + ".") == 0:
                    cleanKey = kwarg.rpartition(".")[2]
                    curKwargs[cleanKey] = kwargs[kwarg].lower()
                elif not "." in kwarg:
                    curKwargs[kwarg] = kwargs[kwarg]
267
268
            except:
                continue
269

270
        return curArgs, curKwargs
271

echel0n's avatar
echel0n committed
272

echel0n's avatar
echel0n committed
273
class ApiCall(ApiHandler):
274
275
276
    _help = {"desc": "This command is not documented. Please report this to the developers."}

    def __init__(self, application, request, *args, **kwargs):
echel0n's avatar
echel0n committed
277
        super(ApiCall, self).__init__(application, request)
echel0n's avatar
echel0n committed
278
279
280
281
282
        self.indexer = 1
        self._missing = []
        self._requiredParams = {}
        self._optionalParams = {}
        self.check_params(*args, **kwargs)
283

echel0n's avatar
echel0n committed
284
    async def run(self):
285
286
287
        # override with real output function in subclass
        return {}

288
    async def return_help(self):
289
290
        for paramDict, paramType in [(self._requiredParams, "requiredParameters"),
                                     (self._optionalParams, "optionalParameters")]:
291

292
            if paramType in self._help:
293
                for paramName in paramDict:
294
295
                    if paramName not in self._help[paramType]:
                        self._help[paramType][paramName] = {}
echel0n's avatar
echel0n committed
296
297
                    if paramDict[paramName]["allowedValues"]:
                        self._help[paramType][paramName]["allowedValues"] = paramDict[paramName]["allowedValues"]
298
                    else:
echel0n's avatar
echel0n committed
299
300
301
302
                        self._help[paramType][paramName]["allowedValues"] = "see desc"

                    self._help[paramType][paramName]["defaultValue"] = paramDict[paramName]["defaultValue"]
                    self._help[paramType][paramName]["type"] = paramDict[paramName]["type"]
303
304
305

            elif paramDict:
                for paramName in paramDict:
echel0n's avatar
echel0n committed
306
307
                    self._help[paramType] = {}
                    self._help[paramType][paramName] = paramDict[paramName]
308
            else:
309
                self._help[paramType] = {}
310

311
312
        msg = "No description available"
        if "desc" in self._help:
313
            msg = self._help["desc"]
314

315
        return await _responds(RESULT_SUCCESS, self._help, msg)
316

317
    async def return_missing(self):
318
319
320
321
        if len(self._missing) == 1:
            msg = "The required parameter: '" + self._missing[0] + "' was not set"
        else:
            msg = "The required parameters: '" + "','".join(self._missing) + "' where not set"
322
        return await _responds(RESULT_ERROR, msg=msg)
323

echel0n's avatar
echel0n committed
324
    def check_params(self, key=None, default=None, required=None, arg_type=None, allowed_values=None, *args, **kwargs):
325

326
        """ function to check passed params for the shorthand wrapper
327
            and to detect missing/required params
328
        """
Nils Vogels's avatar
Nils Vogels committed
329

330
        # auto-select indexer
331
        if key in indexer_ids:
332
333
334
335
            if "tvdbid" in kwargs:
                key = "tvdbid"

            self.indexer = indexer_ids.index(key)
echel0n's avatar
echel0n committed
336

echel0n's avatar
echel0n committed
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
        if key:
            missing = True
            org_default = default

            if arg_type == "bool":
                allowed_values = [0, 1]

            if args:
                default = args[0]
                missing = False
                args = args[1:]
            if kwargs.get(key):
                default = kwargs.get(key)
                missing = False

            key_value = {
                "allowedValues": allowed_values,
                "defaultValue": org_default,
                "type": arg_type
            }
357

echel0n's avatar
echel0n committed
358
359
360
361
362
363
            if required:
                self._requiredParams[key] = key_value
                if missing and key not in self._missing:
                    self._missing.append(key)
            else:
                self._optionalParams[key] = key_value
364

echel0n's avatar
echel0n committed
365
366
367
            if default:
                default = self._check_param_type(default, key, arg_type)
                self._check_param_value(default, key, allowed_values)
368

echel0n's avatar
echel0n committed
369
370
        if self._missing:
            setattr(self, "run", self.return_missing)
371

echel0n's avatar
echel0n committed
372
373
        if 'help' in kwargs:
            setattr(self, "run", self.return_help)
374
375
376

        return default, args

echel0n's avatar
echel0n committed
377
378
    @staticmethod
    def _check_param_type(value, name, arg_type):
379
        """ checks if value can be converted / parsed to arg_type
380
            will raise an error on failure
381
            or will convert it to arg_type and return new converted value
382
383
384
385
386
387
388
389
            can check for:
            - int: will be converted into int
            - bool: will be converted to False / True
            - list: will always return a list
            - string: will do nothing for now
            - ignore: will ignore it, just like "string"
        """
        error = False
390
        if arg_type == "int":
391
392
393
394
            if _is_int(value):
                value = int(value)
            else:
                error = True
395
        elif arg_type == "bool":
396
397
398
399
400
401
            if value in ("0", "1"):
                value = bool(int(value))
            elif value in ("true", "True", "TRUE"):
                value = True
            elif value in ("false", "False", "FALSE"):
                value = False
Gaëtan Muller's avatar
Gaëtan Muller committed
402
            elif value not in (True, False):
403
                error = True
404
        elif arg_type == "list":
405
            value = value.split("|")
406
        elif arg_type == "string":
407
            pass
408
        elif arg_type == "ignore":
409
410
            pass
        else:
echel0n's avatar
echel0n committed
411
            sickrage.app.log.error('Invalid param type: "{}" can not be checked. Ignoring it.'.format(str(arg_type)))
412
413

        if error:
414
            raise InternalApiError(
echel0n's avatar
echel0n committed
415
416
                'param "{}" with given value "{}" could not be parsed into "{}"'.format(str(name), str(value),
                                                                                        str(arg_type)))
417
418
419

        return value

echel0n's avatar
echel0n committed
420
421
    @staticmethod
    def _check_param_value(value, name, allowed_values):
422
423
        """ will check if value (or all values in it ) are in allowed values
            will raise an exception if value is "out of range"
echel0n's avatar
echel0n committed
424
            if bool(allowed_value) is False a check is not performed and all values are excepted
425
        """
echel0n's avatar
echel0n committed
426
        if allowed_values:
427
428
429
            error = False
            if isinstance(value, list):
                for item in value:
echel0n's avatar
echel0n committed
430
                    if item not in allowed_values:
431
432
                        error = True
            else:
echel0n's avatar
echel0n committed
433
                if value not in allowed_values:
434
435
436
                    error = True

            if error:
437
438
                # this is kinda a InternalApiError but raising an error is the only way of quitting here
                raise InternalApiError("param: '" + str(name) + "' with given value: '" + str(
echel0n's avatar
echel0n committed
439
                    value) + "' is out of allowed range '" + str(allowed_values) + "'")
440

441

442
class TVDBShorthandWrapper(ApiCall):
Gaëtan Muller's avatar
Gaëtan Muller committed
443
    _help = {"desc": "This is an internal function wrapper. Call the help command directly for more information."}
444

445
    def __init__(self, sid, application, request, *args, **kwargs):
446
        super(TVDBShorthandWrapper, self).__init__(application, request, *args, **kwargs)
echel0n's avatar
echel0n committed
447

448
449
450
451
        self.origArgs = args
        self.kwargs = kwargs
        self.sid = sid

452
453
        self.s, args = self.check_params("s", None, False, "ignore", [], *args, **kwargs)
        self.e, args = self.check_params("e", None, False, "ignore", [], *args, **kwargs)
454
455
        self.args = args

echel0n's avatar
echel0n committed
456
    async def run(self):
457
458
459
        """ internal function wrapper """
        args = (self.sid,) + self.origArgs
        if self.e:
460
            return await CMD_Episode(self.application, self.request, *args, **self.kwargs).run()
461
        elif self.s:
462
            return await CMD_ShowSeasons(self.application, self.request, *args, **self.kwargs).run()
463
        else:
464
            return await CMD_Show(self.application, self.request, *args, **self.kwargs).run()
465
466


467
# ###############################
468
#       helper functions        #
469
# ###############################
470
471
472
473
474
475
476
477
478
479

def _is_int(data):
    try:
        int(data)
    except (TypeError, ValueError, OverflowError):
        return False
    else:
        return True


480
async def _responds(result_type, data=None, msg=""):
481
482
483
484
485
    """
    result is a string of given "type" (success/failure/timeout/error)
    message is a human readable string, can be empty
    data is either a dict or a array, can be a empty dict or empty array
    """
486

487
    return {"result": result_type_map[result_type], "message": msg, "data": data or {}}
488
489


echel0n's avatar
echel0n committed
490
def _get_status_strings(s):
491
    return statusStrings[s]
492
493


echel0n's avatar
echel0n committed
494
def _map_quality(showObj):
495
496
497
    anyQualities = []
    bestQualities = []

498
    iqualityID, aqualityID = Quality.split_quality(int(showObj))
echel0n's avatar
echel0n committed
499
    for quality in iqualityID:
500
        anyQualities.append(_get_quality_map()[quality])
echel0n's avatar
echel0n committed
501
    for quality in aqualityID:
502
        bestQualities.append(_get_quality_map()[quality])
503
504
505
    return anyQualities, bestQualities


echel0n's avatar
echel0n committed
506
def _get_quality_map():
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
    return {
        Quality.SDTV: 'sdtv',
        'sdtv': Quality.SDTV,

        Quality.SDDVD: 'sddvd',
        'sddvd': Quality.SDDVD,

        Quality.HDTV: 'hdtv',
        'hdtv': Quality.HDTV,

        Quality.RAWHDTV: 'rawhdtv',
        'rawhdtv': Quality.RAWHDTV,

        Quality.FULLHDTV: 'fullhdtv',
        'fullhdtv': Quality.FULLHDTV,

        Quality.HDWEBDL: 'hdwebdl',
        'hdwebdl': Quality.HDWEBDL,

        Quality.FULLHDWEBDL: 'fullhdwebdl',
        'fullhdwebdl': Quality.FULLHDWEBDL,

        Quality.HDBLURAY: 'hdbluray',
        'hdbluray': Quality.HDBLURAY,

        Quality.FULLHDBLURAY: 'fullhdbluray',
        'fullhdbluray': Quality.FULLHDBLURAY,

        Quality.UHD_4K_TV: 'uhd4ktv',
        'udh4ktv': Quality.UHD_4K_TV,

        Quality.UHD_4K_BLURAY: '4kbluray',
        'uhd4kbluray': Quality.UHD_4K_BLURAY,

        Quality.UHD_4K_WEBDL: '4kwebdl',
        'udh4kwebdl': Quality.UHD_4K_WEBDL,

        Quality.UHD_8K_TV: 'uhd8ktv',
        'udh8ktv': Quality.UHD_8K_TV,

        Quality.UHD_8K_BLURAY: 'uhd8kbluray',
        'uhd8kbluray': Quality.UHD_8K_BLURAY,

        Quality.UHD_8K_WEBDL: 'udh8kwebdl',
        "udh8kwebdl": Quality.UHD_8K_WEBDL,

        Quality.UNKNOWN: 'unknown',
        'unknown': Quality.UNKNOWN
    }
556
557


echel0n's avatar
echel0n committed
558
def _get_root_dirs():
559
    if sickrage.app.config.root_dirs == "":
560
561
562
        return {}

    rootDir = {}
563
564
    root_dirs = sickrage.app.config.root_dirs.split('|')
    default_index = int(sickrage.app.config.root_dirs.split('|')[0])
565

566
    rootDir["default_index"] = int(sickrage.app.config.root_dirs.split('|')[0])
567
568
569
570
571
572
573
    # remove default_index value from list (this fixes the offset)
    root_dirs.pop(0)

    if len(root_dirs) < default_index:
        return {}

    # clean up the list - replace %xx escapes by their single-character equivalent
574
    root_dirs = [unquote_plus(x) for x in root_dirs]
575
576
577
578
579
580
581

    default_dir = root_dirs[default_index]

    dir_list = []
    for root_dir in root_dirs:
        valid = 1
        try:
582
            os.listdir(root_dir)
583
        except Exception:
584
585
586
587
588
            valid = 0
        default = 0
        if root_dir is default_dir:
            default = 1

589
        curDir = {'valid': valid, 'location': root_dir, 'default': default}
590
591
592
593
594
        dir_list.append(curDir)

    return dir_list


595
class InternalApiError(Exception):
596
597
598
    """
    Generic API error
    """
599
600
601


class IntParseError(Exception):
602
603
604
    """
    A value could not be parsed into an int, but should be parsable to an int
    """
echel0n's avatar
echel0n committed
605

606

607
# -------------------------------------------------------------------------------------#
608
609
610


class CMD_Help(ApiCall):
echel0n's avatar
echel0n committed
611
    _cmd = "help"
Gaëtan Muller's avatar
Gaëtan Muller committed
612
613
614
615
616
    _help = {
        "desc": "Get help about a given command",
        "optionalParameters": {
            "subject": {"desc": "The name of the command to get the help of"},
        }
echel0n's avatar
echel0n committed
617
    }
618

619
    def __init__(self, application, request, *args, **kwargs):
620
        super(CMD_Help, self).__init__(application, request, *args, **kwargs)
echel0n's avatar
echel0n committed
621
622
        self.subject, args = self.check_params("subject", "help", False, "string", self.api_calls.keys(), *args,
                                               **kwargs)
623

echel0n's avatar
echel0n committed
624
    async def run(self):
Gaëtan Muller's avatar
Gaëtan Muller committed
625
        """ Get help about a given command """
626
        if self.subject in self.api_calls:
627
            api_func = self.api_calls.get(self.subject)
628
            out = api_func(self.application, self.request, **{"help": 1}).run()
629
630
631
632
633
        else:
            out = _responds(RESULT_FAILURE, msg="No such cmd")
        return out


634
635
636
637
638
639
640
641
642
643
644
645
646
647
class CMD_Backup(ApiCall):
    _cmd = "backup"
    _help = {
        "desc": "Backup application data files",
        "requiredParameters": {
            "backup_dir": {"desc": "Directory to store backup files"},
        }
    }

    def __init__(self, application, request, *args, **kwargs):
        super(CMD_Backup, self).__init__(application, request, *args, **kwargs)
        self.backup_dir, args = self.check_params("backup_dir", sickrage.app.data_dir, True, "string", [], *args,
                                                  **kwargs)

echel0n's avatar
echel0n committed
648
    async def run(self):
649
        """ Performs application backup """
echel0n's avatar
echel0n committed
650
        if backup_app_data(self.backup_dir):
651
652
653
654
655
656
657
            response = _responds(RESULT_SUCCESS, msg='Backup successful')
        else:
            response = _responds(RESULT_FAILURE, msg='Backup failed')

        return response


658
class CMD_ComingEpisodes(ApiCall):
echel0n's avatar
echel0n committed
659
    _cmd = "future"
660
    _help = {
Gaëtan Muller's avatar
Gaëtan Muller committed
661
        "desc": "Get the coming episodes",
662
663
        "optionalParameters": {
            "sort": {"desc": "Change the sort order"},
Gaëtan Muller's avatar
Gaëtan Muller committed
664
            "type": {"desc": "One or more categories of coming episodes, separated by |"},
665
            "paused": {
666
                "desc": "0 to exclude paused shows, 1 to include them, or omitted to use SiCKRAGE default value"
667
668
            },
        }
echel0n's avatar
echel0n committed
669
    }
670

671
    def __init__(self, application, request, *args, **kwargs):
672
        super(CMD_ComingEpisodes, self).__init__(application, request, *args, **kwargs)
673
674
        self.sort, args = self.check_params("sort", "date", False, "string", ComingEpisodes.sorts.keys(), *args,
                                            **kwargs)
675
676
        self.type, args = self.check_params("type", '|'.join(ComingEpisodes.categories), False, "list",
                                            ComingEpisodes.categories, *args, **kwargs)
677
        self.paused, args = self.check_params("paused", bool(sickrage.app.config.coming_eps_display_paused), False,
678
                                              "bool", [],
679
                                              *args, **kwargs)
680

echel0n's avatar
echel0n committed
681
    async def run(self):
Gaëtan Muller's avatar
Gaëtan Muller committed
682
        """ Get the coming episodes """
683
        grouped_coming_episodes = ComingEpisodes.get_coming_episodes(self.type, self.sort, True, self.paused)
684
        data = dict([(section, []) for section in grouped_coming_episodes.keys()])
685

686
        for section, coming_episodes in grouped_coming_episodes.items():
687
688
            for coming_episode in coming_episodes:
                data[section].append({
689
690
691
692
693
                    'airdate': coming_episode['airdate'],
                    'airs': coming_episode['airs'],
                    'ep_name': coming_episode['name'],
                    'ep_plot': coming_episode['description'],
                    'episode': coming_episode['episode'],
694
                    'indexer_id': coming_episode['indexer_id'],
695
696
697
698
699
700
701
702
                    'network': coming_episode['network'],
                    'paused': coming_episode['paused'],
                    'quality': coming_episode['quality'],
                    'season': coming_episode['season'],
                    'show_name': coming_episode['show_name'],
                    'show_status': coming_episode['status'],
                    'tvdbid': coming_episode['tvdbid'],
                    'weekday': coming_episode['weekday']
703
                })
704

705
        return await _responds(RESULT_SUCCESS, data)
706
707
708


class CMD_Episode(ApiCall):
echel0n's avatar
echel0n committed
709
    _cmd = "episode"
Gaëtan Muller's avatar
Gaëtan Muller committed
710
711
712
    _help = {
        "desc": "Get detailed information about an episode",
        "requiredParameters": {
713
            "indexerid": {"desc": "Unique ID of a show"},
Gaëtan Muller's avatar
Gaëtan Muller committed
714
715
716
717
718
719
720
721
722
            "season": {"desc": "The season number"},
            "episode": {"desc": "The episode number"},
        },
        "optionalParameters": {
            "tvdbid": {"desc": "thetvdb.com unique ID of a show"},
            "full_path": {
                "desc": "Return the full absolute show location (if valid, and True), or the relative show location"
            },
        }
echel0n's avatar
echel0n committed
723
    }
724

725
    def __init__(self, application, request, *args, **kwargs):
726
        super(CMD_Episode, self).__init__(application, request, *args, **kwargs)
727
        self.indexerid, args = self.check_params("indexerid", None, True, "int", [], *args, **kwargs)
728
729
730
        self.s, args = self.check_params("season", None, True, "int", [], *args, **kwargs)
        self.e, args = self.check_params("episode", None, True, "int", [], *args, **kwargs)
        self.fullPath, args = self.check_params("full_path", False, False, "bool", [], *args, **kwargs)
731

732
    async def run(self):
Gaëtan Muller's avatar
Gaëtan Muller committed
733
        """ Get detailed information about an episode """
734
735
736
        session = sickrage.app.main_db.session()

        show_obj = find_show(int(self.indexerid))
737
        if not show_obj:
738
            return await _responds(RESULT_FAILURE, msg="Show not found")
739

740
        try:
741
            episode = session.query(MainDB.TVEpisode).filter_by(showid=self.indexerid, season=self.s, episode=self.e).one()
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762

            show_path = show_obj.location

            # handle path options
            # absolute vs relative vs broken
            if bool(self.fullPath) is True and os.path.isdir(show_path):
                pass
            elif bool(self.fullPath) is False and os.path.isdir(show_path):
                # using the length because lstrip removes to much
                show_path_length = len(show_path) + 1  # the / or \ yeah not that nice i know
                episode.location = episode.location[show_path_length:]
            elif not os.path.isdir(show_path):  # show dir is broken ... episode path will be empty
                episode.location = ""

            # convert stuff to human form
            if episode.airdate > datetime.date.min:  # 1900
                episode.airdate = srdatetime.SRDateTime(srdatetime.SRDateTime(
                    sickrage.app.tz_updater.parse_date_time(episode.airdate, show_obj.airs, show_obj.network),
                    convert=True).dt).srfdate(d_preset=dateFormat)
            else:
                episode.airdate = 'Never'
763

764
765
766
767
            status, quality = Quality.split_composite_status(int(episode.status))
            episode.status = _get_status_strings(status)
            episode.quality = get_quality_string(quality)
            episode.file_size_human = pretty_file_size(episode.file_size)
768

769
            return await _responds(RESULT_SUCCESS, episode.as_dict())
770
        except orm.exc.NoResultFound:
771
            raise InternalApiError("Episode not found")
772
773
774


class CMD_EpisodeSearch(ApiCall):
echel0n's avatar
echel0n committed
775
    _cmd = "episode.search"
Gaëtan Muller's avatar
Gaëtan Muller committed
776
777
778
    _help = {
        "desc": "Search for an episode. The response might take some time.",
        "requiredParameters": {
779
            "indexerid": {"desc": "Unique ID of a show"},
Gaëtan Muller's avatar
Gaëtan Muller committed
780
781
782
783
784
785
            "season": {"desc": "The season number"},
            "episode": {"desc": "The episode number"},
        },
        "optionalParameters": {
            "tvdbid": {"desc": "thetvdb.com unique ID of a show"},
        }
echel0n's avatar
echel0n committed
786
    }
787

788
    def __init__(self, application, request, *args, **kwargs):
789
        super(CMD_EpisodeSearch, self).__init__(application, request, *args, **kwargs)
790
        self.indexerid, args = self.check_params("indexerid", None, True, "int", [], *args, **kwargs)
791
792
        self.s, args = self.check_params("season", None, True, "int", [], *args, **kwargs)
        self.e, args = self.check_params("episode", None, True, "int", [], *args, **kwargs)
793

794
    async def run(self):
Gaëtan Muller's avatar
Gaëtan Muller committed
795
        """ Search for an episode """
796
        showObj = find_show(int(self.indexerid))
797
        if not showObj:
798
            return await _responds(RESULT_FAILURE, msg="Show not found")
799
800

        # retrieve the episode object and fail if we can't get one
801
802
803
        try:
            epObj = showObj.get_episode(int(self.s), int(self.e))
        except EpisodeNotFoundException:
804
            return await _responds(RESULT_FAILURE, msg="Episode not found")
805
806

        # make a queue item for it and put it on the queue
807
        ep_queue_item = ManualSearchQueueItem(showObj, epObj.season, epObj.episode)
808
        sickrage.app.io_loop.add_callback(sickrage.app.search_queue.put, ep_queue_item)
809
810

        # wait until the queue item tells us whether it worked or not
echel0n's avatar
echel0n committed
811
        while not ep_queue_item.success:
812
            time.sleep(1)
813
814
815

        # return the correct json value
        if ep_queue_item.success:
816
            status, quality = Quality.split_composite_status(epObj.status)
817
            # TODO: split quality and status?
818
819
            return await _responds(RESULT_SUCCESS, {"quality": get_quality_string(quality)},
                                   "Snatched (" + get_quality_string(quality) + ")")
820

821
        return await _responds(RESULT_FAILURE, msg='Unable to find episode')
822
823
824


class CMD_EpisodeSetStatus(ApiCall):
echel0n's avatar
echel0n committed
825
    _cmd = "episode.setstatus"
Gaëtan Muller's avatar
Gaëtan Muller committed
826
827
828
    _help = {
        "desc": "Set the status of an episode or a season (when no episode is provided)",
        "requiredParameters": {
829
            "indexerid": {"desc": "Unique ID of a show"},
Gaëtan Muller's avatar
Gaëtan Muller committed
830
831
832
833
834
835
836
837
            "season": {"desc": "The season number"},
            "status": {"desc": "The status of the episode or season"}
        },
        "optionalParameters": {
            "episode": {"desc": "The episode number"},
            "force": {"desc": "True to replace existing downloaded episode or season, False otherwise"},
            "tvdbid": {"desc": "thetvdb.com unique ID of a show"},
        }
echel0n's avatar
echel0n committed
838
    }
839

840
    def __init__(self, application, request, *args, **kwargs):
841
        super(CMD_EpisodeSetStatus, self).__init__(application, request, *args, **kwargs)
842
        self.indexerid, args = self.check_params("indexerid", None, True, "int", [], *args, **kwargs)
843
844
845
846
847
        self.s, args = self.check_params("season", None, True, "int", [], *args, **kwargs)
        self.status, args = self.check_params("status", None, True, "string",
                                              ["wanted", "skipped", "ignored", "failed"], *args, **kwargs)
        self.e, args = self.check_params("episode", None, False, "int", [], *args, **kwargs)
        self.force, args = self.check_params("force", False, False, "bool", [], *args, **kwargs)
848

849
    async def run(self):
Gaëtan Muller's avatar
Gaëtan Muller committed
850
        """ Set the status of an episode or a season (when no episode is provided) """
851
        show_obj = find_show(int(self.indexerid))
852
        if not show_obj:
853
            return await _responds(RESULT_FAILURE, msg="Show not found")
854
855

        # convert the string status to a int
856
        for status in statusStrings.status_strings:
857
            if str(statusStrings[status]).lower() == str(self.status).lower():
858
859
                self.status = status
                break
echel0n's avatar
echel0n committed
860
        else:  # if we dont break out of the for loop we got here.
861
            # the allowed values has at least one item that could not be matched against the internal status strings
862
            raise InternalApiError("The status string could not be matched to a status. Report to Devs!")