Browse Source

Change increase namecache size and fix deleting items from it when at capacity.

Add thread lock to NameParserCache.
Add exception handling for add to cache.
Change increase NameParserCache to 1000 entries.
Change NameParserCache to OrderedDefaultdict.
Add move_to_end to OrderedDefaultdict class (backported from python 3).
Add first_key, last_key to OrderedDefaultdict.
Change use much faster first_key in NameParserCache.
Change remove import of python pre 2.7 OrderedDict.
pull/1093/head
Prinz23 7 years ago
committed by JackDandy
parent
commit
c1d3487a21
  1. 1
      CHANGES.md
  2. 43
      sickbeard/classes.py
  3. 3
      sickbeard/databases/cache_db.py
  4. 19
      sickbeard/name_parser/parser.py
  5. 4
      sickbeard/providers/blutopia.py
  6. 4
      sickbeard/providers/fano.py
  7. 4
      sickbeard/providers/hdspace.py
  8. 4
      sickbeard/providers/hdtorrents.py
  9. 35
      sickbeard/providers/newznab.py
  10. 4
      sickbeard/providers/privatehd.py
  11. 4
      sickbeard/providers/ptf.py

1
CHANGES.md

@ -1,5 +1,6 @@
### 0.16.0 (2018-xx-xx xx:xx:xx UTC) ### 0.16.0 (2018-xx-xx xx:xx:xx UTC)
* Change increase namecache size and fix deleting items from it when at capacity
* Change improve security with cross-site request forgery (xsrf) protection on web forms * Change improve security with cross-site request forgery (xsrf) protection on web forms
* Change improve security by sending header flags httponly and secure with cookies * Change improve security by sending header flags httponly and secure with cookies
* Change improve security with DNS rebinding prevention, set "Allowed browser hostnames" at config/General/Web Interface * Change improve security with DNS rebinding prevention, set "Allowed browser hostnames" at config/General/Web Interface

43
sickbeard/classes.py

@ -15,18 +15,16 @@
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with SickGear. If not, see <http://www.gnu.org/licenses/>. # along with SickGear. If not, see <http://www.gnu.org/licenses/>.
import re from collections import OrderedDict
import datetime
import os
import sickbeard
from sickbeard.common import Quality from sickbeard.common import Quality
from unidecode import unidecode from unidecode import unidecode
try: import datetime
from collections import OrderedDict import os
except ImportError: import re
from requests.compat import OrderedDict
import sickbeard
class SearchResult(object): class SearchResult(object):
@ -343,6 +341,35 @@ class OrderedDefaultdict(OrderedDict):
args = (self.default_factory,) if self.default_factory else () args = (self.default_factory,) if self.default_factory else ()
return self.__class__, args, None, None, self.iteritems() return self.__class__, args, None, None, self.iteritems()
# backport from python 3
def move_to_end(self, key, last=True):
"""Move an existing element to the end (or beginning if last==False).
Raises KeyError if the element does not exist.
When last=True, acts like a fast version of self[key]=self.pop(key).
"""
link_prev, link_next, key = link = self._OrderedDict__map[key]
link_prev[1] = link_next
link_next[0] = link_prev
root = self._OrderedDict__root
if last:
last = root[0]
link[0] = last
link[1] = root
last[1] = root[0] = link
else:
first = root[1]
link[0] = root
link[1] = first
root[1] = first[0] = link
def first_key(self):
return self._OrderedDict__root[1][2]
def last_key(self):
return self._OrderedDict__root[0][2]
class ImageUrlList(list): class ImageUrlList(list):
def __init__(self, max_age=30): def __init__(self, max_age=30):

3
sickbeard/databases/cache_db.py

@ -16,8 +16,9 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with SickGear. If not, see <http://www.gnu.org/licenses/>. # along with SickGear. If not, see <http://www.gnu.org/licenses/>.
from sickbeard import db
from collections import OrderedDict from collections import OrderedDict
from sickbeard import db
import re import re
MIN_DB_VERSION = 1 MIN_DB_VERSION = 1

19
sickbeard/name_parser/parser.py

@ -23,6 +23,7 @@ import os
import os.path import os.path
import re import re
import time import time
import threading
import regexes import regexes
import sickbeard import sickbeard
@ -35,6 +36,7 @@ except ImportError:
from sickbeard import logger, helpers, scene_numbering, common, scene_exceptions, encodingKludge as ek, db from sickbeard import logger, helpers, scene_numbering, common, scene_exceptions, encodingKludge as ek, db
from sickbeard.exceptions import ex from sickbeard.exceptions import ex
from sickbeard.classes import OrderedDefaultdict
class NameParser(object): class NameParser(object):
@ -706,19 +708,30 @@ class ParseResult(object):
class NameParserCache(object): class NameParserCache(object):
_previous_parsed = {} def __init__(self):
_cache_size = 100 super(NameParserCache, self).__init__()
self._previous_parsed = OrderedDefaultdict()
self._cache_size = 1000
self.lock = threading.Lock()
def add(self, name, parse_result): def add(self, name, parse_result):
with self.lock:
self._previous_parsed[name] = parse_result self._previous_parsed[name] = parse_result
_current_cache_size = len(self._previous_parsed) _current_cache_size = len(self._previous_parsed)
if _current_cache_size > self._cache_size: if _current_cache_size > self._cache_size:
key = None
for i in range(_current_cache_size - self._cache_size): for i in range(_current_cache_size - self._cache_size):
del self._previous_parsed[self._previous_parsed.keys()[0]] try:
key = self._previous_parsed.first_key()
del self._previous_parsed[key]
except KeyError:
logger.log('Could not remove old NameParserCache entry: %s' % key, logger.DEBUG)
def get(self, name): def get(self, name):
with self.lock:
if name in self._previous_parsed: if name in self._previous_parsed:
logger.log('Using cached parse result for: ' + name, logger.DEBUG) logger.log('Using cached parse result for: ' + name, logger.DEBUG)
self._previous_parsed.move_to_end(name)
return self._previous_parsed[name] return self._previous_parsed[name]

4
sickbeard/providers/blutopia.py

@ -15,10 +15,8 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with SickGear. If not, see <http://www.gnu.org/licenses/>. # along with SickGear. If not, see <http://www.gnu.org/licenses/>.
try:
from collections import OrderedDict from collections import OrderedDict
except ImportError:
from requests.compat import OrderedDict
import re import re
import traceback import traceback

4
sickbeard/providers/fano.py

@ -15,10 +15,8 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with SickGear. If not, see <http://www.gnu.org/licenses/>. # along with SickGear. If not, see <http://www.gnu.org/licenses/>.
try:
from collections import OrderedDict from collections import OrderedDict
except ImportError:
from requests.compat import OrderedDict
import re import re
import traceback import traceback

4
sickbeard/providers/hdspace.py

@ -15,10 +15,8 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with SickGear. If not, see <http://www.gnu.org/licenses/>. # along with SickGear. If not, see <http://www.gnu.org/licenses/>.
try:
from collections import OrderedDict from collections import OrderedDict
except ImportError:
from requests.compat import OrderedDict
import re import re
import traceback import traceback

4
sickbeard/providers/hdtorrents.py

@ -15,10 +15,8 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with SickGear. If not, see <http://www.gnu.org/licenses/>. # along with SickGear. If not, see <http://www.gnu.org/licenses/>.
try:
from collections import OrderedDict from collections import OrderedDict
except ImportError:
from requests.compat import OrderedDict
import re import re
import traceback import traceback

35
sickbeard/providers/newznab.py

@ -17,26 +17,28 @@
# along with SickGear. If not, see <http://www.gnu.org/licenses/>. # along with SickGear. If not, see <http://www.gnu.org/licenses/>.
from __future__ import division from __future__ import division
from collections import OrderedDict
from math import ceil
import time
import sickbeard
import datetime import datetime
import re import re
import time
import urllib import urllib
from math import ceil
from sickbeard.sbdatetime import sbdatetime import sickbeard
from io import BytesIO
from lib.dateutil import parser
from . import generic from . import generic
from sickbeard import helpers, logger, tvcache, classes, db from sickbeard import classes, db, helpers, logger, tvcache
from sickbeard.common import neededQualities, Quality, SNATCHED, SNATCHED_PROPER, SNATCHED_BEST, DOWNLOADED from sickbeard.common import neededQualities, Quality, DOWNLOADED, SNATCHED, SNATCHED_PROPER, SNATCHED_BEST
from sickbeard.exceptions import AuthException, MultipleShowObjectsException from sickbeard.exceptions import AuthException, MultipleShowObjectsException
from sickbeard.helpers import tryInt
from sickbeard.indexers.indexer_config import * from sickbeard.indexers.indexer_config import *
from io import BytesIO
from lib.dateutil import parser
from sickbeard.network_timezones import sb_timezone from sickbeard.network_timezones import sb_timezone
from sickbeard.helpers import tryInt from sickbeard.sbdatetime import sbdatetime
from sickbeard.search import get_aired_in_season, get_wanted_qualities
from sickbeard.show_name_helpers import get_show_names from sickbeard.show_name_helpers import get_show_names
from sickbeard.search import get_wanted_qualities, get_aired_in_season
try: try:
from lxml import etree from lxml import etree
@ -46,11 +48,6 @@ except ImportError:
except ImportError: except ImportError:
import xml.etree.ElementTree as etree import xml.etree.ElementTree as etree
try:
from collections import OrderedDict
except ImportError:
from requests.compat import OrderedDict
class NewznabConstants: class NewznabConstants:
SEARCH_TEXT = -100 SEARCH_TEXT = -100
@ -101,8 +98,8 @@ class NewznabConstants:
class NewznabProvider(generic.NZBProvider): class NewznabProvider(generic.NZBProvider):
def __init__(self, name, url, key='', cat_ids=None, search_mode=None, def __init__(self, name, url, key='', cat_ids=None, search_mode=None, search_fallback=False,
search_fallback=False, enable_recentsearch=False, enable_backlog=False, enable_scheduled_backlog=False): enable_recentsearch=False, enable_backlog=False, enable_scheduled_backlog=False):
generic.NZBProvider.__init__(self, name, True, False) generic.NZBProvider.__init__(self, name, True, False)
self.url = url self.url = url
@ -235,8 +232,8 @@ class NewznabProvider(generic.NZBProvider):
limit = xml_caps.find('.//limits') limit = xml_caps.find('.//limits')
if None is not limit: if None is not limit:
l = helpers.tryInt(limit.get('max'), 100) lim = helpers.tryInt(limit.get('max'), 100)
self._limits = (100, l)[l >= 100] self._limits = (100, lim)[lim >= 100]
try: try:
for category in xml_caps.iter('category'): for category in xml_caps.iter('category'):

4
sickbeard/providers/privatehd.py

@ -15,10 +15,8 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with SickGear. If not, see <http://www.gnu.org/licenses/>. # along with SickGear. If not, see <http://www.gnu.org/licenses/>.
try:
from collections import OrderedDict from collections import OrderedDict
except ImportError:
from requests.compat import OrderedDict
import re import re
import traceback import traceback

4
sickbeard/providers/ptf.py

@ -15,10 +15,8 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with SickGear. If not, see <http://www.gnu.org/licenses/>. # along with SickGear. If not, see <http://www.gnu.org/licenses/>.
try:
from collections import OrderedDict from collections import OrderedDict
except ImportError:
from requests.compat import OrderedDict
import re import re
import time import time
import traceback import traceback

Loading…
Cancel
Save