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
|
2014-04-25 07:45:34 -04:00
|
|
|
import urlparse
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
import sickbeard
|
|
|
|
import generic
|
|
|
|
|
|
|
|
from sickbeard import helpers
|
|
|
|
from sickbeard import encodingKludge as ek
|
|
|
|
from sickbeard import logger
|
|
|
|
from sickbeard import tvcache
|
|
|
|
from sickbeard import clients
|
|
|
|
from sickbeard.exceptions import ex
|
|
|
|
|
|
|
|
from lib import requests
|
2014-04-02 01:44:47 -04:00
|
|
|
from lib.requests import exceptions
|
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-05-23 01:36:48 -04:00
|
|
|
self.url = re.sub('\/$', '', url)
|
2014-05-20 23:22:48 -04:00
|
|
|
self.url = 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-08-29 01:16:25 -04:00
|
|
|
return self.name + '|' + self.url + '|' + self.cookies + '|' + str(
|
|
|
|
int(self.enabled)) + '|' + self.search_mode + '|' + str(int(self.search_fallback)) + '|' + str(
|
|
|
|
int(self.enable_daily)) + '|' + str(int(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-03-10 01:18:05 -04:00
|
|
|
title, url = None, None
|
|
|
|
|
2014-04-25 19:41:40 -04:00
|
|
|
title = item.title
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-07-14 22:00:53 -04:00
|
|
|
if title:
|
|
|
|
title = u'' + title
|
|
|
|
title = title.replace(' ', '.')
|
|
|
|
|
2014-04-25 19:41:40 -04:00
|
|
|
attempt_list = [lambda: item.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-04-25 19:41:40 -04:00
|
|
|
lambda: item.link]
|
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:
|
|
|
|
return (title, url)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
return (title, url)
|
|
|
|
|
|
|
|
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-08-30 04:47:00 -04:00
|
|
|
items = self.cache._getRSSData()
|
2014-08-29 01:16:25 -04:00
|
|
|
|
2014-04-25 21:39:43 -04:00
|
|
|
if not len(items) > 0:
|
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-04-25 21:39:43 -04:00
|
|
|
(title, url) = self._get_title_and_url(items[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
|
|
|
|
logger.log(u"Saved custom_torrent html dump " + dumpName + " ", logger.MESSAGE)
|
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
|
|
|
|
|
|
|
request_headers = None
|
2014-05-25 03:28:13 -04:00
|
|
|
if self.provider.cookies:
|
2014-08-29 01:16:25 -04:00
|
|
|
request_headers = {'Cookie': self.provider.cookies}
|
2014-07-27 06:59:21 -04:00
|
|
|
|
2014-08-12 06:09:11 -04:00
|
|
|
data = self.getRSSFeed(self.provider.url, request_headers=request_headers)
|
|
|
|
|
|
|
|
if data and 'entries' in data:
|
|
|
|
return data.entries
|
|
|
|
else:
|
|
|
|
return []
|