SickGear/sickbeard/providers/rsstorrent.py

130 lines
4.4 KiB
Python
Raw Normal View History

# coding=utf-8
#
# 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.helpers import tryInt
from sickbeard.exceptions import ex
from sickbeard.rssfeeds import RSSFeeds
from lib.bencode import bdecode
class TorrentRssProvider(generic.TorrentProvider):
def __init__(self, name, url, cookies='', search_mode='eponly', search_fallback=False,
2017-01-01 20:24:41 +00:00
enable_recentsearch=False, enable_backlog=False, enable_scheduled_backlog=False):
self.enable_backlog = bool(tryInt(enable_backlog))
2017-01-01 20:24:41 +00:00
self.enable_scheduled_backlog = bool(tryInt(enable_scheduled_backlog))
generic.TorrentProvider.__init__(self, name, supports_backlog=self.enable_backlog, cache_update_freq=15)
self.url = url.rstrip('/')
self.url_base = self.url
self.cookies = cookies
self.enable_recentsearch = bool(tryInt(enable_recentsearch)) or not self.enable_backlog
self.search_mode = search_mode
self.search_fallback = bool(tryInt(search_fallback))
self.feeder = RSSFeeds(self)
def image_name(self):
return generic.GenericProvider.image_name(self, 'torrentrss')
def config_str(self):
2017-01-01 20:24:41 +00:00
return '%s|%s|%s|%d|%s|%d|%d|%d|%d' % (
self.name or '', self.url or '', self.cookies or '', self.enabled,
2017-01-01 20:24:41 +00:00
self.search_mode or '', self.search_fallback, self.enable_recentsearch, self.enable_backlog,
self.enable_scheduled_backlog)
def _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 (StandardError, Exception):
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._search_provider({'Validate': ['']})
for item in items:
title, url = self._title_and_url(item)
if not (title and url):
continue
if url.startswith('magnet:'):
btih = None
try:
btih = re.findall('urn:btih:([\w]{32,40})', url)[0]
if 32 == len(btih):
from base64 import b16encode, b32decode
btih = b16encode(b32decode(btih))
except (StandardError, Exception):
pass
if re.search('(?i)[0-9a-f]{32,40}', btih):
break
else:
torrent_file = self.get_url(url)
try:
bdecode(torrent_file)
break
except (StandardError, 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 _search_provider(self, search_params, **kwargs):
result = []
for mode in search_params.keys():
data = self.feeder.get_feed(self.url)
result += (data and 'entries' in data) and data.entries or []
self.log_result(mode, count=len(result), url=self.url)
return result