2014-05-27 07:44:23 +00:00
|
|
|
# Author: Dennis Lutter <lad1337@gmail.com>
|
|
|
|
# URL: http://code.google.com/p/sickbeard/
|
|
|
|
#
|
|
|
|
# This file is part of Sick Beard.
|
|
|
|
#
|
|
|
|
# Sick Beard 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.
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
# along with Sick Beard. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
from sickbeard import db, logger
|
|
|
|
|
|
|
|
|
2015-01-19 14:27:48 +00:00
|
|
|
class BlackAndWhiteList(object):
|
|
|
|
blacklist = []
|
|
|
|
whitelist = []
|
2014-05-27 07:44:23 +00:00
|
|
|
|
|
|
|
def __init__(self, show_id):
|
|
|
|
if not show_id:
|
|
|
|
raise BlackWhitelistNoShowIDException()
|
|
|
|
self.show_id = show_id
|
2015-01-19 14:27:48 +00:00
|
|
|
self.load()
|
2014-05-27 07:44:23 +00:00
|
|
|
|
2015-01-19 14:27:48 +00:00
|
|
|
def load(self):
|
|
|
|
logger.log(u'Building black and white list for ' + str(self.show_id), logger.DEBUG)
|
|
|
|
self.blacklist = self._load_list('blacklist')
|
|
|
|
self.whitelist = self._load_list('whitelist')
|
2014-05-27 07:44:23 +00:00
|
|
|
|
2015-01-19 14:27:48 +00:00
|
|
|
def _add_keywords(self, table, values):
|
|
|
|
myDB = db.DBConnection()
|
|
|
|
for value in values:
|
|
|
|
myDB.action('INSERT INTO [' + table + '] (show_id, keyword) VALUES (?,?)', [self.show_id, value])
|
2014-05-27 07:44:23 +00:00
|
|
|
|
2015-01-19 14:27:48 +00:00
|
|
|
def set_black_keywords(self, values):
|
|
|
|
self._del_all_keywords('blacklist')
|
|
|
|
self._add_keywords('blacklist', values)
|
|
|
|
self.blacklist = values
|
|
|
|
logger.log('Blacklist set to: %s' % self.blacklist, logger.DEBUG)
|
2014-05-27 07:44:23 +00:00
|
|
|
|
2015-01-19 14:27:48 +00:00
|
|
|
def set_white_keywords(self, values):
|
|
|
|
self._del_all_keywords('whitelist')
|
|
|
|
self._add_keywords('whitelist', values)
|
|
|
|
self.whitelist = values
|
|
|
|
logger.log('Whitelist set to: %s' % self.whitelist, logger.DEBUG)
|
2014-05-27 07:44:23 +00:00
|
|
|
|
2015-01-19 14:27:48 +00:00
|
|
|
def _del_all_keywords(self, table):
|
|
|
|
myDB = db.DBConnection()
|
|
|
|
myDB.action('DELETE FROM [' + table + '] WHERE show_id = ?', [self.show_id])
|
2014-05-27 07:44:23 +00:00
|
|
|
|
2015-01-19 14:27:48 +00:00
|
|
|
def _load_list(self, table):
|
2014-06-21 22:46:59 +00:00
|
|
|
myDB = db.DBConnection()
|
2015-01-19 14:27:48 +00:00
|
|
|
sqlResults = myDB.select('SELECT keyword FROM [' + table + '] WHERE show_id = ?', [self.show_id])
|
|
|
|
if not sqlResults or not len(sqlResults):
|
|
|
|
return []
|
2014-06-21 22:46:59 +00:00
|
|
|
|
2015-01-19 14:27:48 +00:00
|
|
|
groups = []
|
|
|
|
for result in sqlResults:
|
|
|
|
groups.append(result["keyword"])
|
2014-05-27 07:44:23 +00:00
|
|
|
|
2015-01-19 14:27:48 +00:00
|
|
|
logger.log('BWL: ' + str(self.show_id) + ' loaded keywords from ' + table + ': ' + str(groups), logger.DEBUG)
|
2014-05-27 07:44:23 +00:00
|
|
|
|
2015-01-19 14:27:48 +00:00
|
|
|
return groups
|
2014-05-27 07:44:23 +00:00
|
|
|
|
2015-01-19 14:27:48 +00:00
|
|
|
def is_valid(self, result):
|
2014-05-27 07:44:23 +00:00
|
|
|
|
2015-01-19 14:27:48 +00:00
|
|
|
if result.release_group.lower() in [x.lower() for x in self.whitelist]:
|
|
|
|
white_result = True
|
|
|
|
else:
|
|
|
|
white_result = False
|
2014-05-27 07:44:23 +00:00
|
|
|
|
2015-01-19 14:27:48 +00:00
|
|
|
if result.release_group.lower() in [x.lower() for x in self.blacklist]:
|
|
|
|
black_result = False
|
|
|
|
else:
|
|
|
|
black_result = True
|
2014-05-27 07:44:23 +00:00
|
|
|
|
2015-01-19 14:27:48 +00:00
|
|
|
logger.log('Whitelist check passed: %s. Blacklist check passed: %s' % (white_result, black_result), logger.DEBUG)
|
2014-05-27 07:44:23 +00:00
|
|
|
|
2015-01-19 14:27:48 +00:00
|
|
|
if white_result and black_result:
|
2014-05-27 07:44:23 +00:00
|
|
|
return True
|
|
|
|
else:
|
2015-01-19 14:27:48 +00:00
|
|
|
return False
|
2014-05-27 07:44:23 +00:00
|
|
|
|
|
|
|
class BlackWhitelistNoShowIDException(Exception):
|
2015-01-19 14:27:48 +00:00
|
|
|
'No show_id was given'
|