SickGear/sickbeard/providers/rsstorrent.py

133 lines
4.1 KiB
Python
Raw Normal View History

# Author: Mr_Orange
#
# This file is part of SickGear.
#
# SickGear 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.
#
# SickGear 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 SickGear. If not, see <http://www.gnu.org/licenses/>.
import re
from . import generic
from sickbeard import logger, tvcache
from sickbeard.rssfeeds import RSSFeeds
from sickbeard.exceptions import ex
from lib.bencode import bdecode
class TorrentRssProvider(generic.TorrentProvider):
def __init__(self, name, url, cookies='', search_mode='eponly', search_fallback=False,
enable_recentsearch=False, enable_backlog=False):
generic.TorrentProvider.__init__(self, name)
self.url = url.rstrip('/')
self.cookies = cookies
self.enable_recentsearch = enable_recentsearch
self.enable_backlog = enable_backlog
self.search_mode = search_mode
self.search_fallback = search_fallback
self.feeder = RSSFeeds(self)
self.cache = TorrentRssCache(self)
def image_name(self):
return generic.GenericProvider.image_name(self, 'torrentrss')
def config_str(self):
return '%s|%s|%s|%d|%s|%d|%d|%d' % (self.name or '',
self.url or '',
self.cookies or '',
self.enabled,
self.search_mode or '',
self.search_fallback,
self.enable_recentsearch,
self.enable_backlog)
def _get_title_and_url(self, item):
title, url = None, None
if item.title:
title = re.sub(r'\s+', '.', u'' + item.title)
Fixed issues with editing/saving custom scene exceptions. Fixed charmap issues for anime show names. Fixed issues with display show page and epCat key errors. Fixed duplicate log messages for clearing provider caches. Fixed issues with email notifier ep names not properly being encoded to UTF-8. TVDB<->TVRAGE Indexer ID mapping is now performed on demand to be used when needed such as newznab providers can be searched with tvrage_id's and some will return tvrage_id's that later can be used to create show objects from for faster and more accurate name parsing, mapping is done via Trakt API calls. Added stop event signals to schedualed tasks, SR now waits indefinate till task has been fully stopped before completing a restart or shutdown event. NameParserCache is now persistent and stores 200 parsed results at any given time for quicker lookups and better performance, this helps maintain results between updates or shutdown/startup events. Black and White lists for anime now only get used for anime shows as intended, performance gain for non-anime shows that dont need to load these lists. Internal name cache now builds it self on demand when needed per show request plus checks if show is already in cache and if true exits routine to save time. Schedualer and QueueItems classes are now a sub-class of threading.Thread and a stop threading event signal has been added to each. If I forgot to list something it doesn't mean its not fixed so please test and report back if anything is wrong or has been corrected by this new release.
2014-07-15 02:00:53 +00:00
attempt_list = [lambda: item.torrent_magneturi,
lambda: item.enclosures[0].href,
lambda: item.link]
for cur_attempt in attempt_list:
try:
url = cur_attempt()
except:
continue
if title and url:
break
return title, url
def validate_feed(self):
success, err_msg = self._check_cookie()
if not success:
return success, err_msg
try:
items = self.get_cache_data()
for item in items:
title, url = self._get_title_and_url(item)
if not (title and url):
continue
if url.startswith('magnet:'):
if re.search('urn:btih:([0-9a-f]{32,40})', url):
break
else:
torrent_file = self.get_url(url)
try:
bdecode(torrent_file)
break
except Exception:
pass
else:
return False, '%s fetched RSS feed data: %s' % \
(('Fail to validate', 'No items found in the')[0 == len(items)], self.url)
return True, None
except Exception as e:
return False, 'Error when trying to load RSS: ' + ex(e)
def get_cache_data(self):
logger.log(u'TorrentRssCache cache update URL: ' + self.url, logger.DEBUG)
data = self.feeder.get_feed(self.url)
return [] if not (data and 'entries' in data) else data.entries
class TorrentRssCache(tvcache.TVCache):
def __init__(self, provider):
tvcache.TVCache.__init__(self, provider)
self.minTime = 15
def _getRSSData(self):
return self.provider.get_cache_data()