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

20
21
from __future__ import unicode_literals

echel0n's avatar
echel0n committed
22
import sickrage
23
from adba.aniDBerrors import AniDBCommandTimeoutError
24

25

26
class BlackAndWhiteList(object):
27
28
    blacklist = []
    whitelist = []
29
30
31
32
33

    def __init__(self, show_id):
        if not show_id:
            raise BlackWhitelistNoShowIDException()
        self.show_id = show_id
34
35
36
        self.load()

    def load(self):
37
38
39
        """
        Builds black and whitelist
        """
40
        sickrage.app.log.debug('Building black and white list for ' + str(self.show_id))
41
42
        self.blacklist = self._load_list('blacklist')
        self.whitelist = self._load_list('whitelist')
43

44
    def _add_keywords(self, table, values):
45
46
47
        """
        DB: Adds keywords into database for current show

echel0n's avatar
echel0n committed
48
        :param table: database table to add keywords to
49
50
        :param values: Values to be inserted in table
        """
51
        for value in values:
52
            sickrage.app.main_db.insert({
echel0n's avatar
echel0n committed
53
54
55
56
                '_t': table,
                'show_id': self.show_id,
                'keywork': value
            })
57

58
    def set_black_keywords(self, values):
59
60
61
62
63
        """
        Sets blacklist to new value

        :param values: Complete list of keywords to be set as blacklist
        """
64
65
66
        self._del_all_keywords('blacklist')
        self._add_keywords('blacklist', values)
        self.blacklist = values
67
        sickrage.app.log.debug('Blacklist set to: %s' % self.blacklist)
68

69
    def set_white_keywords(self, values):
70
71
72
73
74
        """
        Sets whitelist to new value

        :param values: Complete list of keywords to be set as whitelist
        """
75
76
77
        self._del_all_keywords('whitelist')
        self._add_keywords('whitelist', values)
        self.whitelist = values
78
        sickrage.app.log.debug('Whitelist set to: %s' % self.whitelist)
79
80

    def _del_all_keywords(self, table):
81
82
83
        """
        DB: Remove all keywords for current show

echel0n's avatar
echel0n committed
84
        :param table: database table remove keywords from
85
        """
86
87
88
        dbData = sickrage.app.main_db.get(table, self.show_id)
        if dbData:
            sickrage.app.main_db.delete(sickrage.app.main_db.get(table, self.show_id))
89

90

91
    def _load_list(self, table):
92
93
94
95
96
97
98
        """
        DB: Fetch keywords for current show

        :param table: Table to fetch list of keywords from

        :return: keywords in list
        """
99
100
101
102
        try:
            groups = [x['keyword'] for x in sickrage.app.main_db.get_many(table, self.show_id)]
        except KeyError:
            groups = []
103

104
        sickrage.app.log.debug('BWL: ' + str(self.show_id) + ' loaded keywords from ' + table + ': ' + str(groups))
105

106
107
108
        return groups

    def is_valid(self, result):
109
110
111
112
113
114
        """
        Check if result is valid according to white/blacklist for current show

        :param result: Result to analyse
        :return: False if result is not allowed in white/blacklist, True if it is
        """
115

116
        if self.whitelist or self.blacklist:
117
            if not result.release_group:
118
                sickrage.app.log.debug('Failed to detect release group')
119
120
                return False

121
122
123
124
            if result.release_group.lower() in [x.lower() for x in self.whitelist]:
                white_result = True
            elif not self.whitelist:
                white_result = True
125
            else:
126
127
128
129
130
                white_result = False
            if result.release_group.lower() in [x.lower() for x in self.blacklist]:
                black_result = False
            else:
                black_result = True
131

132
            sickrage.app.log.debug(
echel0n's avatar
echel0n committed
133
                'Whitelist check passed: %s. Blacklist check passed: %s' % (white_result, black_result))
134

135
            if white_result and black_result:
136
                return True
137
            else:
138
                return False
139
        else:
140
            sickrage.app.log.debug('No Whitelist and  Blacklist defined')
141
142
            return True

143

144
class BlackWhitelistNoShowIDException(Exception):
145
    """No show_id was given"""
146

147

148
def short_group_names(groups):
149
150
151
152
153
154
    """
    Find AniDB short group names for release groups

    :param groups: list of groups to find short group names for
    :return: list of shortened group names
    """
155
156
    groups = groups.split(",")
    shortGroupList = []
157
    if sickrage.app.adba_connection:
158
        for groupName in groups:
159
            try:
160
                group = sickrage.app.adba_connection.group(gname=groupName)
161
            except AniDBCommandTimeoutError:
162
                sickrage.app.log.debug("Timeout while loading group from AniDB. Trying next group")
163
            except Exception:
164
                sickrage.app.log.debug("Failed while loading group from AniDB. Trying next group")
165
166
            else:
                for line in group.datalines:
167
168
                    if line["shortname"]:
                        shortGroupList.append(line["shortname"])
169
                    else:
170
                        if groupName not in shortGroupList:
171
                            shortGroupList.append(groupName)
172
173
    else:
        shortGroupList = groups
174
    return shortGroupList