2014-03-10 01:18:05 -04:00
|
|
|
# Author: Mr_Orange
|
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# This file is part of SickRage.
|
2014-03-10 01:18:05 -04:00
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# SickRage is free software: you can redistribute it and/or modify
|
2014-03-10 01:18:05 -04: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-05-23 08:37:22 -04:00
|
|
|
# SickRage is distributed in the hope that it will be useful,
|
2014-03-10 01:18:05 -04: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-05-23 08:37:22 -04:00
|
|
|
# along with SickRage. If not, see <http://www.gnu.org/licenses/>.
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
|
|
|
import sickbeard
|
|
|
|
import generic
|
|
|
|
|
|
|
|
from sickbeard import helpers
|
|
|
|
from sickbeard import encodingKludge as ek
|
|
|
|
from sickbeard import logger
|
|
|
|
from sickbeard import tvcache
|
|
|
|
from sickbeard.exceptions import ex
|
|
|
|
|
|
|
|
from lib import requests
|
2014-03-20 14:03:22 -04:00
|
|
|
from lib.bencode import bdecode
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-08-29 01:16:25 -04:00
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
class TorrentRssProvider(generic.TorrentProvider):
|
2014-08-29 01:16:25 -04:00
|
|
|
def __init__(self, name, url, cookies='', search_mode='eponly', search_fallback=False, enable_daily=False,
|
|
|
|
enable_backlog=False):
|
2014-03-10 01:18:05 -04:00
|
|
|
generic.TorrentProvider.__init__(self, name)
|
|
|
|
self.cache = TorrentRssCache(self)
|
2014-12-20 00:57:11 -05:00
|
|
|
|
|
|
|
self.urls = {'base_url': re.sub('\/$', '', url)}
|
|
|
|
|
|
|
|
self.url = self.urls['base_url']
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
self.enabled = True
|
2014-07-01 01:48:49 -04:00
|
|
|
self.ratio = None
|
2014-03-10 01:18:05 -04:00
|
|
|
self.supportsBacklog = False
|
|
|
|
|
2014-05-20 23:22:48 -04:00
|
|
|
self.search_mode = search_mode
|
|
|
|
self.search_fallback = search_fallback
|
2014-08-29 01:16:25 -04:00
|
|
|
self.enable_daily = enable_daily
|
|
|
|
self.enable_backlog = enable_backlog
|
2014-07-27 06:59:21 -04:00
|
|
|
self.cookies = cookies
|
2014-05-25 03:28:13 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def configStr(self):
|
2014-09-14 15:11:57 -04: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_daily,
|
|
|
|
self.enable_backlog)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
def imageName(self):
|
2014-08-29 01:16:25 -04:00
|
|
|
if ek.ek(os.path.isfile,
|
|
|
|
ek.ek(os.path.join, sickbeard.PROG_DIR, 'gui', sickbeard.GUI_NAME, 'images', 'providers',
|
|
|
|
self.getID() + '.png')):
|
2014-03-25 01:57:24 -04:00
|
|
|
return self.getID() + '.png'
|
2014-03-10 01:18:05 -04:00
|
|
|
return 'torrentrss.png'
|
|
|
|
|
|
|
|
def isEnabled(self):
|
|
|
|
return self.enabled
|
|
|
|
|
|
|
|
def _get_title_and_url(self, item):
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-12-11 13:06:50 -05:00
|
|
|
title = item.get('title')
|
2014-07-14 22:00:53 -04:00
|
|
|
if title:
|
|
|
|
title = u'' + title
|
|
|
|
title = title.replace(' ', '.')
|
|
|
|
|
2014-12-11 13:06:50 -05:00
|
|
|
attempt_list = [lambda: item.get('torrent_magneturi'),
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-04-25 19:41:40 -04:00
|
|
|
lambda: item.enclosures[0].href,
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-12-11 13:06:50 -05:00
|
|
|
lambda: item.get('link')]
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-12-11 13:06:50 -05:00
|
|
|
url = None
|
2014-03-10 01:18:05 -04:00
|
|
|
for cur_attempt in attempt_list:
|
|
|
|
try:
|
|
|
|
url = cur_attempt()
|
|
|
|
except:
|
|
|
|
continue
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
if title and url:
|
2014-12-11 13:06:50 -05:00
|
|
|
break
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-12-11 13:06:50 -05:00
|
|
|
return title, url
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
def validateRSS(self):
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
try:
|
2014-05-25 03:28:13 -04:00
|
|
|
if self.cookies:
|
2014-08-29 01:16:25 -04:00
|
|
|
cookie_validator = re.compile("^(\w+=\w+)(;\w+=\w+)*$")
|
2014-05-25 03:28:13 -04:00
|
|
|
if not cookie_validator.match(self.cookies):
|
|
|
|
return (False, 'Cookie is not correctly formatted: ' + self.cookies)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-12-07 12:16:41 -05:00
|
|
|
data = self.cache._getRSSData()['entries']
|
|
|
|
if not data:
|
2014-03-10 01:18:05 -04:00
|
|
|
return (False, 'No items found in the RSS feed ' + self.url)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-12-07 12:16:41 -05:00
|
|
|
(title, url) = self._get_title_and_url(data[0])
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
if not title:
|
|
|
|
return (False, 'Unable to get title from first item')
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
if not url:
|
|
|
|
return (False, 'Unable to get torrent url from first item')
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
if url.startswith('magnet:') and re.search('urn:btih:([\w]{32,40})', url):
|
|
|
|
return (True, 'RSS feed Parsed correctly')
|
|
|
|
else:
|
2014-07-27 06:59:21 -04:00
|
|
|
if self.cookies:
|
|
|
|
requests.utils.add_dict_to_cookiejar(self.session.cookies,
|
|
|
|
dict(x.rsplit('=', 1) for x in (self.cookies.split(';'))))
|
2014-03-10 01:18:05 -04:00
|
|
|
torrent_file = self.getURL(url)
|
2014-03-25 01:57:24 -04:00
|
|
|
try:
|
2014-03-20 14:03:22 -04:00
|
|
|
bdecode(torrent_file)
|
2014-03-10 01:18:05 -04:00
|
|
|
except Exception, e:
|
|
|
|
self.dumpHTML(torrent_file)
|
|
|
|
return (False, 'Torrent link is not a valid torrent file: ' + ex(e))
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
return (True, 'RSS feed Parsed correctly')
|
|
|
|
|
|
|
|
except Exception, e:
|
|
|
|
return (False, 'Error when trying to load RSS: ' + ex(e))
|
|
|
|
|
|
|
|
def dumpHTML(self, data):
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
dumpName = ek.ek(os.path.join, sickbeard.CACHE_DIR, 'custom_torrent.html')
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
try:
|
2014-03-10 01:18:05 -04:00
|
|
|
fileOut = open(dumpName, 'wb')
|
|
|
|
fileOut.write(data)
|
|
|
|
fileOut.close()
|
|
|
|
helpers.chmodAsParent(dumpName)
|
|
|
|
except IOError, e:
|
|
|
|
logger.log("Unable to save the file: " + ex(e), logger.ERROR)
|
|
|
|
return False
|
2014-12-16 05:24:06 -05:00
|
|
|
logger.log(u"Saved custom_torrent html dump " + dumpName + " ", logger.INFO)
|
2014-03-25 01:57:24 -04:00
|
|
|
return True
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-07-01 01:48:49 -04:00
|
|
|
def seedRatio(self):
|
|
|
|
return self.ratio
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-08-29 01:16:25 -04:00
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
class TorrentRssCache(tvcache.TVCache):
|
2014-03-10 01:18:05 -04:00
|
|
|
def __init__(self, provider):
|
|
|
|
tvcache.TVCache.__init__(self, provider)
|
|
|
|
self.minTime = 15
|
|
|
|
|
2014-08-30 04:47:00 -04:00
|
|
|
def _getRSSData(self):
|
2014-03-10 01:18:05 -04:00
|
|
|
logger.log(u"TorrentRssCache cache update URL: " + self.provider.url, logger.DEBUG)
|
2014-07-27 06:59:21 -04:00
|
|
|
|
2014-05-25 03:28:13 -04:00
|
|
|
if self.provider.cookies:
|
2014-12-20 00:57:11 -05:00
|
|
|
self.provider.headers.update({'Cookie': self.provider.cookies})
|
2014-07-27 06:59:21 -04:00
|
|
|
|
2014-12-20 00:57:11 -05:00
|
|
|
return self.getRSSFeed(self.provider.url, items=['entries', 'feed'])
|