2014-04-22 04:02:43 -04:00
|
|
|
# coding=utf-8
|
2014-03-10 01:18:05 -04:00
|
|
|
# Author: Nic Wolfe <nic@wolfeden.ca>
|
|
|
|
# URL: http://code.google.com/p/sickbeard/
|
|
|
|
#
|
|
|
|
# This file is part of Sick Beard.
|
|
|
|
#
|
|
|
|
# Sick Beard is free software: you can redistribute it and/or modify
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# Sick Beard is distributed in the hope that it will be useful,
|
|
|
|
# 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
|
|
|
|
# along with Sick Beard. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
from __future__ import with_statement
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import re
|
2014-04-25 21:39:43 -04:00
|
|
|
import urllib
|
2014-03-10 01:18:05 -04:00
|
|
|
import urllib2
|
2014-04-26 06:37:40 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
import itertools
|
|
|
|
import operator
|
|
|
|
import collections
|
2014-04-25 20:03:24 -04:00
|
|
|
import urlparse
|
|
|
|
from lib.feedparser import feedparser
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
import sickbeard
|
|
|
|
|
|
|
|
from sickbeard import helpers, classes, logger, db
|
2014-03-25 01:57:24 -04:00
|
|
|
from sickbeard.common import Quality, MULTI_EP_RESULT, SEASON_RESULT #, SEED_POLICY_TIME, SEED_POLICY_RATIO
|
2014-03-10 01:18:05 -04:00
|
|
|
from sickbeard import tvcache
|
|
|
|
from sickbeard import encodingKludge as ek
|
|
|
|
from sickbeard.exceptions import ex
|
|
|
|
from lib.hachoir_parser import createParser
|
|
|
|
from sickbeard.name_parser.parser import NameParser, InvalidNameException
|
|
|
|
from sickbeard.common import Quality, Overview
|
|
|
|
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
class GenericProvider:
|
2014-03-10 01:18:05 -04:00
|
|
|
NZB = "nzb"
|
|
|
|
TORRENT = "torrent"
|
|
|
|
|
|
|
|
def __init__(self, name):
|
|
|
|
|
|
|
|
# these need to be set in the subclass
|
|
|
|
self.providerType = None
|
|
|
|
self.name = name
|
|
|
|
self.url = ''
|
|
|
|
|
|
|
|
self.supportsBacklog = False
|
|
|
|
|
|
|
|
self.cache = tvcache.TVCache(self)
|
|
|
|
|
|
|
|
def getID(self):
|
|
|
|
return GenericProvider.makeID(self.name)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def makeID(name):
|
2014-03-25 01:57:24 -04:00
|
|
|
return re.sub("[^\w\d_]", "_", name.strip().lower())
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
def imageName(self):
|
|
|
|
return self.getID() + '.png'
|
|
|
|
|
|
|
|
def _checkAuth(self):
|
|
|
|
return
|
|
|
|
|
|
|
|
def isActive(self):
|
|
|
|
if self.providerType == GenericProvider.NZB and sickbeard.USE_NZBS:
|
|
|
|
return self.isEnabled()
|
|
|
|
elif self.providerType == GenericProvider.TORRENT and sickbeard.USE_TORRENTS:
|
|
|
|
return self.isEnabled()
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def isEnabled(self):
|
|
|
|
"""
|
|
|
|
This should be overridden and should return the config setting eg. sickbeard.MYPROVIDER
|
|
|
|
"""
|
|
|
|
return False
|
|
|
|
|
|
|
|
def getResult(self, episodes):
|
|
|
|
"""
|
|
|
|
Returns a result of the correct type for this provider
|
|
|
|
"""
|
|
|
|
|
|
|
|
if self.providerType == GenericProvider.NZB:
|
|
|
|
result = classes.NZBSearchResult(episodes)
|
|
|
|
elif self.providerType == GenericProvider.TORRENT:
|
|
|
|
result = classes.TorrentSearchResult(episodes)
|
|
|
|
else:
|
|
|
|
result = classes.SearchResult(episodes)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
|
|
|
result.provider = self
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
return result
|
|
|
|
|
2014-04-25 23:42:35 -04:00
|
|
|
def getURL(self, url, post_data=None, headers=None, json=False):
|
2014-03-10 01:18:05 -04:00
|
|
|
"""
|
|
|
|
By default this is just a simple urlopen call but this method should be overridden
|
|
|
|
for providers with special URL requirements (like cookies)
|
|
|
|
"""
|
|
|
|
|
|
|
|
if not headers:
|
|
|
|
headers = []
|
|
|
|
|
2014-04-25 23:42:35 -04:00
|
|
|
data = helpers.getURL(url, post_data, headers, json=json)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
if not data:
|
|
|
|
logger.log(u"Error loading " + self.name + " URL: " + url, logger.ERROR)
|
|
|
|
return None
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
2014-04-25 21:39:43 -04:00
|
|
|
def getRSSFeed(self, url, post_data=None):
|
2014-04-25 20:03:24 -04:00
|
|
|
parsed = list(urlparse.urlparse(url))
|
|
|
|
parsed[2] = re.sub("/{2,}", "/", parsed[2]) # replace two or more / with one
|
|
|
|
|
2014-04-25 21:39:43 -04:00
|
|
|
if post_data:
|
|
|
|
url = url + 'api?' + urllib.urlencode(post_data)
|
|
|
|
|
2014-04-25 20:03:24 -04:00
|
|
|
f = feedparser.parse(url)
|
|
|
|
|
2014-04-25 21:39:43 -04:00
|
|
|
if not f:
|
|
|
|
logger.log(u"Error loading " + self.name + " URL: " + url, logger.ERROR)
|
2014-04-25 20:03:24 -04:00
|
|
|
return None
|
|
|
|
|
2014-04-25 21:39:43 -04:00
|
|
|
return f
|
2014-04-25 20:03:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def downloadResult(self, result):
|
|
|
|
"""
|
|
|
|
Save the result to disk.
|
|
|
|
"""
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
logger.log(u"Downloading a result from " + self.name + " at " + result.url)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
data = self.getURL(result.url)
|
|
|
|
|
2014-03-20 14:03:22 -04:00
|
|
|
if data == None:
|
2014-03-10 01:18:05 -04:00
|
|
|
return False
|
|
|
|
|
|
|
|
# use the appropriate watch folder
|
|
|
|
if self.providerType == GenericProvider.NZB:
|
|
|
|
saveDir = sickbeard.NZB_DIR
|
|
|
|
writeMode = 'w'
|
|
|
|
elif self.providerType == GenericProvider.TORRENT:
|
|
|
|
saveDir = sickbeard.TORRENT_DIR
|
|
|
|
writeMode = 'wb'
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# use the result name as the filename
|
|
|
|
file_name = ek.ek(os.path.join, saveDir, helpers.sanitizeFileName(result.name) + '.' + self.providerType)
|
|
|
|
|
|
|
|
logger.log(u"Saving to " + file_name, logger.DEBUG)
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(file_name, writeMode) as fileOut:
|
|
|
|
fileOut.write(data)
|
|
|
|
helpers.chmodAsParent(file_name)
|
|
|
|
except EnvironmentError, e:
|
|
|
|
logger.log("Unable to save the file: " + ex(e), logger.ERROR)
|
|
|
|
return False
|
|
|
|
|
|
|
|
# as long as it's a valid download then consider it a successful snatch
|
|
|
|
return self._verify_download(file_name)
|
|
|
|
|
|
|
|
def _verify_download(self, file_name=None):
|
|
|
|
"""
|
|
|
|
Checks the saved file to see if it was actually valid, if not then consider the download a failure.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# primitive verification of torrents, just make sure we didn't get a text file or something
|
|
|
|
if self.providerType == GenericProvider.TORRENT:
|
|
|
|
parser = createParser(file_name)
|
|
|
|
if parser:
|
|
|
|
mime_type = parser._getMimeType()
|
|
|
|
try:
|
|
|
|
parser.stream._input.close()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
if mime_type != 'application/x-bittorrent':
|
|
|
|
logger.log(u"Result is not a valid torrent file", logger.WARNING)
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def searchRSS(self):
|
|
|
|
|
|
|
|
self._checkAuth()
|
|
|
|
self.cache.updateCache()
|
|
|
|
|
|
|
|
return self.cache.findNeededEpisodes()
|
|
|
|
|
|
|
|
def getQuality(self, item):
|
|
|
|
"""
|
|
|
|
Figures out the quality of the given RSS item node
|
|
|
|
|
|
|
|
item: An elementtree.ElementTree element representing the <item> tag of the RSS feed
|
|
|
|
|
|
|
|
Returns a Quality value obtained from the node's data
|
|
|
|
"""
|
2014-03-25 01:57:24 -04:00
|
|
|
(title, url) = self._get_title_and_url(item) # @UnusedVariable
|
2014-03-10 01:18:05 -04:00
|
|
|
quality = Quality.sceneQuality(title)
|
|
|
|
return quality
|
|
|
|
|
2014-03-15 21:01:12 -04:00
|
|
|
def _doSearch(self, search_params, show=None, age=None):
|
2014-03-10 01:18:05 -04:00
|
|
|
return []
|
|
|
|
|
2014-04-27 06:31:54 -04:00
|
|
|
def _get_season_search_strings(self, show, season, episode, abd=False):
|
2014-03-10 01:18:05 -04:00
|
|
|
return []
|
|
|
|
|
2014-04-27 06:31:54 -04:00
|
|
|
def _get_episode_search_strings(self, show, season, episode, abd=False):
|
2014-03-10 01:18:05 -04:00
|
|
|
return []
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _get_title_and_url(self, item):
|
|
|
|
"""
|
|
|
|
Retrieves the title and URL data from the item XML node
|
|
|
|
|
|
|
|
item: An elementtree.ElementTree element representing the <item> tag of the RSS feed
|
|
|
|
|
|
|
|
Returns: A tuple containing two strings representing title and URL respectively
|
|
|
|
"""
|
2014-04-25 21:39:43 -04:00
|
|
|
title = item.title
|
2014-03-10 01:18:05 -04:00
|
|
|
if title:
|
|
|
|
title = title.replace(' ', '.')
|
|
|
|
|
2014-04-25 21:39:43 -04:00
|
|
|
url = item.link
|
2014-03-10 01:18:05 -04:00
|
|
|
if url:
|
|
|
|
url = url.replace('&', '&')
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
return (title, url)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-04-27 06:31:54 -04:00
|
|
|
def getSearchResults(self, show, season, ep_objs, seasonSearch=False, manualSearch=False):
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
itemList = []
|
2014-04-27 06:31:54 -04:00
|
|
|
results = {}
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-04-27 06:31:54 -04:00
|
|
|
self._checkAuth()
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-04-27 06:31:54 -04:00
|
|
|
for ep_obj in ep_objs:
|
2014-04-27 08:13:46 -04:00
|
|
|
if not seasonSearch:
|
|
|
|
logger.log(u'Searching "%s" for "%s" as "%s"'
|
|
|
|
% (self.name, ep_obj.prettyName(), ep_obj.scene_prettyName()))
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-04-27 06:31:54 -04:00
|
|
|
self.cache.updateCache()
|
|
|
|
results = self.cache.searchCache(ep_obj, manualSearch)
|
|
|
|
logger.log(u"Cache results: " + str(results), logger.DEBUG)
|
|
|
|
logger.log(u"manualSearch: " + str(manualSearch), logger.DEBUG)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-04-27 06:31:54 -04:00
|
|
|
# if we got some results then use them no matter what.
|
|
|
|
# OR
|
|
|
|
# return anyway unless we're doing a manual search
|
2014-04-27 07:32:22 -04:00
|
|
|
if results:
|
2014-04-27 06:31:54 -04:00
|
|
|
return results
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-04-27 06:31:54 -04:00
|
|
|
abd = False
|
|
|
|
if show.air_by_date:
|
|
|
|
abd = True
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-04-27 06:31:54 -04:00
|
|
|
if seasonSearch:
|
2014-04-27 06:40:50 -04:00
|
|
|
for curString in self._get_season_search_strings(show, ep_obj.scene_season, ep_obj.scene_episode if not abd else ep_obj._airdate, abd=abd):
|
2014-04-27 06:31:54 -04:00
|
|
|
itemList += self._doSearch(curString)
|
|
|
|
else:
|
2014-04-27 06:40:50 -04:00
|
|
|
for curString in self._get_episode_search_strings(show, ep_obj.scene_season, ep_obj.scene_episode if not abd else ep_obj._airdate, abd=abd):
|
2014-04-27 06:31:54 -04:00
|
|
|
itemList += self._doSearch(curString, show=show)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
for item in itemList:
|
|
|
|
|
|
|
|
(title, url) = self._get_title_and_url(item)
|
|
|
|
|
|
|
|
quality = self.getQuality(item)
|
|
|
|
|
|
|
|
# parse the file name
|
|
|
|
try:
|
|
|
|
myParser = NameParser(False)
|
|
|
|
parse_result = myParser.parse(title, True)
|
|
|
|
except InvalidNameException:
|
|
|
|
logger.log(u"Unable to parse the filename " + title + " into a valid episode", logger.WARNING)
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not show.air_by_date:
|
|
|
|
# this check is meaningless for non-season searches
|
2014-03-25 01:57:24 -04:00
|
|
|
if (parse_result.season_number != None and parse_result.season_number != season) or (
|
|
|
|
parse_result.season_number == None and season != 1):
|
|
|
|
logger.log(u"The result " + title + " doesn't seem to be a valid episode for season " + str(
|
|
|
|
season) + ", ignoring", logger.DEBUG)
|
2014-03-10 01:18:05 -04:00
|
|
|
continue
|
|
|
|
|
|
|
|
# we just use the existing info for normal searches
|
|
|
|
actual_season = season
|
|
|
|
actual_episodes = parse_result.episode_numbers
|
|
|
|
|
|
|
|
else:
|
|
|
|
if not parse_result.air_by_date:
|
2014-03-25 01:57:24 -04:00
|
|
|
logger.log(
|
|
|
|
u"This is supposed to be an air-by-date search but the result " + title + " didn't parse as one, skipping it",
|
|
|
|
logger.DEBUG)
|
2014-03-10 01:18:05 -04:00
|
|
|
continue
|
|
|
|
|
|
|
|
myDB = db.DBConnection()
|
2014-03-25 01:57:24 -04:00
|
|
|
sql_results = myDB.select("SELECT season, episode FROM tv_episodes WHERE showid = ? AND airdate = ?",
|
|
|
|
[show.indexerid, parse_result.air_date.toordinal()])
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
if len(sql_results) != 1:
|
2014-03-25 01:57:24 -04:00
|
|
|
logger.log(
|
|
|
|
u"Tried to look up the date for the episode " + title + " but the database didn't give proper results, skipping it",
|
|
|
|
logger.WARNING)
|
2014-03-10 01:18:05 -04:00
|
|
|
continue
|
|
|
|
|
|
|
|
actual_season = int(sql_results[0]["season"])
|
|
|
|
actual_episodes = [int(sql_results[0]["episode"])]
|
|
|
|
|
|
|
|
# make sure we want the episode
|
|
|
|
wantEp = True
|
|
|
|
for epNo in actual_episodes:
|
2014-04-27 06:31:54 -04:00
|
|
|
if not show.wantEpisode(actual_season, epNo, quality, manualSearch=manualSearch):
|
2014-03-10 01:18:05 -04:00
|
|
|
wantEp = False
|
|
|
|
break
|
|
|
|
|
|
|
|
if not wantEp:
|
2014-03-25 01:57:24 -04:00
|
|
|
logger.log(
|
|
|
|
u"Ignoring result " + title + " because we don't want an episode that is " + Quality.qualityStrings[
|
|
|
|
quality], logger.DEBUG)
|
2014-03-10 01:18:05 -04:00
|
|
|
continue
|
|
|
|
|
|
|
|
logger.log(u"Found result " + title + " at " + url, logger.DEBUG)
|
|
|
|
|
|
|
|
# make a result object
|
|
|
|
epObj = []
|
|
|
|
for curEp in actual_episodes:
|
|
|
|
epObj.append(show.getEpisode(actual_season, curEp))
|
|
|
|
|
|
|
|
result = self.getResult(epObj)
|
|
|
|
result.url = url
|
|
|
|
result.name = title
|
|
|
|
result.quality = quality
|
|
|
|
result.provider = self
|
|
|
|
result.content = None
|
|
|
|
|
|
|
|
if len(epObj) == 1:
|
|
|
|
epNum = epObj[0].episode
|
|
|
|
elif len(epObj) > 1:
|
|
|
|
epNum = MULTI_EP_RESULT
|
2014-03-25 01:57:24 -04:00
|
|
|
logger.log(u"Separating multi-episode result to check for later - result contains episodes: " + str(
|
|
|
|
parse_result.episode_numbers), logger.DEBUG)
|
2014-03-10 01:18:05 -04:00
|
|
|
elif len(epObj) == 0:
|
|
|
|
epNum = SEASON_RESULT
|
|
|
|
result.extraInfo = [show]
|
|
|
|
logger.log(u"Separating full season result to check for later", logger.DEBUG)
|
|
|
|
|
|
|
|
if epNum in results:
|
|
|
|
results[epNum].append(result)
|
|
|
|
else:
|
2014-04-27 06:31:54 -04:00
|
|
|
results = {epNum:[result]}
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
return results
|
|
|
|
|
|
|
|
def findPropers(self, search_date=None):
|
|
|
|
|
|
|
|
results = self.cache.listPropers(search_date)
|
|
|
|
|
|
|
|
return [classes.Proper(x['name'], x['url'], datetime.datetime.fromtimestamp(x['time'])) for x in results]
|
|
|
|
|
|
|
|
|
|
|
|
class NZBProvider(GenericProvider):
|
|
|
|
def __init__(self, name):
|
|
|
|
GenericProvider.__init__(self, name)
|
|
|
|
|
|
|
|
self.providerType = GenericProvider.NZB
|
|
|
|
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
class TorrentProvider(GenericProvider):
|
2014-03-10 01:18:05 -04:00
|
|
|
def __init__(self, name):
|
|
|
|
GenericProvider.__init__(self, name)
|
|
|
|
|
2014-04-27 09:46:08 -04:00
|
|
|
self.providerType = GenericProvider.TORRENT
|