__init__.py 120 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
# ##############################################################################
echel0n's avatar
echel0n committed
21
22
import collections
import datetime
23
import functools
24
import os
Dustyn Gibson's avatar
Dustyn Gibson committed
25
import re
26
import time
27
import traceback
28
from concurrent.futures.thread import ThreadPoolExecutor
29
from urllib.parse import unquote_plus
Dustyn Gibson's avatar
Dustyn Gibson committed
30

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

36
import sickrage
37
from sickrage.core.caches import image_cache
38
39
from sickrage.core.common import dateFormat, dateTimeFormat, Overview, timeFormat, Quality, Qualities, EpisodeStatus
from sickrage.core.databases.main import MainDB
40
from sickrage.core.databases.main.schemas import TVEpisodeSchema
41
from sickrage.core.enums import ProcessMethod, SeriesProviderID, SearchFormat
42
from sickrage.core.exceptions import EpisodeNotFoundException, CantRemoveShowException, CantRefreshShowException, CantUpdateShowException
43
44
from sickrage.core.helpers import backup_app_data, srdatetime, pretty_file_size, read_file_buffered, try_int, sanitize_file_name, chmod_as_parent, flatten, \
    make_dir
45
46
47
48
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
49
from sickrage.core.queues.search import ManualSearchTask, BacklogSearchTask
50
51
from sickrage.core.tv.show.coming_episodes import ComingEpisodes, ComingEpsSortBy
from sickrage.core.tv.show.helpers import find_show, get_show_list
52
from sickrage.core.tv.show.history import History
53
from sickrage.series_providers.helpers import map_series_providers
54
from sickrage.subtitles import Subtitles
55

echel0n's avatar
echel0n committed
56
57
58
59
60
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
61
RESULT_DENIED = 60  # only use in Api.default() ! this is the access denied error
62

Gaëtan Muller's avatar
Gaëtan Muller committed
63
64
65
66
67
68
69
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
70
}
71

72
73
74
75
best_quality_list = [
    "sdtv", "sddvd", "hdtv", "rawhdtv", "fullhdtv", "hdwebdl", "fullhdwebdl", "hdbluray", "fullhdbluray",
    "udh4ktv", "uhd4kbluray", "udh4kwebdl", "udh8ktv", "uhd8kbluray", "udh8kwebdl"
]
76

77
78
any_quality_list = best_quality_list + ["unknown"]

79

80
class ApiV1BaseHandler(RequestHandler):
81
    """ api class that returns json results """
82
    version = 5  # use an int since float-point is unpredictable
echel0n's avatar
echel0n committed
83

84
    def __init__(self, application, request, **kwargs):
85
        super(ApiV1BaseHandler, self).__init__(application, request, **kwargs)
86
87
        self.executor = ThreadPoolExecutor(thread_name_prefix='APIv1-Thread')

88
    async def prepare(self, *args, **kwargs):
89
90
        # set the output callback
        # default json
91
        output_callback_dict = {
92
            'default': self._out_as_json,
Gaëtan Muller's avatar
Gaëtan Muller committed
93
            'image': self._out_as_image,
echel0n's avatar
echel0n committed
94
        }
95

96
        if sickrage.app.config.general.api_v1_key == self.path_args[0]:
97
98
            access_msg = "IP:{} - ACCESS GRANTED".format(self.request.remote_ip)
            sickrage.app.log.debug(access_msg)
echel0n's avatar
echel0n committed
99
100
101
102
103
104
105
106
107
108
109
110

            # 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:
111
                out_dict = await self.route(_call_dispatcher)
echel0n's avatar
echel0n committed
112
113
            except Exception as e:
                sickrage.app.log.error(str(e))
114
                error_data = {"error_msg": e, "request arguments": recursive_unicode(self.request.arguments)}
115
                out_dict = _responds(RESULT_FATAL, error_data, "SiCKRAGE encountered an internal error! Please report to the Devs")
echel0n's avatar
echel0n committed
116
        else:
117
118
            access_msg = "IP:{} - ACCESS DENIED".format(self.request.remote_ip)
            sickrage.app.log.debug(access_msg)
echel0n's avatar
echel0n committed
119

120
            error_data = {"error_msg": access_msg, "request arguments": recursive_unicode(self.request.arguments)}
121
            out_dict = _responds(RESULT_DENIED, error_data, access_msg)
122

123
124
125
        output_callback = output_callback_dict['default']
        if 'outputType' in out_dict:
            output_callback = output_callback_dict[out_dict['outputType']]
126

127
        await self.finish(output_callback(out_dict))
128

129
130
    async def route(self, method):
        kwargs = recursive_unicode(self.request.arguments)
echel0n's avatar
echel0n committed
131
132
133
134
        for arg, value in kwargs.items():
            if len(value) == 1:
                kwargs[arg] = value[0]

135
        return await IOLoop.current().run_in_executor(self.executor, functools.partial(method, **kwargs))
136

137
    def _out_as_image(self, _dict):
echel0n's avatar
echel0n committed
138
139
        self.set_header('Content-Type', _dict['image'].type)
        return _dict['image'].content
140

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

echel0n's avatar
echel0n committed
153
154
    @property
    def api_calls(self):
155
156
        """
        :return: api calls
157
        :rtype: Union[dict, object]
158
        """
159
        return dict((cls._cmd, cls) for cls in ApiV1Handler.__subclasses__() if '_cmd' in cls.__dict__)
echel0n's avatar
echel0n committed
160

161
    def call_dispatcher(self, *args, **kwargs):
162
163
164
165
166
        """ 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
        """
167
        sickrage.app.log.debug("all params: '" + str(kwargs) + "'")
168

echel0n's avatar
echel0n committed
169
170
171
172
        cmds = []
        if args:
            cmds, args = args[0], args[1:]
        cmds = kwargs.pop("cmd", cmds)
173
174

        outDict = {}
echel0n's avatar
echel0n committed
175
        if not len(cmds):
176
            outDict = CMD_SiCKRAGE(self.application, self.request, *args, **kwargs).run()
echel0n's avatar
echel0n committed
177
178
179
        else:
            cmds = cmds.split('|')

180
        multiCmds = bool(len(cmds) > 1)
echel0n's avatar
echel0n committed
181

182
183
184
185
186
187
        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

188
            sickrage.app.log.debug(cmd + ": current params " + str(curKwargs))
189
190
191
            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
192
193
194
                    # backport old sb calls
                    cmd = (cmd, 'sr' + cmd[2:])[cmd[:2] == 'sb']

195
                    if cmd in self.api_calls:
196
                        # call function and get response back
197
                        curOutDict = self.api_calls[cmd](self.application, self.request, *curArgs, **curKwargs).run()
198
                    elif _is_int(cmd):
199
                        curOutDict = TVDBShorthandWrapper(cmd, self.application, self.request, *curArgs, **curKwargs).run()
200
                    else:
201
                        curOutDict = _responds(RESULT_ERROR, "No such cmd: '" + cmd + "'")
202
                except InternalApiError as e:  # Api errors that we raised, they are harmless
echel0n's avatar
echel0n committed
203
                    curOutDict = _responds(RESULT_ERROR, msg=str(e))
204
205
206
207
208
209
210
            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
211
                if cmdIndex:  # do we need a index dict for this cmd ?
212
                    if cmd not in outDict:
213
214
                        outDict[cmd] = {}
                    outDict[cmd][cmdIndex] = curOutDict
echel0n's avatar
echel0n committed
215
216
                else:
                    outDict[cmd] = curOutDict
217
218
            else:
                outDict = curOutDict
219

echel0n's avatar
echel0n committed
220
221
        if multiCmds:
            outDict = _responds(RESULT_SUCCESS, outDict)
222

echel0n's avatar
echel0n committed
223
        return outDict
224
225

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

230
            all args and kwarks are lowerd
231

232
            cmd are separated by "|" e.g. &cmd=shows|future
233
            kwargs are namespaced with "." e.g. show.series_id=101501
234
            if a karg has no namespace asing it anyways (global)
235

236
            full e.g.
237
            /api?apikey=1234&cmd=show.seasonlist_asd|show.seasonlist_2&show.seasonlist_asd.series_id=101501&show.seasonlist_2.series_id=79488&sort=asc
238

239
240
            two calls of show.seasonlist
            one has the index "asd" the other one "2"
241
            the "indexerid" kwargs / params have the indexed cmd as a namspace
242
243
244
            and the kwarg / param "sort" is a used as a global
        """
        curArgs = []
245
246
247
        for arg in args[1:] or []:
            try:
                curArgs += [arg.lower()]
248
249
            except:
                continue
250

251
        curKwargs = {}
252
253
254
255
256
257
258
        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]
259
260
            except:
                continue
261

262
        return curArgs, curKwargs
263

echel0n's avatar
echel0n committed
264

265
class ApiV1Handler(ApiV1BaseHandler):
266
267
268
    _help = {"desc": "This command is not documented. Please report this to the developers."}

    def __init__(self, application, request, *args, **kwargs):
269
        super(ApiV1Handler, self).__init__(application, request)
echel0n's avatar
echel0n committed
270
271
272
273
        self._missing = []
        self._requiredParams = {}
        self._optionalParams = {}
        self.check_params(*args, **kwargs)
274

275
    def run(self):
276
277
278
        # override with real output function in subclass
        return {}

279
    def return_help(self):
280
281
        for paramDict, paramType in [(self._requiredParams, "requiredParameters"),
                                     (self._optionalParams, "optionalParameters")]:
282

283
            if paramType in self._help:
284
                for paramName in paramDict:
285
286
                    if paramName not in self._help[paramType]:
                        self._help[paramType][paramName] = {}
echel0n's avatar
echel0n committed
287
288
                    if paramDict[paramName]["allowedValues"]:
                        self._help[paramType][paramName]["allowedValues"] = paramDict[paramName]["allowedValues"]
289
                    else:
echel0n's avatar
echel0n committed
290
291
292
293
                        self._help[paramType][paramName]["allowedValues"] = "see desc"

                    self._help[paramType][paramName]["defaultValue"] = paramDict[paramName]["defaultValue"]
                    self._help[paramType][paramName]["type"] = paramDict[paramName]["type"]
294
295
296

            elif paramDict:
                for paramName in paramDict:
echel0n's avatar
echel0n committed
297
298
                    self._help[paramType] = {}
                    self._help[paramType][paramName] = paramDict[paramName]
299
            else:
300
                self._help[paramType] = {}
301

302
303
        msg = "No description available"
        if "desc" in self._help:
304
            msg = self._help["desc"]
305

306
        return _responds(RESULT_SUCCESS, self._help, msg)
307

308
    def return_missing(self):
309
310
311
312
        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"
313
        return _responds(RESULT_ERROR, msg=msg)
314

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

317
        """ function to check passed params for the shorthand wrapper
318
            and to detect missing/required params
319
        """
Nils Vogels's avatar
Nils Vogels committed
320

321
322
323
        if key == "series_id" and "tvdbid" in kwargs:
            key = "tvdbid"

echel0n's avatar
echel0n committed
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
        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
            }
344

echel0n's avatar
echel0n committed
345
346
347
348
349
350
            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
351

echel0n's avatar
echel0n committed
352
353
354
            if default:
                default = self._check_param_type(default, key, arg_type)
                self._check_param_value(default, key, allowed_values)
355

echel0n's avatar
echel0n committed
356
357
        if self._missing:
            setattr(self, "run", self.return_missing)
358

echel0n's avatar
echel0n committed
359
360
        if 'help' in kwargs:
            setattr(self, "run", self.return_help)
361
362
363

        return default, args

echel0n's avatar
echel0n committed
364
365
    @staticmethod
    def _check_param_type(value, name, arg_type):
366
        """ checks if value can be converted / parsed to arg_type
367
            will raise an error on failure
368
            or will convert it to arg_type and return new converted value
369
370
371
372
373
374
375
376
            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
377
        if arg_type == "int":
378
379
380
381
            if _is_int(value):
                value = int(value)
            else:
                error = True
382
        elif arg_type == "bool":
383
384
385
386
387
388
            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
389
            elif value not in (True, False):
390
                error = True
391
        elif arg_type == "list":
392
            value = value.split("|")
393
        elif arg_type == "string":
394
            pass
395
        elif arg_type == "ignore":
396
397
            pass
        else:
echel0n's avatar
echel0n committed
398
            sickrage.app.log.error('Invalid param type: "{}" can not be checked. Ignoring it.'.format(str(arg_type)))
399
400

        if error:
401
            raise InternalApiError(
echel0n's avatar
echel0n committed
402
403
                'param "{}" with given value "{}" could not be parsed into "{}"'.format(str(name), str(value),
                                                                                        str(arg_type)))
404
405
406

        return value

echel0n's avatar
echel0n committed
407
408
    @staticmethod
    def _check_param_value(value, name, allowed_values):
409
410
        """ 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
411
            if bool(allowed_value) is False a check is not performed and all values are excepted
412
        """
echel0n's avatar
echel0n committed
413
        if allowed_values:
414
415
416
            error = False
            if isinstance(value, list):
                for item in value:
echel0n's avatar
echel0n committed
417
                    if item not in allowed_values:
418
419
                        error = True
            else:
echel0n's avatar
echel0n committed
420
                if value not in allowed_values:
421
422
423
                    error = True

            if error:
424
425
                # 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
426
                    value) + "' is out of allowed range '" + str(allowed_values) + "'")
427

428

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

432
    def __init__(self, sid, application, request, *args, **kwargs):
433
        super(TVDBShorthandWrapper, self).__init__(application, request, *args, **kwargs)
echel0n's avatar
echel0n committed
434

435
436
437
438
        self.origArgs = args
        self.kwargs = kwargs
        self.sid = sid

439
440
        self.s, args = self.check_params("s", None, False, "ignore", [], *args, **kwargs)
        self.e, args = self.check_params("e", None, False, "ignore", [], *args, **kwargs)
441
442
        self.args = args

443
    def run(self):
444
445
446
        """ internal function wrapper """
        args = (self.sid,) + self.origArgs
        if self.e:
447
            return CMD_Episode(self.application, self.request, *args, **self.kwargs).run()
448
        elif self.s:
449
            return CMD_ShowSeasons(self.application, self.request, *args, **self.kwargs).run()
450
        else:
451
            return CMD_Show(self.application, self.request, *args, **self.kwargs).run()
452
453
454
455
456
457
458
459
460
461
462


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


463
def _responds(result_type, data=None, msg=""):
464
465
466
467
468
    """
    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
    """
469

470
    return {"result": result_type_map[result_type], "message": msg, "data": data or {}}
471
472


473
def _map_quality(show_object):
474
475
476
    anyQualities = []
    bestQualities = []

477
    iqualityID, aqualityID = Quality.split_quality(int(show_object))
echel0n's avatar
echel0n committed
478
    for quality in iqualityID:
479
        anyQualities.append(_get_quality_map()[quality])
echel0n's avatar
echel0n committed
480
    for quality in aqualityID:
481
        bestQualities.append(_get_quality_map()[quality])
482
483
484
    return anyQualities, bestQualities


echel0n's avatar
echel0n committed
485
def _get_quality_map():
486
    return {
487
488
        Qualities.SDTV: 'sdtv',
        'sdtv': Qualities.SDTV,
489

490
491
        Qualities.SDDVD: 'sddvd',
        'sddvd': Qualities.SDDVD,
492

493
494
        Qualities.HDTV: 'hdtv',
        'hdtv': Qualities.HDTV,
495

496
497
        Qualities.RAWHDTV: 'rawhdtv',
        'rawhdtv': Qualities.RAWHDTV,
498

499
500
        Qualities.FULLHDTV: 'fullhdtv',
        'fullhdtv': Qualities.FULLHDTV,
501

502
503
        Qualities.HDWEBDL: 'hdwebdl',
        'hdwebdl': Qualities.HDWEBDL,
504

505
506
        Qualities.FULLHDWEBDL: 'fullhdwebdl',
        'fullhdwebdl': Qualities.FULLHDWEBDL,
507

508
509
        Qualities.HDBLURAY: 'hdbluray',
        'hdbluray': Qualities.HDBLURAY,
510

511
512
        Qualities.FULLHDBLURAY: 'fullhdbluray',
        'fullhdbluray': Qualities.FULLHDBLURAY,
513

514
515
        Qualities.UHD_4K_TV: 'uhd4ktv',
        'udh4ktv': Qualities.UHD_4K_TV,
516

517
518
        Qualities.UHD_4K_BLURAY: '4kbluray',
        'uhd4kbluray': Qualities.UHD_4K_BLURAY,
519

520
521
        Qualities.UHD_4K_WEBDL: '4kwebdl',
        'udh4kwebdl': Qualities.UHD_4K_WEBDL,
522

523
524
        Qualities.UHD_8K_TV: 'uhd8ktv',
        'udh8ktv': Qualities.UHD_8K_TV,
525

526
527
        Qualities.UHD_8K_BLURAY: 'uhd8kbluray',
        'uhd8kbluray': Qualities.UHD_8K_BLURAY,
528

529
530
        Qualities.UHD_8K_WEBDL: 'udh8kwebdl',
        "udh8kwebdl": Qualities.UHD_8K_WEBDL,
531

532
533
        Qualities.UNKNOWN: 'unknown',
        'unknown': Qualities.UNKNOWN
534
    }
535
536


echel0n's avatar
echel0n committed
537
def _get_root_dirs():
538
    if sickrage.app.config.general.root_dirs == "":
539
540
541
        return {}

    rootDir = {}
542
543
    root_dirs = sickrage.app.config.general.root_dirs.split('|')
    default_index = int(sickrage.app.config.general.root_dirs.split('|')[0])
544

545
    rootDir["default_index"] = int(sickrage.app.config.general.root_dirs.split('|')[0])
546
547
548
549
550
551
552
    # 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
553
    root_dirs = [unquote_plus(x) for x in root_dirs]
554
555
556
557
558
559
560

    default_dir = root_dirs[default_index]

    dir_list = []
    for root_dir in root_dirs:
        valid = 1
        try:
561
            os.listdir(root_dir)
562
        except Exception:
563
564
565
566
567
            valid = 0
        default = 0
        if root_dir is default_dir:
            default = 1

568
        curDir = {'valid': valid, 'location': root_dir, 'default': default}
569
570
571
572
573
        dir_list.append(curDir)

    return dir_list


574
class InternalApiError(Exception):
575
576
577
    """
    Generic API error
    """
578
579
580


class IntParseError(Exception):
581
582
583
    """
    A value could not be parsed into an int, but should be parsable to an int
    """
echel0n's avatar
echel0n committed
584

585

586
class CMD_Help(ApiV1Handler):
echel0n's avatar
echel0n committed
587
    _cmd = "help"
Gaëtan Muller's avatar
Gaëtan Muller committed
588
589
590
591
592
    _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
593
    }
594

595
    def __init__(self, application, request, *args, **kwargs):
596
        super(CMD_Help, self).__init__(application, request, *args, **kwargs)
echel0n's avatar
echel0n committed
597
598
        self.subject, args = self.check_params("subject", "help", False, "string", self.api_calls.keys(), *args,
                                               **kwargs)
599

600
    def run(self):
Gaëtan Muller's avatar
Gaëtan Muller committed
601
        """ Get help about a given command """
602
        if self.subject in self.api_calls:
603
            api_func = self.api_calls.get(self.subject)
604
            out = api_func(self.application, self.request, **{"help": 1}).run()
605
606
607
608
609
        else:
            out = _responds(RESULT_FAILURE, msg="No such cmd")
        return out


610
class CMD_Backup(ApiV1Handler):
611
612
613
614
615
616
617
618
619
620
621
622
623
    _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)

624
    def run(self):
625
        """ Performs application backup """
echel0n's avatar
echel0n committed
626
        if backup_app_data(self.backup_dir):
627
628
629
630
631
632
633
            response = _responds(RESULT_SUCCESS, msg='Backup successful')
        else:
            response = _responds(RESULT_FAILURE, msg='Backup failed')

        return response


634
class CMD_ComingEpisodes(ApiV1Handler):
echel0n's avatar
echel0n committed
635
    _cmd = "future"
636
    _help = {
Gaëtan Muller's avatar
Gaëtan Muller committed
637
        "desc": "Get the coming episodes",
638
639
        "optionalParameters": {
            "sort": {"desc": "Change the sort order"},
Gaëtan Muller's avatar
Gaëtan Muller committed
640
            "type": {"desc": "One or more categories of coming episodes, separated by |"},
641
            "paused": {
642
                "desc": "0 to exclude paused shows, 1 to include them, or omitted to use SiCKRAGE default value"
643
644
            },
        }
echel0n's avatar
echel0n committed
645
    }
646

647
    def __init__(self, application, request, *args, **kwargs):
648
        super(CMD_ComingEpisodes, self).__init__(application, request, *args, **kwargs)
649
650
        self.sort, args = self.check_params("sort", ComingEpsSortBy.DATE.name.lower(), False, "string", [x.name.lower() for x in ComingEpsSortBy], *args,
                                            **kwargs)
651
652
        self.type, args = self.check_params("type", '|'.join(ComingEpisodes.categories), False, "list", ComingEpisodes.categories, *args, **kwargs)
        self.paused, args = self.check_params("paused", bool(sickrage.app.config.gui.coming_eps_display_paused), False, "bool", [], *args, **kwargs)
653

654
    def run(self):
Gaëtan Muller's avatar
Gaëtan Muller committed
655
        """ Get the coming episodes """
656
        grouped_coming_episodes = ComingEpisodes.get_coming_episodes(self.type, ComingEpsSortBy[self.sort.upper()], True, self.paused)
657
        data = dict([(section, []) for section in grouped_coming_episodes.keys()])
658

659
        for section, coming_episodes in grouped_coming_episodes.items():
660
661
            for coming_episode in coming_episodes:
                data[section].append({
662
663
664
665
666
                    'airdate': coming_episode['airdate'],
                    'airs': coming_episode['airs'],
                    'ep_name': coming_episode['name'],
                    'ep_plot': coming_episode['description'],
                    'episode': coming_episode['episode'],
667
                    'episode_id': coming_episode['episode_id'],
668
669
670
671
672
673
                    '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'],
674
675
                    'series_id': coming_episode['series_id'],
                    'series_provider_id': coming_episode['series_provider_id'],
676
                    'weekday': coming_episode['weekday']
677
                })
678

679
        return _responds(RESULT_SUCCESS, data)
680
681


682
class CMD_Episode(ApiV1Handler):
echel0n's avatar
echel0n committed
683
    _cmd = "episode"
Gaëtan Muller's avatar
Gaëtan Muller committed
684
685
686
    _help = {
        "desc": "Get detailed information about an episode",
        "requiredParameters": {
687
            "series_id": {"desc": "Unique ID of a show"},
Gaëtan Muller's avatar
Gaëtan Muller committed
688
689
690
691
            "season": {"desc": "The season number"},
            "episode": {"desc": "The episode number"},
        },
        "optionalParameters": {
692
            "series_provider_id": {"desc": "Unique ID of series provider"},
Gaëtan Muller's avatar
Gaëtan Muller committed
693
694
695
696
697
            "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
698
    }
699

700
    def __init__(self, application, request, *args, **kwargs):
701
        super(CMD_Episode, self).__init__(application, request, *args, **kwargs)
702
        self.series_id, args = self.check_params("series_id", None, True, "int", [], *args, **kwargs)
703
704
        self.series_provider_id, args = self.check_params("series_provider_id", sickrage.app.config.general.series_provider_default.value, False, "string",
                                                          [x.name.lower() for x in SeriesProviderID], *args, **kwargs)
705
706
707
        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)
708

709
    def run(self):
Gaëtan Muller's avatar
Gaëtan Muller committed
710
        """ Get detailed information about an episode """
711
712
        session = sickrage.app.main_db.session()

713
        show_obj = find_show(int(self.series_id), SeriesProviderID[self.series_provider_id.upper()])
714
        if not show_obj:
715
            return _responds(RESULT_FAILURE, msg="Show not found")
716

717
        try:
718
            db_data = session.query(MainDB.TVEpisode).filter_by(series_id=self.series_id, season=self.s, episode=self.e).one()
719
            episode_result = TVEpisodeSchema().dump(db_data)
720
721
722
723
724
725
726
727
728
729

            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
730
                episode_result['location'] = episode_result['location'][show_path_length:]
731
            elif not os.path.isdir(show_path):  # show dir is broken ... episode path will be empty
732
                episode_result['location'] = ""
733
734

            # convert stuff to human form
735
736
737
            if episode_result['airdate'] > datetime.date.min:  # 1900
                episode_result['airdate'] = srdatetime.SRDateTime(srdatetime.SRDateTime(
                    sickrage.app.tz_updater.parse_date_time(episode_result['airdate'], show_obj.airs, show_obj.network),
738
739
                    convert=True).dt).srfdate(d_preset=dateFormat)
            else:
740
                episode_result['airdate'] = 'Never'
741

742
            status, quality = Quality.split_composite_status(int(episode_result['status']))
743
744
            episode_result['status'] = status.display_name
            episode_result['quality'] = quality.display_name
745
            episode_result['file_size_human'] = pretty_file_size(episode_result['file_size'])
746

747
            return _responds(RESULT_SUCCESS, episode_result)
748
        except orm.exc.NoResultFound:
749
            raise InternalApiError("Episode not found")
750
751


752
class CMD_EpisodeSearch(ApiV1Handler):
echel0n's avatar
echel0n committed
753
    _cmd = "episode.search"
Gaëtan Muller's avatar
Gaëtan Muller committed
754
755
756
    _help = {
        "desc": "Search for an episode. The response might take some time.",
        "requiredParameters": {
757
            "series_id": {"desc": "Unique ID of a show"},
Gaëtan Muller's avatar
Gaëtan Muller committed
758
759
760
761
            "season": {"desc": "The season number"},
            "episode": {"desc": "The episode number"},
        },
        "optionalParameters": {
762
            "series_provider_id": {"desc": "Unique ID of series provider"},
Gaëtan Muller's avatar
Gaëtan Muller committed
763
764
            "tvdbid": {"desc": "thetvdb.com unique ID of a show"},
        }
echel0n's avatar
echel0n committed
765
    }
766

767
    def __init__(self, application, request, *args, **kwargs):
768
        super(CMD_EpisodeSearch, self).__init__(application, request, *args, **kwargs)
769
        self.series_id, args = self.check_params("series_id", None, True, "int", [], *args, **kwargs)
770
771
        self.series_provider_id, args = self.check_params("series_provider_id", sickrage.app.config.general.series_provider_default.value, False, "string",
                                                          [x.name.lower() for x in SeriesProviderID], *args, **kwargs)
772
773
        self.s, args = self.check_params("season", None, True, "int", [], *args, **kwargs)
        self.e, args = self.check_params("episode", None, True, "int", [], *args, **kwargs)
774

775
    def run(self):
Gaëtan Muller's avatar
Gaëtan Muller committed
776
        """ Search for an episode """
777
        show_object = find_show(int(self.series_id), SeriesProviderID[self.series_provider_id.upper()])
778
779
        if not show_object:
            return _responds(RESULT_FAILURE, msg="Show not found")
780
781

        # retrieve the episode object and fail if we can't get one
782
        try:
783
            epObj = show_object.get_episode(int(self.s), int(self.e))
784
        except EpisodeNotFoundException:
785
            return _responds(RESULT_FAILURE, msg="Episode not found")
786
787

        # make a queue item for it and put it on the queue
788
        ep_queue_item = ManualSearchTask(show_object.series_id, show_object.series_provider_id, epObj.season, epObj.episode)
789
        sickrage.app.search_queue.put(ep_queue_item)
790
791

        # wait until the queue item tells us whether it worked or not
echel0n's avatar
echel0n committed
792
        while not ep_queue_item.success: