2014-03-10 05:18:05 +00:00
|
|
|
# Author: Mr_Orange
|
|
|
|
#
|
2014-05-23 12:37:22 +00:00
|
|
|
# This file is part of SickRage.
|
2014-03-10 05:18:05 +00:00
|
|
|
#
|
2014-05-23 12:37:22 +00:00
|
|
|
# SickRage 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-05-23 12:37:22 +00:00
|
|
|
# SickRage 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-05-23 12:37:22 +00:00
|
|
|
# along with SickRage. If not, see <http://www.gnu.org/licenses/>.
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
2014-04-25 11:45:34 +00:00
|
|
|
import urlparse
|
2014-03-10 05:18:05 +00: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 05:44:47 +00:00
|
|
|
from lib.requests import exceptions
|
2014-03-20 18:03:22 +00:00
|
|
|
from lib.bencode import bdecode
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-03-25 05:57:24 +00:00
|
|
|
class TorrentRssProvider(generic.TorrentProvider):
|
2014-07-27 10:59:21 +00:00
|
|
|
def __init__(self, name, url, cookies='', search_mode='eponly', search_fallback=False, backlog_only=False):
|
2014-03-10 05:18:05 +00:00
|
|
|
generic.TorrentProvider.__init__(self, name)
|
|
|
|
self.cache = TorrentRssCache(self)
|
2014-05-23 05:36:48 +00:00
|
|
|
self.url = re.sub('\/$', '', url)
|
2014-05-21 03:22:48 +00:00
|
|
|
self.url = url
|
2014-03-10 05:18:05 +00:00
|
|
|
self.enabled = True
|
2014-07-01 05:48:49 +00:00
|
|
|
self.ratio = None
|
2014-03-10 05:18:05 +00:00
|
|
|
self.supportsBacklog = False
|
|
|
|
|
2014-05-21 03:22:48 +00:00
|
|
|
self.search_mode = search_mode
|
|
|
|
self.search_fallback = search_fallback
|
|
|
|
self.backlog_only = backlog_only
|
2014-07-27 10:59:21 +00:00
|
|
|
self.cookies = cookies
|
2014-05-25 07:28:13 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
def configStr(self):
|
2014-05-25 07:28:13 +00:00
|
|
|
return self.name + '|' + self.url + '|' + self.cookies + '|' + str(int(self.enabled)) + '|' + self.search_mode + '|' + str(int(self.search_fallback)) + '|' + str(int(self.backlog_only))
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
def imageName(self):
|
2014-08-07 23:17:31 +00: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 05:57:24 +00:00
|
|
|
return self.getID() + '.png'
|
2014-03-10 05:18:05 +00:00
|
|
|
return 'torrentrss.png'
|
|
|
|
|
|
|
|
def isEnabled(self):
|
|
|
|
return self.enabled
|
|
|
|
|
|
|
|
def _get_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
|
|
|
|
|
2014-04-25 23:41:40 +00:00
|
|
|
title = item.title
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-07-15 02:00:53 +00:00
|
|
|
if title:
|
|
|
|
title = u'' + title
|
|
|
|
title = title.replace(' ', '.')
|
|
|
|
|
2014-04-25 23:41:40 +00:00
|
|
|
attempt_list = [lambda: item.torrent_magneturi,
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-04-25 23:41:40 +00:00
|
|
|
lambda: item.enclosures[0].href,
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-04-25 23:41:40 +00:00
|
|
|
lambda: item.link]
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
for cur_attempt in attempt_list:
|
|
|
|
try:
|
|
|
|
url = cur_attempt()
|
|
|
|
except:
|
|
|
|
continue
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
if title and url:
|
|
|
|
return (title, url)
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
return (title, url)
|
|
|
|
|
|
|
|
def validateRSS(self):
|
|
|
|
|
2014-03-25 05:57:24 +00:00
|
|
|
try:
|
2014-05-25 07:28:13 +00:00
|
|
|
if self.cookies:
|
|
|
|
cookie_validator=re.compile("^(\w+=\w+)(;\w+=\w+)*$")
|
|
|
|
if not cookie_validator.match(self.cookies):
|
|
|
|
return (False, 'Cookie is not correctly formatted: ' + self.cookies)
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-08-20 08:34:42 +00:00
|
|
|
items = self.cache._getDailyData()
|
|
|
|
|
2014-04-26 01:39:43 +00:00
|
|
|
if not len(items) > 0:
|
2014-03-10 05:18:05 +00:00
|
|
|
return (False, 'No items found in the RSS feed ' + self.url)
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-04-26 01:39:43 +00:00
|
|
|
(title, url) = self._get_title_and_url(items[0])
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
if not title:
|
|
|
|
return (False, 'Unable to get title from first item')
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
if not url:
|
|
|
|
return (False, 'Unable to get torrent url from first item')
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
if url.startswith('magnet:') and re.search('urn:btih:([\w]{32,40})', url):
|
|
|
|
return (True, 'RSS feed Parsed correctly')
|
|
|
|
else:
|
2014-07-27 10:59:21 +00: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 05:18:05 +00:00
|
|
|
torrent_file = self.getURL(url)
|
2014-03-25 05:57:24 +00:00
|
|
|
try:
|
2014-03-20 18:03:22 +00:00
|
|
|
bdecode(torrent_file)
|
2014-03-10 05:18:05 +00:00
|
|
|
except Exception, e:
|
|
|
|
self.dumpHTML(torrent_file)
|
|
|
|
return (False, 'Torrent link is not a valid torrent file: ' + ex(e))
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00: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 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
dumpName = ek.ek(os.path.join, sickbeard.CACHE_DIR, 'custom_torrent.html')
|
|
|
|
|
2014-03-25 05:57:24 +00:00
|
|
|
try:
|
2014-03-10 05:18:05 +00: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 05:57:24 +00:00
|
|
|
return True
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-07-01 05:48:49 +00:00
|
|
|
def seedRatio(self):
|
|
|
|
return self.ratio
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-03-25 05:57:24 +00:00
|
|
|
class TorrentRssCache(tvcache.TVCache):
|
2014-03-10 05:18:05 +00:00
|
|
|
def __init__(self, provider):
|
|
|
|
tvcache.TVCache.__init__(self, provider)
|
|
|
|
self.minTime = 15
|
|
|
|
|
2014-08-05 16:19:54 +00:00
|
|
|
def _getDailyData(self):
|
2014-03-10 05:18:05 +00:00
|
|
|
logger.log(u"TorrentRssCache cache update URL: " + self.provider.url, logger.DEBUG)
|
2014-07-27 10:59:21 +00:00
|
|
|
|
|
|
|
request_headers = None
|
2014-05-25 07:28:13 +00:00
|
|
|
if self.provider.cookies:
|
2014-08-12 10:09:11 +00:00
|
|
|
request_headers = { 'Cookie': self.provider.cookies }
|
2014-07-27 10:59:21 +00:00
|
|
|
|
2014-08-12 10:09:11 +00:00
|
|
|
data = self.getRSSFeed(self.provider.url, request_headers=request_headers)
|
|
|
|
|
|
|
|
if data and 'entries' in data:
|
|
|
|
return data.entries
|
|
|
|
else:
|
|
|
|
return []
|