1
0
mirror of https://github.com/moparisthebest/SickRage synced 2024-11-05 17:05:03 -05:00

Fixed issues with WebAPI and showPoster function

This commit is contained in:
echel0n 2014-12-19 03:40:09 -08:00
parent a6773a4be8
commit 26e4979f10

View File

@ -37,7 +37,6 @@ from sickbeard import processTV
from sickbeard import network_timezones, sbdatetime
from sickbeard.exceptions import ex
from sickbeard.common import Quality, Overview, qualityPresetStrings, statusStrings, SNATCHED, SNATCHED_PROPER, DOWNLOADED, SKIPPED, UNAIRED, IGNORED, ARCHIVED, WANTED, UNKNOWN
from sickbeard.webserve import WebRoot
try:
import json
@ -176,9 +175,9 @@ class ApiHandler(RequestHandler):
if not (multiCmds and cmd in ('show.getposter', 'show.getbanner')): # skip these cmd while chaining
try:
if cmd in _functionMaper:
curOutDict = _functionMaper.get(cmd)(curArgs, curKwargs).run()
curOutDict = _functionMaper.get(cmd)(curArgs, curKwargs).run(self)
elif _is_int(cmd):
curOutDict = TVDBShorthandWrapper(curArgs, curKwargs, cmd).run()
curOutDict = TVDBShorthandWrapper(curArgs, curKwargs, cmd).run(self)
else:
curOutDict = _responds(RESULT_ERROR, "No such cmd: '" + cmd + "'")
except ApiError, e: # Api errors that we raised, they are harmless
@ -202,7 +201,7 @@ class ApiHandler(RequestHandler):
if multiCmds: # if we had multiple cmds we have to wrap it in a response dict
outDict = _responds(RESULT_SUCCESS, outDict)
else: # index / no cmd given
outDict = CMD_SickBeard(args, kwargs).run()
outDict = CMD_SickBeard(args, kwargs).run(self)
return outDict
@ -240,6 +239,33 @@ class ApiHandler(RequestHandler):
curKwargs[kwarg] = kwargs[kwarg]
return curArgs, curKwargs
def showPoster(self, show=None, which=None):
# Redirect initial poster/banner thumb to default images
if which[0:6] == 'poster':
default_image_name = 'poster.png'
else:
default_image_name = 'banner.png'
# image_path = ek.ek(os.path.join, sickbeard.PROG_DIR, 'gui', 'slick', 'images', default_image_name)
static_image_path = os.path.join('/images', default_image_name)
if show and sickbeard.helpers.findCertainShow(sickbeard.showList, int(show)):
cache_obj = image_cache.ImageCache()
image_file_name = None
if which == 'poster':
image_file_name = cache_obj.poster_path(show)
if which == 'poster_thumb' or which == 'small':
image_file_name = cache_obj.poster_thumb_path(show)
if which == 'banner':
image_file_name = cache_obj.banner_path(show)
if which == 'banner_thumb':
image_file_name = cache_obj.banner_thumb_path(show)
if ek.ek(os.path.isfile, image_file_name):
static_image_path = os.path.normpath(image_file_name.replace(sickbeard.CACHE_DIR, '/cache'))
static_image_path = static_image_path.replace('\\', '/')
return self.redirect(static_image_path)
class ApiCall(ApiHandler):
_help = {"desc": "No help message available. Please tell the devs that a help msg is missing for this cmd"}
@ -256,11 +282,11 @@ class ApiCall(ApiHandler):
if 'help' in kwargs:
self.run = self.return_help
def run(self):
def run(self, rh):
# override with real output function in subclass
return {}
def return_help(self):
def return_help(self, **kwargs):
try:
if self._requiredParams:
pass
@ -297,7 +323,7 @@ class ApiCall(ApiHandler):
del self._help["desc"]
return _responds(RESULT_SUCCESS, self._help, msg)
def return_missing(self):
def return_missing(self, **kwargs):
if len(self._missing) == 1:
msg = "The required parameter: '" + self._missing[0] + "' was not set"
else:
@ -424,7 +450,6 @@ class ApiCall(ApiHandler):
raise ApiError(u"param: '" + str(name) + "' with given value: '" + str(
value) + "' is out of allowed range '" + str(allowedValues) + "'")
class TVDBShorthandWrapper(ApiCall):
_help = {"desc": "this is an internal function wrapper. call the help command directly for more information"}
@ -439,15 +464,15 @@ class TVDBShorthandWrapper(ApiCall):
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" internal function wrapper """
args = (self.sid,) + self.origArgs
if self.e:
return CMD_Episode(args, self.kwargs).run()
return CMD_Episode(args, self.kwargs).run(rh)
elif self.s:
return CMD_ShowSeasons(args, self.kwargs).run()
return CMD_ShowSeasons(args, self.kwargs).run(rh)
else:
return CMD_Show(args, self.kwargs).run()
return CMD_Show(args, self.kwargs).run(rh)
# ###############################
@ -639,10 +664,10 @@ class CMD_Help(ApiCall):
_functionMaper.keys())
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" display help information for a given subject/command """
if self.subject in _functionMaper:
out = _responds(RESULT_SUCCESS, _functionMaper.get(self.subject)((), {"help": 1}).run())
out = _responds(RESULT_SUCCESS, _functionMaper.get(self.subject)((), {"help": 1}).run(rh))
else:
out = _responds(RESULT_FAILURE, msg="No such cmd")
return out
@ -671,7 +696,7 @@ class CMD_ComingEpisodes(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" display the coming episodes """
today = datetime.date.today().toordinal()
next_week = (datetime.date.today() + datetime.timedelta(days=7)).toordinal()
@ -792,7 +817,7 @@ class CMD_Episode(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" display detailed info about an episode """
showObj = sickbeard.helpers.findCertainShow(sickbeard.showList, int(self.indexerid))
if not showObj:
@ -855,7 +880,7 @@ class CMD_EpisodeSearch(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" search for an episode """
showObj = sickbeard.helpers.findCertainShow(sickbeard.showList, int(self.indexerid))
if not showObj:
@ -912,7 +937,7 @@ class CMD_EpisodeSetStatus(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" set status of an episode or a season (when no ep is provided) """
showObj = sickbeard.helpers.findCertainShow(sickbeard.showList, int(self.indexerid))
if not showObj:
@ -1021,7 +1046,7 @@ class CMD_SubtitleSearch(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" search episode subtitles """
showObj = sickbeard.helpers.findCertainShow(sickbeard.showList, int(self.indexerid))
if not showObj:
@ -1073,7 +1098,7 @@ class CMD_Exceptions(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" display scene exceptions for all or a given show """
myDB = db.DBConnection("cache.db", row_type="dict")
@ -1117,7 +1142,7 @@ class CMD_History(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" display sickrage downloaded/snatched history """
typeCodes = []
@ -1172,7 +1197,7 @@ class CMD_HistoryClear(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" clear sickrage's history """
myDB = db.DBConnection()
myDB.action("DELETE FROM history WHERE 1=1")
@ -1190,7 +1215,7 @@ class CMD_HistoryTrim(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" trim sickrage's history """
myDB = db.DBConnection()
myDB.action("DELETE FROM history WHERE date < " + str(
@ -1211,7 +1236,7 @@ class CMD_Failed(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" display failed downloads """
myDB = db.DBConnection('failed.db', row_type="dict")
@ -1233,7 +1258,7 @@ class CMD_Backlog(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" display backlogged episodes """
shows = []
@ -1277,7 +1302,7 @@ class CMD_Logs(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" view sickrage's log """
# 10 = Debug / 20 = Info / 30 = Warning / 40 = Error
minLevel = logger.reverseNames[str(self.min_level).upper()]
@ -1349,7 +1374,7 @@ class CMD_PostProcess(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" Starts the postprocess """
if not self.path and not sickbeard.TV_DOWNLOAD_DIR:
return _responds(RESULT_FAILURE, msg="You need to provide a path or set TV Download Dir")
@ -1378,7 +1403,7 @@ class CMD_SickBeard(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" display misc sickrage related information """
data = {"sr_version": sickbeard.BRANCH, "api_version": self.version,
"api_commands": sorted(_functionMaper.keys())}
@ -1401,7 +1426,7 @@ class CMD_SickBeardAddRootDir(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" add a parent directory to sickrage's config """
self.location = urllib.unquote_plus(self.location)
@ -1452,14 +1477,14 @@ class CMD_SickBeardCheckScheduler(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" query the scheduler """
myDB = db.DBConnection()
sqlResults = myDB.select("SELECT last_backlog FROM info")
backlogPaused = sickbeard.searchQueueScheduler.action.is_backlog_paused() # @UndefinedVariable
backlogRunning = sickbeard.searchQueueScheduler.action.is_backlog_in_progress() # @UndefinedVariable
nextBacklog = sickbeard.backlogSearchScheduler.nextRun().strftime(dateFormat).decode(sickbeard.SYS_ENCODING)
nextBacklog = sickbeard.backlogSearchScheduler.nextrun().strftime(dateFormat).decode(sickbeard.SYS_ENCODING)
data = {"backlog_is_paused": int(backlogPaused), "backlog_is_running": int(backlogRunning),
"last_backlog": _ordinal_to_dateForm(sqlResults[0]["last_backlog"]),
@ -1479,7 +1504,7 @@ class CMD_SickBeardDeleteRootDir(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" delete a parent directory from sickrage's config """
if sickbeard.ROOT_DIRS == "":
return _responds(RESULT_FAILURE, _getRootDirs(), msg="No root directories detected")
@ -1522,7 +1547,7 @@ class CMD_SickBeardGetDefaults(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" get sickrage user defaults """
anyQualities, bestQualities = _mapQuality(sickbeard.QUALITY_DEFAULT)
@ -1542,7 +1567,7 @@ class CMD_SickBeardGetMessages(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
messages = []
for cur_notification in ui.notifications.get_notifications(self.request.remote_ip):
messages.append({"title": cur_notification.title,
@ -1560,7 +1585,7 @@ class CMD_SickBeardGetRootDirs(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" get the parent directories defined in sickrage's config """
return _responds(RESULT_SUCCESS, _getRootDirs())
@ -1578,7 +1603,7 @@ class CMD_SickBeardPauseBacklog(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" pause the backlog search """
if self.pause:
sickbeard.searchQueueScheduler.action.pause_backlog() # @UndefinedVariable
@ -1597,7 +1622,7 @@ class CMD_SickBeardPing(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" check to see if sickrage is running """
self.set_header('Cache-Control', "max-age=0,no-cache,no-store")
if sickbeard.started:
@ -1615,7 +1640,7 @@ class CMD_SickBeardRestart(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" restart sickrage """
sickbeard.events.put(sickbeard.events.SystemEvent.RESTART)
return _responds(RESULT_SUCCESS, msg="SickRage is restarting...")
@ -1648,7 +1673,7 @@ class CMD_SickBeardSearchIndexers(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" search for show at tvdb with a given string and language """
results = []
@ -1770,7 +1795,7 @@ class CMD_SickBeardSetDefaults(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" set sickrage user defaults """
quality_map = {'sdtv': Quality.SDTV,
@ -1829,7 +1854,7 @@ class CMD_SickBeardShutdown(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" shutdown sickrage """
sickbeard.events.put(sickbeard.events.SystemEvent.SHUTDOWN)
return _responds(RESULT_SUCCESS, msg="SickRage is shutting down...")
@ -1854,15 +1879,15 @@ class CMD_Show(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" display information for a given show """
showObj = sickbeard.helpers.findCertainShow(sickbeard.showList, int(self.indexerid))
if not showObj:
return _responds(RESULT_FAILURE, msg="Show not found")
showDict = {}
showDict["season_list"] = CMD_ShowSeasonList((), {"indexerid": self.indexerid}).run()["data"]
showDict["cache"] = CMD_ShowCache((), {"indexerid": self.indexerid}).run()["data"]
showDict["season_list"] = CMD_ShowSeasonList((), {"indexerid": self.indexerid}).run(rh)["data"]
showDict["cache"] = CMD_ShowCache((), {"indexerid": self.indexerid}).run(rh)["data"]
genreList = []
if showObj.genre:
@ -1964,7 +1989,7 @@ class CMD_ShowAddExisting(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" add a show in sickrage with an existing folder """
showObj = sickbeard.helpers.findCertainShow(sickbeard.showList, int(self.indexerid))
if showObj:
@ -1974,7 +1999,7 @@ class CMD_ShowAddExisting(ApiCall):
return _responds(RESULT_FAILURE, msg='Not a valid location')
indexerName = None
indexerResult = CMD_SickBeardSearchIndexers([], {indexer_ids[self.indexer]: self.indexerid}).run()
indexerResult = CMD_SickBeardSearchIndexers([], {indexer_ids[self.indexer]: self.indexerid}).run(rh)
if indexerResult['result'] == result_type_map[RESULT_SUCCESS]:
if not indexerResult['data']['results']:
@ -2078,7 +2103,7 @@ class CMD_ShowAddNew(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" add a show in sickrage with an existing folder """
showObj = sickbeard.helpers.findCertainShow(sickbeard.showList, int(self.indexerid))
if showObj:
@ -2139,7 +2164,7 @@ class CMD_ShowAddNew(ApiCall):
newStatus = self.status
indexerName = None
indexerResult = CMD_SickBeardSearchIndexers([], {indexer_ids[self.indexer]: self.indexerid}).run()
indexerResult = CMD_SickBeardSearchIndexers([], {indexer_ids[self.indexer]: self.indexerid}).run(rh)
if indexerResult['result'] == result_type_map[RESULT_SUCCESS]:
if not indexerResult['data']['results']:
@ -2194,7 +2219,7 @@ class CMD_ShowCache(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" check sickrage's cache to see if the banner or poster image for a show is valid """
showObj = sickbeard.helpers.findCertainShow(sickbeard.showList, int(self.indexerid))
if not showObj:
@ -2236,7 +2261,7 @@ class CMD_ShowDelete(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" delete a show in sickrage """
showObj = sickbeard.helpers.findCertainShow(sickbeard.showList, int(self.indexerid))
if not showObj:
@ -2273,7 +2298,7 @@ class CMD_ShowGetQuality(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" get quality setting for a show in sickrage """
showObj = sickbeard.helpers.findCertainShow(sickbeard.showList, int(self.indexerid))
if not showObj:
@ -2302,9 +2327,9 @@ class CMD_ShowGetPoster(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" get the poster for a show in sickrage """
return {'outputType': 'image', 'image': WebRoot().showPoster(self.indexerid, 'poster')}
return {'outputType': 'image', 'image': rh.showPoster(self.indexerid, 'poster')}
class CMD_ShowGetBanner(ApiCall):
@ -2325,9 +2350,9 @@ class CMD_ShowGetBanner(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" get the banner for a show in sickrage """
return {'outputType': 'image', 'image': WebRoot().showPoster(self.indexerid, 'banner')}
return {'outputType': 'image', 'image': self.showPoster(self.indexerid, 'banner')}
class CMD_ShowPause(ApiCall):
@ -2350,7 +2375,7 @@ class CMD_ShowPause(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" set a show's paused state in sickrage """
showObj = sickbeard.helpers.findCertainShow(sickbeard.showList, int(self.indexerid))
if not showObj:
@ -2381,7 +2406,7 @@ class CMD_ShowRefresh(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" refresh a show in sickrage """
showObj = sickbeard.helpers.findCertainShow(sickbeard.showList, int(self.indexerid))
if not showObj:
@ -2417,7 +2442,7 @@ class CMD_ShowSeasonList(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" display the season list for a given show """
showObj = sickbeard.helpers.findCertainShow(sickbeard.showList, int(self.indexerid))
if not showObj:
@ -2457,7 +2482,7 @@ class CMD_ShowSeasons(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" display a listing of episodes for all or a given show """
showObj = sickbeard.helpers.findCertainShow(sickbeard.showList, int(self.indexerid))
if not showObj:
@ -2538,7 +2563,7 @@ class CMD_ShowSetQuality(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" set the quality for a show in sickrage by taking in a deliminated
string of qualities, map to their value and combine for new values
"""
@ -2595,7 +2620,7 @@ class CMD_ShowStats(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" display episode statistics for a given show """
showObj = sickbeard.helpers.findCertainShow(sickbeard.showList, int(self.indexerid))
if not showObj:
@ -2704,7 +2729,7 @@ class CMD_ShowUpdate(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" update a show in sickrage """
showObj = sickbeard.helpers.findCertainShow(sickbeard.showList, int(self.indexerid))
if not showObj:
@ -2734,7 +2759,7 @@ class CMD_Shows(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" display_is_int_multi( self.indexerid )shows in sickrage """
shows = {}
for curShow in sickbeard.showList:
@ -2769,7 +2794,7 @@ class CMD_Shows(ApiCall):
showDict['next_ep_airdate'] = ''
showDict["cache"] = \
CMD_ShowCache((), {"indexerid": curShow.indexerid}).run()["data"]
CMD_ShowCache((), {"indexerid": curShow.indexerid}).run(rh)["data"]
if not showDict["network"]:
showDict["network"] = ""
if self.sort == "name":
@ -2790,7 +2815,7 @@ class CMD_ShowsStats(ApiCall):
# super, missing, help
ApiCall.__init__(self, args, kwargs)
def run(self):
def run(self, rh):
""" display the global shows and episode stats """
stats = {}