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-05-21 03:22:48 +00:00
|
|
|
def __init__(self, name, url, 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
|
|
|
|
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-03-10 05:18:05 +00:00
|
|
|
def configStr(self):
|
2014-05-21 03:22:48 +00:00
|
|
|
return self.name + '|' + self.url + '|' + 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-04-22 08:02:43 +00:00
|
|
|
if ek.ek(os.path.isfile, ek.ek(os.path.join, sickbeard.PROG_DIR, 'data', '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-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-03-10 05:18:05 +00:00
|
|
|
data = self.cache._getRSSData()
|
|
|
|
if not data:
|
|
|
|
return (False, 'No data returned from url: ' + self.url)
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-04-26 01:39:43 +00:00
|
|
|
items = data.entries
|
|
|
|
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:
|
|
|
|
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))
|
|
|
|
|
2014-04-30 13:49:50 +00:00
|
|
|
def getURL(self, url, post_data=None, headers=None, json=False):
|
2014-04-27 10:31:54 +00:00
|
|
|
|
|
|
|
if not self.session:
|
|
|
|
self.session = requests.Session()
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
try:
|
2014-04-25 11:45:34 +00:00
|
|
|
parsed = list(urlparse.urlparse(url))
|
|
|
|
parsed[2] = re.sub("/{2,}", "/", parsed[2]) # replace two or more / with one
|
2014-04-27 10:31:54 +00:00
|
|
|
r = self.session.get(url, verify=False)
|
2014-03-10 05:18:05 +00:00
|
|
|
except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError), e:
|
2014-03-25 05:57:24 +00:00
|
|
|
logger.log(u"Error loading " + self.name + " URL: " + ex(e), logger.ERROR)
|
2014-03-10 05:18:05 +00:00
|
|
|
return None
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-04-27 10:31:54 +00:00
|
|
|
if r.status_code != 200:
|
2014-03-25 05:57:24 +00:00
|
|
|
logger.log(self.name + u" page requested with url " + url + " returned status code is " + str(
|
2014-04-27 10:31:54 +00:00
|
|
|
r.status_code) + ': ' + clients.http_error_code[r.status_code], logger.WARNING)
|
2014-03-10 05:18:05 +00:00
|
|
|
return None
|
|
|
|
|
2014-04-27 10:31:54 +00:00
|
|
|
return r.content
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
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-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
|
|
|
|
|
|
|
|
def _getRSSData(self):
|
2014-04-25 23:41:40 +00:00
|
|
|
url = self.provider.url
|
2014-03-10 05:18:05 +00:00
|
|
|
logger.log(u"TorrentRssCache cache update URL: " + self.provider.url, logger.DEBUG)
|
2014-05-04 12:05:27 +00:00
|
|
|
return self.getRSSFeed(url)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
def _parseItem(self, item):
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
(title, url) = self.provider._get_title_and_url(item)
|
|
|
|
if not title or not url:
|
|
|
|
logger.log(u"The XML returned from the RSS feed is incomplete, this result is unusable", logger.ERROR)
|
|
|
|
return None
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-05-21 03:22:48 +00:00
|
|
|
logger.log(u"Attempting to add item to cache: " + title, logger.DEBUG)
|
2014-04-25 23:41:40 +00:00
|
|
|
return self._addCacheEntry(title, url)
|