From 047568ec76fb19c9b32f981aa1c24cd86715727f Mon Sep 17 00:00:00 2001 From: echel0n Date: Wed, 12 Mar 2014 19:44:40 -0700 Subject: [PATCH] Improved the API Handlers and modulaized it. --- sickbeard/__init__.py | 5 +-- sickbeard/indexers/__init__.py | 18 ++++++++- sickbeard/indexers/generic.py | 61 +++++++++++++++++++++++++++++++ sickbeard/indexers/indexer_api.py | 25 +++++-------- sickbeard/metadata/generic.py | 1 - sickbeard/metadata/xbmc_12plus.py | 3 +- 6 files changed, 90 insertions(+), 23 deletions(-) create mode 100644 sickbeard/indexers/generic.py diff --git a/sickbeard/__init__.py b/sickbeard/__init__.py index a8014452..e7770c62 100644 --- a/sickbeard/__init__.py +++ b/sickbeard/__init__.py @@ -29,9 +29,8 @@ import urllib from threading import Lock # apparently py2exe won't build these unless they're imported somewhere -from sickbeard import indexers -from indexers import indexer_api, indexer_exceptions, indexer_config -from sickbeard import providers, metadata +from sickbeard import providers, metadata, indexers +from indexers import indexer_api, indexer_exceptions from providers import ezrss, tvtorrents, btn, newznab, womble, thepiratebay, torrentleech, kat, publichd, iptorrents, omgwtfnzbs, scc, hdtorrents, torrentday, hdbits, nextgen from sickbeard.config import CheckSection, check_setting_int, check_setting_str, ConfigMigrator from sickbeard import searchCurrent, searchBacklog, showUpdater, versionChecker, properFinder, autoPostProcesser, subtitles, traktWatchListChecker diff --git a/sickbeard/indexers/__init__.py b/sickbeard/indexers/__init__.py index 8c650d6f..14a9eb78 100644 --- a/sickbeard/indexers/__init__.py +++ b/sickbeard/indexers/__init__.py @@ -16,4 +16,20 @@ # You should have received a copy of the GNU General Public License # along with Sick Beard. If not, see . -__all__ = ["indexer_api", "indexer_exceptions", "indexer_config"] \ No newline at end of file +__all__ = ["generic","indexer_api","indexer_exceptions"] + +import indexer_api, indexer_exceptions + +def getClientModule(name): + + name = name.lower() + prefix = "sickbeard.indexers." + + return __import__(prefix+name, fromlist=__all__) + +def getClientIstance(name): + + module = getClientModule(name) + className = module.__class__.__name__ + + return getattr(module, className) \ No newline at end of file diff --git a/sickbeard/indexers/generic.py b/sickbeard/indexers/generic.py new file mode 100644 index 00000000..13643c7b --- /dev/null +++ b/sickbeard/indexers/generic.py @@ -0,0 +1,61 @@ +# Author: Nic Wolfe +# 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 . + +class GenericIndexer(object): + def __init__(self, indexer): + + INDEXER_NONE = None + INDEXER_TVDB = 'Tvdb' + INDEXER_TVRAGE = 'TVRage' + + INDEXER_NAME = {} + INDEXER_NAME[INDEXER_NONE] = '' + INDEXER_NAME[INDEXER_TVDB] = 'theTVDB' + INDEXER_NAME[INDEXER_TVRAGE] = 'TVRage' + + INDEXER_API_KEY = {} + INDEXER_API_KEY[INDEXER_NONE] = '' + INDEXER_API_KEY[INDEXER_TVDB] = '9DAF49C96CBF8DAC' + INDEXER_API_KEY[INDEXER_TVRAGE] = 'Uhewg1Rr0o62fvZvUIZt' + + INDEXER_BASEURL = {} + INDEXER_BASEURL[INDEXER_NONE] = '' + INDEXER_BASEURL[INDEXER_TVDB] = 'http://thetvdb.com/api/' + INDEXER_API_KEY[INDEXER_TVDB] + INDEXER_BASEURL[INDEXER_TVRAGE] = 'http://tvrage.com/showinfo?key=' + INDEXER_API_KEY[INDEXER_TVRAGE] + 'sid=' + + INDEXER_API_PARMS = {} + INDEXER_API_PARMS[INDEXER_NONE] = '' + INDEXER_API_PARMS[INDEXER_TVDB] = {'apikey': INDEXER_API_KEY[INDEXER_TVDB], + 'language': 'en', + 'useZip': True} + + INDEXER_API_PARMS[INDEXER_TVRAGE] = {'apikey': INDEXER_API_KEY[INDEXER_TVRAGE], + 'language': 'en'} + + config = {} + config['valid_languages'] = [ + "da", "fi", "nl", "de", "it", "es", "fr","pl", "hu","el","tr", + "ru","he","ja","pt","zh","cs","sl", "hr","ko","en","sv","no"] + + config['langabbv_to_id'] = {'el': 20, 'en': 7, 'zh': 27, + 'it': 15, 'cs': 28, 'es': 16, 'ru': 22, 'nl': 13, 'pt': 26, 'no': 9, + 'tr': 21, 'pl': 18, 'fr': 17, 'hr': 31, 'de': 14, 'da': 10, 'fi': 11, + 'hu': 19, 'ja': 25, 'he': 24, 'ko': 32, 'sv': 8, 'sl': 30} + + self.api_parms = config['api_parms'] = INDEXER_API_PARMS[indexer] + self.name = config['name'] = INDEXER_NAME[indexer] \ No newline at end of file diff --git a/sickbeard/indexers/indexer_api.py b/sickbeard/indexers/indexer_api.py index 830fcc86..c3937b76 100644 --- a/sickbeard/indexers/indexer_api.py +++ b/sickbeard/indexers/indexer_api.py @@ -18,30 +18,23 @@ import os import sickbeard +import generic -from indexer_config import * from lib.tvdb_api.tvdb_api import Tvdb from lib.tvrage_api.tvrage_api import TVRage -class indexerApi: - def __new__(self, indexer): - cls = type(indexer) - new_type = type(cls.__name__ + '_wrapped', (indexerApi, cls), {}) - return object.__new__(new_type) +class indexerApi(generic.GenericIndexer): + def __init__(self, indexer=None, *args, **kwargs): + super(indexerApi, self).__init__(indexer) - def __init__(self, indexer=None, language=None, *args, **kwargs): - self.name = indexer - self.config = INDEXER_CONFIG.copy() + if indexer: + self.api_parms.update(**kwargs) - # wrap the indexer API object and return it back - if indexer is not None: if sickbeard.CACHE_DIR: - INDEXER_API_PARMS[indexer]['cache'] = os.path.join(sickbeard.CACHE_DIR, indexer) + self.api_parms['cache'] = os.path.join(sickbeard.CACHE_DIR, indexer) - lINDEXER_API_PARMS = INDEXER_API_PARMS[indexer].copy() - lINDEXER_API_PARMS.update(**kwargs) - - self._wrapped = eval(indexer)(*args, **lINDEXER_API_PARMS) + # wrap the indexer API object and return it back + self._wrapped = eval(indexer)(*args, **self.api_parms) def __getattr__(self, attr): return getattr(self._wrapped, attr) diff --git a/sickbeard/metadata/generic.py b/sickbeard/metadata/generic.py index f792767e..08afb4c9 100644 --- a/sickbeard/metadata/generic.py +++ b/sickbeard/metadata/generic.py @@ -32,7 +32,6 @@ from sickbeard import logger from sickbeard import encodingKludge as ek from sickbeard.exceptions import ex - from sickbeard.indexers import indexer_api, indexer_exceptions class GenericMetadata(): diff --git a/sickbeard/metadata/xbmc_12plus.py b/sickbeard/metadata/xbmc_12plus.py index e5b71b24..14dc8c43 100644 --- a/sickbeard/metadata/xbmc_12plus.py +++ b/sickbeard/metadata/xbmc_12plus.py @@ -21,7 +21,6 @@ import datetime import sickbeard from sickbeard.indexers import indexer_api, indexer_exceptions -from sickbeard.indexers.indexer_config import INDEXER_BASEURL from sickbeard import logger, exceptions, helpers from sickbeard.exceptions import ex @@ -154,7 +153,7 @@ class XBMC_12PlusMetadata(generic.GenericMetadata): episodeguideurl = etree.SubElement(episodeguide, "url") episodeguideurl2 = etree.SubElement(tv_node, "episodeguideurl") if getattr(myShow, 'id', None) is not None: - showurl = INDEXER_BASEURL[show_obj.indexer] + '/series/' + myShow["id"] + '/all/en.zip' + showurl = indexer_api.indexerApi(show_obj.indexer) + '/series/' + myShow["id"] + '/all/en.zip' episodeguideurl.text = showurl episodeguideurl2.text = showurl