__init__.py 3.02 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
14
15
16
# 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
echel0n's avatar
echel0n committed
17
# along with SickRage.  If not, see <http://www.gnu.org/licenses/>.
18

19
from __future__ import unicode_literals
echel0n's avatar
echel0n committed
20

21
import re
echel0n's avatar
echel0n committed
22
23

import sickrage
24
from sickrage.core.classes import ShowListUI
25
from sickrage.indexers.config import indexerConfig
echel0n's avatar
echel0n committed
26
27


28
class IndexerApi(object):
29
    def __init__(self, indexerID=1):
30
        self.indexerID = indexerID
31
        self.module = indexerConfig[self.indexerID]['module']
echel0n's avatar
echel0n committed
32
33

    def indexer(self, *args, **kwargs):
34
35
        self.module.settings(*args, **kwargs)
        return self.module
echel0n's avatar
echel0n committed
36
37
38

    @property
    def config(self):
39
        return indexerConfig[self.indexerID]
echel0n's avatar
echel0n committed
40
41
42

    @property
    def name(self):
43
        return indexerConfig[self.indexerID]['name']
echel0n's avatar
echel0n committed
44

echel0n's avatar
echel0n committed
45
46
47
48
    @property
    def trakt_id(self):
        return indexerConfig[self.indexerID]['trakt_id']

echel0n's avatar
echel0n committed
49
50
    @property
    def api_params(self):
51
52
        if sickrage.app.config.proxy_setting and sickrage.app.config.proxy_indexers:
            indexerConfig[self.indexerID]['api_params']['proxy'] = sickrage.app.config.proxy_setting
echel0n's avatar
echel0n committed
53

54
        return indexerConfig[self.indexerID]['api_params']
echel0n's avatar
echel0n committed
55
56
57

    @property
    def cache(self):
58
        return self.api_params['cache']
echel0n's avatar
echel0n committed
59
60
61

    @property
    def indexers(self):
62
        return dict((int(x['id']), x['name']) for x in indexerConfig.values())
echel0n's avatar
echel0n committed
63

echel0n's avatar
echel0n committed
64
65
66
67
    @property
    def indexersByTraktID(self):
        return dict((x['trakt_id'], int(x['id'])) for x in indexerConfig.values())

68
69
70
71
72
    def searchForShowID(self, regShowName, showid=None, ui=ShowListUI):
        """
        Contacts indexer to check for information on shows by showid

        :param regShowName: Name of show
73
        :param showid: Which indexer ID to look for
74
75
76
77
78
79
80
81
        :param ui: Custom UI for indexer use
        :return:
        """

        showNames = [re.sub('[. -]', ' ', regShowName)]

        # Query Indexers for each search term and build the list of results
        lINDEXER_API_PARMS = self.api_params.copy()
82
        lINDEXER_API_PARMS['custom_ui'] = ui
83
84
85
        t = self.indexer(**lINDEXER_API_PARMS)

        for name in showNames:
86
            sickrage.app.log.debug("Trying to find show {} on indexer {}".format(name, self.name))
87
88
89

            try:
                search = t[showid] if showid else t[name]
90
91
                seriesname = search['seriesname']
                series_id = search['id']
92
93
94
            except Exception:
                continue

95
            return seriesname, self.indexerID, int(series_id)
96
97

        return None, None, None