2015-09-18 00:06:34 +00:00
|
|
|
# coding=utf-8
|
2014-03-10 05:18:05 +00:00
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# This file is part of SickGear.
|
2014-03-10 05:18:05 +00:00
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# SickGear is free software: you can redistribute it and/or modify
|
2014-03-10 05:18:05 +00:00
|
|
|
# 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.
|
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# SickGear is distributed in the hope that it will be useful,
|
2014-03-10 05:18:05 +00:00
|
|
|
# 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
|
2014-11-12 16:43:14 +00:00
|
|
|
# along with SickGear. If not, see <http://www.gnu.org/licenses/>.
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
import re
|
|
|
|
|
2015-06-15 19:09:01 +00:00
|
|
|
from . import generic
|
|
|
|
from sickbeard import logger, tvcache
|
2016-09-25 23:46:36 +00:00
|
|
|
from sickbeard.helpers import tryInt
|
2014-03-10 05:18:05 +00:00
|
|
|
from sickbeard.exceptions import ex
|
2015-09-18 00:06:34 +00:00
|
|
|
from sickbeard.rssfeeds import RSSFeeds
|
2014-03-20 18:03:22 +00:00
|
|
|
from lib.bencode import bdecode
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-08-29 05:16:25 +00:00
|
|
|
|
2014-03-25 05:57:24 +00:00
|
|
|
class TorrentRssProvider(generic.TorrentProvider):
|
2015-06-15 19:09:01 +00:00
|
|
|
|
|
|
|
def __init__(self, name, url, cookies='', search_mode='eponly', search_fallback=False,
|
|
|
|
enable_recentsearch=False, enable_backlog=False):
|
2016-09-25 23:46:36 +00:00
|
|
|
self.enable_backlog = bool(tryInt(enable_backlog))
|
|
|
|
generic.TorrentProvider.__init__(self, name, supports_backlog=self.enable_backlog, cache_update_freq=15)
|
2015-06-15 19:09:01 +00:00
|
|
|
|
|
|
|
self.url = url.rstrip('/')
|
2016-09-25 23:46:36 +00:00
|
|
|
self.url_base = self.url
|
2015-06-15 19:09:01 +00:00
|
|
|
self.cookies = cookies
|
|
|
|
|
2016-09-25 23:46:36 +00:00
|
|
|
self.enable_recentsearch = bool(tryInt(enable_recentsearch)) or not self.enable_backlog
|
2015-06-15 19:09:01 +00:00
|
|
|
self.search_mode = search_mode
|
2016-09-25 23:46:36 +00:00
|
|
|
self.search_fallback = bool(tryInt(search_fallback))
|
2015-06-15 19:09:01 +00:00
|
|
|
|
|
|
|
self.feeder = RSSFeeds(self)
|
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
def image_name(self):
|
2015-06-15 19:09:01 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
return generic.GenericProvider.image_name(self, 'torrentrss')
|
2014-05-25 07:28:13 +00:00
|
|
|
|
2015-06-15 19:09:01 +00:00
|
|
|
def config_str(self):
|
2016-09-25 23:46:36 +00:00
|
|
|
|
|
|
|
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)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
def _title_and_url(self, item):
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
title, url = None, None
|
|
|
|
|
2015-06-15 19:09:01 +00:00
|
|
|
if item.title:
|
|
|
|
title = re.sub(r'\s+', '.', u'' + item.title)
|
2014-07-15 02:00:53 +00:00
|
|
|
|
2014-04-25 23:41:40 +00:00
|
|
|
attempt_list = [lambda: item.torrent_magneturi,
|
|
|
|
lambda: item.enclosures[0].href,
|
|
|
|
lambda: item.link]
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
for cur_attempt in attempt_list:
|
|
|
|
try:
|
|
|
|
url = cur_attempt()
|
2016-09-25 23:46:36 +00:00
|
|
|
except (StandardError, Exception):
|
2014-03-10 05:18:05 +00:00
|
|
|
continue
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
if title and url:
|
2015-06-15 19:09:01 +00:00
|
|
|
break
|
2014-08-29 05:16:25 +00:00
|
|
|
|
2015-06-15 19:09:01 +00:00
|
|
|
return title, url
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2015-06-15 19:09:01 +00:00
|
|
|
def validate_feed(self):
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
success, err_msg = self._check_cookie()
|
|
|
|
if not success:
|
|
|
|
return success, err_msg
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2015-06-15 19:09:01 +00:00
|
|
|
try:
|
2016-09-25 23:46:36 +00:00
|
|
|
items = self._search_provider({'Validate': ['']})
|
2015-06-15 19:09:01 +00:00
|
|
|
|
|
|
|
for item in items:
|
2015-09-18 00:06:34 +00:00
|
|
|
title, url = self._title_and_url(item)
|
2015-06-15 19:09:01 +00:00
|
|
|
if not (title and url):
|
|
|
|
continue
|
|
|
|
if url.startswith('magnet:'):
|
|
|
|
if re.search('urn:btih:([0-9a-f]{32,40})', url):
|
|
|
|
break
|
|
|
|
else:
|
2015-07-13 09:39:20 +00:00
|
|
|
torrent_file = self.get_url(url)
|
2015-06-15 19:09:01 +00:00
|
|
|
try:
|
|
|
|
bdecode(torrent_file)
|
|
|
|
break
|
2016-08-26 23:36:01 +00:00
|
|
|
except (StandardError, Exception):
|
2015-06-15 19:09:01 +00:00
|
|
|
pass
|
2014-03-10 05:18:05 +00:00
|
|
|
else:
|
2015-06-15 19:09:01 +00:00
|
|
|
return False, '%s fetched RSS feed data: %s' % \
|
|
|
|
(('Fail to validate', 'No items found in the')[0 == len(items)], self.url)
|
|
|
|
|
|
|
|
return True, None
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-06-08 12:47:01 +00:00
|
|
|
except Exception as e:
|
2015-06-15 19:09:01 +00:00
|
|
|
return False, 'Error when trying to load RSS: ' + ex(e)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2016-09-25 23:46:36 +00:00
|
|
|
def _search_provider(self, search_params, **kwargs):
|
2015-06-15 19:09:01 +00:00
|
|
|
|
2016-09-25 23:46:36 +00:00
|
|
|
result = []
|
|
|
|
for mode in search_params.keys():
|
|
|
|
data = self.feeder.get_feed(self.url)
|
2015-06-15 19:09:01 +00:00
|
|
|
|
2016-09-25 23:46:36 +00:00
|
|
|
result += (data and 'entries' in data) and data.entries or []
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2016-09-25 23:46:36 +00:00
|
|
|
self.log_result(mode, count=len(result), url=self.url)
|
2014-08-12 10:09:11 +00:00
|
|
|
|
2016-09-25 23:46:36 +00:00
|
|
|
return result
|