コード例 #1
0
ファイル: api.py プロジェクト: toroettg/pytvdbapi
    def __init__(self, api_key, **kwargs):
        self.config = dict()

        #cache old searches to avoid hitting the server
        self.search_buffer = dict()

        #Store the path to where we are
        self.path = os.path.abspath(os.path.dirname(__file__))

        #extract all argument and store for later use
        self.config['force_lang'] = kwargs.get("force_lang", False)
        self.config['api_key'] = api_key
        self.config['cache_dir'] = kwargs.get("cache_dir", os.path.join(tempfile.gettempdir(), name))
        self.config['actors'] = kwargs.get('actors', False)
        self.config['banners'] = kwargs.get('banners', False)
        self.config['ignore_case'] = kwargs.get('ignore_case', False)

        #Create the loader object to use
        self.loader = Loader(self.config['cache_dir'])

        language_file = resource_filename(__name__, 'data/languages.xml')

        #If requested, update the local language file from the server
        if self.config['force_lang']:
            logger.debug("updating Language file from server")
            with open(language_file, "wt", encoding='utf-8') as languages:
                language_data = self.loader.load(__languages__.format(**self.config))
                languages.write(language_data)

        #Setup the list of supported languages
        self.languages = LanguageList(generate_tree(language_file))

        #Create the list of available mirrors
        tree = generate_tree(self.loader.load(__mirrors__.format(**self.config)))
        self.mirrors = MirrorList(tree)
コード例 #2
0
ファイル: api.py プロジェクト: BenoitZugmeyer/pytvdbapi
    def __init__(self, api_key, **kwargs):
        self.config = dict()

        #cache old searches to avoid hitting the server
        self.search_buffer = dict()

        #Store the path to where we are
        self.path = os.path.abspath(os.path.dirname(__file__))

        if 'force_lang' in kwargs:
            logger.warning(
                "'force_lang' keyword argument is deprecated as of version 0.4"
            )

        #extract all argument and store for later use
        self.config['api_key'] = api_key
        self.config['cache_dir'] = kwargs.get(
            "cache_dir", os.path.join(tempfile.gettempdir(), name))
        self.config['actors'] = kwargs.get('actors', False)
        self.config['banners'] = kwargs.get('banners', False)
        self.config['ignore_case'] = kwargs.get('ignore_case', False)

        #Create the loader object to use
        self.loader = Loader(self.config['cache_dir'])

        #Create the list of available mirrors
        tree = generate_tree(
            self.loader.load(__mirrors__.format(**self.config)))
        self.mirrors = MirrorList(tree)
コード例 #3
0
ファイル: api.py プロジェクト: BenoitZugmeyer/pytvdbapi
    def __init__(self, api_key, **kwargs):
        self.config = dict()

        #cache old searches to avoid hitting the server
        self.search_buffer = dict()

        #Store the path to where we are
        self.path = os.path.abspath(os.path.dirname(__file__))

        if 'force_lang' in kwargs:
            logger.warning("'force_lang' keyword argument is deprecated as of version 0.4")

        #extract all argument and store for later use
        self.config['api_key'] = api_key
        self.config['cache_dir'] = kwargs.get("cache_dir", os.path.join(tempfile.gettempdir(), name))
        self.config['actors'] = kwargs.get('actors', False)
        self.config['banners'] = kwargs.get('banners', False)
        self.config['ignore_case'] = kwargs.get('ignore_case', False)

        #Create the loader object to use
        self.loader = Loader(self.config['cache_dir'])

        #Create the list of available mirrors
        tree = generate_tree(self.loader.load(__mirrors__.format(**self.config)))
        self.mirrors = MirrorList(tree)
コード例 #4
0
ファイル: api.py プロジェクト: BenoitZugmeyer/pytvdbapi
    def load_banners(self):
        """
        .. versionadded:: 0.4

        Loads the extended banner information into a list of :class:`pytvdbapi.banner.Banner` objects.
        They are available through the *banner_objects* attribute of the show.

        If you have used the :code:`banners=True` keyword when creating the :class:`TVDB` instance the
        banners will be loaded automatically and there is no need to use this function.

        .. seealso::
          :class:`TVDB` for information on how to use the *banners* keyword argument.

        """
        context = {
            'mirror': self.api.mirrors.get_mirror(TypeMask.XML).url,
            'api_key': self.config['api_key'],
            'seriesid': self.id
        }

        url = __banners__.format(**context)
        logger.debug('Loading Banner data from {0}'.format(url))

        data = generate_tree(self.api.loader.load(url))
        mirror = self.api.mirrors.get_mirror(TypeMask.BANNER).url

        self.banner_objects = [
            Banner(mirror, b, self) for b in parse_xml(data, "Banner")
        ]
コード例 #5
0
ファイル: api.py プロジェクト: BenoitZugmeyer/pytvdbapi
    def load_actors(self):
        """
        .. versionadded:: 0.4

        Loads the extended actor information into a list of :class:`pytvdbapi.actor.Actor` objects.
        They are available through the *actor_objects* attribute of the show.

        If you have used the :code:`actors=True` keyword when creating the :class:`TVDB` instance
        the actors will be loaded automatically and there is no need to use this function.

        .. note::
          The :class:`Show` instance always contain a list of actor names. If that is all you need, do not
          use this function to avoid unnecessary network traffic.

        .. seealso::
          :class:`TVDB` for information on how to use the *actors* keyword argument.

        """
        context = {
            'mirror': self.api.mirrors.get_mirror(TypeMask.XML).url,
            'api_key': self.config['api_key'],
            'seriesid': self.id
        }
        url = __actors__.format(**context)

        logger.debug('Loading Actors data from {0}'.format(url))

        data = generate_tree(self.api.loader.load(url))

        mirror = self.api.mirrors.get_mirror(TypeMask.BANNER).url

        #generate all the Actor objects
        self.actor_objects = [
            Actor(mirror, d, self) for d in parse_xml(data, 'Actor')
        ]
コード例 #6
0
ファイル: api.py プロジェクト: fuzzycode/pytvdbapi
    def load_banners(self):
        """
        .. versionadded:: 0.4

        Loads the extended banner information into a list of :class:`pytvdbapi.banner.Banner` objects.
        They are available through the *banner_objects* attribute of the show.

        If you have used the `banners=True` keyword when creating the :class:`TVDB` instance the
        banners will be loaded automatically and there is no need to use this
        function.

        .. seealso::
          :class:`TVDB` for information on how to use the *banners* keyword
          argument.
        """
        context = {'mirror': self.api.mirrors.get_mirror(TypeMask.XML).url,
                   'api_key': self.config['api_key'],
                   'seriesid': self.id}

        url = banners.format(**context)
        logger.debug(u'Loading Banner data from {0}'.format(url))

        data = generate_tree(self.api.loader.load(url))
        mirror = self.api.mirrors.get_mirror(TypeMask.BANNER).url

        # pylint: disable=W0201
        self.banner_objects = [Banner(mirror, b, self) for b in parse_xml(data, "Banner")]
コード例 #7
0
ファイル: api.py プロジェクト: BenoitZugmeyer/pytvdbapi
    def load_actors(self):
        """
        .. versionadded:: 0.4

        Loads the extended actor information into a list of :class:`pytvdbapi.actor.Actor` objects.
        They are available through the *actor_objects* attribute of the show.

        If you have used the :code:`actors=True` keyword when creating the :class:`TVDB` instance
        the actors will be loaded automatically and there is no need to use this function.

        .. note::
          The :class:`Show` instance always contain a list of actor names. If that is all you need, do not
          use this function to avoid unnecessary network traffic.

        .. seealso::
          :class:`TVDB` for information on how to use the *actors* keyword argument.

        """
        context = {'mirror': self.api.mirrors.get_mirror(TypeMask.XML).url,
                   'api_key': self.config['api_key'],
                   'seriesid': self.id}
        url = __actors__.format(**context)

        logger.debug('Loading Actors data from {0}'.format(url))

        data = generate_tree(self.api.loader.load(url))

        mirror = self.api.mirrors.get_mirror(TypeMask.BANNER).url

        #generate all the Actor objects
        self.actor_objects = [Actor(mirror, d, self)
                              for d in parse_xml(data, 'Actor')]
コード例 #8
0
ファイル: api.py プロジェクト: greeeny101/Dreambox.bundle
    def get_episode(self, episode_id, language, cache=True):
        """
        .. versionadded:: 0.4

        :param episode_id: The Episode Id to fetch
        :param language: The language abbreviation to search for. E.g. "en"
        :param cache: If False, the local cache will not be used and the
                    resources will be reloaded from server.

        :return: An :class:`Episode()` instance
        :raise: :exc:`pytvdbapi.error.TVDBIdError` if no episode is found with the given Id


        Given a valid episode Id the corresponding episode data is fetched and
        the :class:`Episode()` instance is returned.

        Example::

            >>> from pytvdbapi import api
            >>> db = api.TVDB("B43FF87DE395DF56")
            >>> episode = db.get_episode(308834, "en") # Load an episode of dexter
            >>> print(episode.id)
            308834

            >>> print(episode.EpisodeName)
            Crocodile

        .. Note:: When the :class:`Episode()` is loaded using :func:`get_episode()`
            the *season* attribute used to link the episode with a season will be None.
        """

        logger.debug(u"Getting episode with id {0} with language {1}".format(episode_id, language))

        if language != 'all' and language not in __LANGUAGES__:
            raise error.TVDBValueError(u"{0} is not a valid language".format(language))

        context = {'episodeid': episode_id, "language": language,
                   'mirror': self.mirrors.get_mirror(TypeMask.XML).url,
                   'api_key': self.config['api_key']}

        url = __episode__.format(**context)
        logger.debug(u'Getting episode from {0}'.format(url))

        try:
            data = self.loader.load(url, cache)
        except error.TVDBNotFoundError:
            raise error.TVDBIdError(u"No Episode with id {0} found".format(episode_id))

        if data.strip():
            data = generate_tree(data)
        else:
            raise error.BadData("Bad data received")

        episodes = parse_xml(data, "Episode")

        if len(episodes) == 0:
            raise error.BadData("Bad data received")
        else:
            return Episode(episodes[0], None, self.config)
コード例 #9
0
ファイル: api.py プロジェクト: thefkboss/Dreambox.bundle
    def get_series(self, series_id, language, cache=True):
        """
        .. versionadded:: 0.4

        :param series_id: The Show Id to fetch
        :param language: The language abbreviation to search for. E.g. "en"
        :param cache: If False, the local cache will not be used and the
                    resources will be reloaded from server.

        :return: A :class:`Show()` instance
        :raise: :exc:`pytvdbapi.error.TVDBValueError`, :exc:`pytvdbapi.error.TVDBIdError`

        Provided a valid Show ID, the data for the show is fetched and a
        corresponding :class:`Show()` object is returned.

        Example::

            >>> from pytvdbapi import api
            >>> db = api.TVDB("B43FF87DE395DF56")
            >>> show = db.get_series( 79349, "en" )  # Load Dexter
            >>> print(show.SeriesName)
            Dexter
        """

        logger.debug(u"Getting series with id {0} with language {1}".format(
            series_id, language))

        if language != 'all' and language not in __LANGUAGES__:
            raise error.TVDBValueError(
                u"{0} is not a valid language".format(language))

        context = {
            'seriesid': series_id,
            "language": language,
            'mirror': self.mirrors.get_mirror(TypeMask.XML).url,
            'api_key': self.config['api_key']
        }

        url = __series__.format(**context)
        logger.debug(u'Getting series from {0}'.format(url))

        try:
            data = self.loader.load(url, cache)
        except error.TVDBNotFoundError:
            raise error.TVDBIdError(
                u"Series id {0} not found".format(series_id))

        if data.strip():
            data = generate_tree(data)
        else:
            raise error.BadData("Bad data received")

        series = parse_xml(data, "Series")

        if len(series) == 0:
            raise error.BadData("Bad data received")
        else:
            return Show(series[0], self, language, self.config)
コード例 #10
0
ファイル: api.py プロジェクト: BenoitZugmeyer/pytvdbapi
    def get_series(self, series_id, language, cache=True):
        """
        .. versionadded:: 0.4

        :param series_id: The Show Id to fetch
        :param language: The language abbreviation to search for. E.g. "en"
        :param cache: If False, the local cache will not be used and the
                    resources will be reloaded from server.

        :return: A :class:`Show()` instance
        :raise: :class:`pytvdbapi.error.TVDBValueError`, :class:`pytvdbapi.error.TVDBIdError`

        Provided a valid Show ID, the data for the show is fetched and a
        corresponding :class:`Show()` object is returned.

        Example::

            >>> from pytvdbapi import api
            >>> db = api.TVDB("B43FF87DE395DF56")
            >>> show = db.get( 79349, "en" )
            >>> show.id
            79349

            >>> show.SeriesName
            'Dexter'

        """

        logger.debug("Getting series with id {0} with language {1}".format(series_id, language))

        if language != 'all' and language not in __LANGUAGES__:
            raise error.TVDBValueError("{0} is not a valid language".format(language))

        context = {'seriesid': series_id, "language": language,
                   'mirror': self.mirrors.get_mirror(TypeMask.XML).url,
                   'api_key': self.config['api_key']}

        url = __series__.format(**context)
        try:
            data = self.loader.load(url, cache)
        except error.TVDBNotFoundError:
            raise error.TVDBIdError("Series id {0} not found".format(series_id))
        except error.ConnectionError as _error:
            logger.debug("Unable to connect to URL: {0}. {1}".format(url, _error))
            raise

        if data.strip():
            data = generate_tree(data)
        else:
            raise error.TVDBIdError("No Show with id {0} found".format(series_id))

        series = parse_xml(data, "Series")
        assert len(series) <= 1, "Should not find more than one series"

        if len(series) >= 1:
            return Show(series[0], self, language, self.config)
        else:
            raise error.TVDBIdError("No Show with id {0} found".format(series_id))
コード例 #11
0
ファイル: test_languages.py プロジェクト: alvra/pytvdbapi
    def setUp(self):
        super(TestLanguage, self).setUp()

        data = utils.file_loader(os.path.join(self.path, "languages.xml"))
        self.languages = language.LanguageList(xmlhelpers.generate_tree(data))

        self.langs = ('da', 'fi', 'nl', 'de', 'it', 'es', 'fr', 'pl', 'hu',
                      'el', 'tr', 'ru', 'he', 'ja', 'pt', 'zh', 'cs', 'sl',
                      'hr', 'ko', 'en', 'sv', 'no')
コード例 #12
0
ファイル: api.py プロジェクト: thefkboss/Dreambox.bundle
    def search(self, show, language, cache=True):
        """
        :param show: The show name to search for
        :param language: The language abbreviation to search for. E.g. "en"
        :param cache: If False, the local cache will not be used and the
            resources will be reloaded from server.
        :return: A :class:`Search()` instance
        :raise: :exc:`pytvdbapi.error.TVDBValueError`

        Searches the server for a show with the provided show name in the
        provided language. The language should be one of the supported
        language abbreviations or it could be set to *all* to search all
        languages. It will raise :class:`pytvdbapi.error.TVDBValueError` if
        an invalid language is provided.

        Searches are always cached within a session to make subsequent
        searches with the same parameters fast. If *cache*
        is set to True searches will also be cached across sessions,
        this is recommended to increase speed and to reduce the workload of
        the servers.

        Example::

            >>> from pytvdbapi import api
            >>> db = api.TVDB("B43FF87DE395DF56")
            >>> result = db.search("House", "en")

            >>> print(result[0])
            <Show - House>

            >>> for show in result:
            ...     print(show) # doctest: +ELLIPSIS
            <Show - House>
            ...
            <Show - House Of Cards (2013)>
            ...
        """

        logger.debug(u"Searching for {0} using language {1}".format(
            show, language))

        if language != u'all' and language not in __LANGUAGES__:
            raise error.TVDBValueError(
                u"{0} is not a valid language".format(language))

        if (show, language) not in self.search_buffer or not cache:
            context = {'series': quote(make_bytes(show)), "language": language}
            data = generate_tree(
                self.loader.load(__search__.format(**context), cache))
            shows = [
                Show(d, self, language, self.config)
                for d in parse_xml(data, "Series")
            ]

            self.search_buffer[(show, language)] = shows

        return Search(self.search_buffer[(show, language)], show, language)
コード例 #13
0
    def get_episode_by_air_date(self,
                                series_id,
                                language,
                                air_date,
                                cache=True):
        """
        .. versionadded:: 0.5

        :param series_id: The TVDB series id of the episode
        :param language: The language to search for. Should be a two letter abbreviation e.g. *en*.
        :param air_date: The air date to search for. Should be of type :class:`datetime.date`
        :type air_date: datetime.date
        :param cache: If False, the local cache will not be used and the
                    resources will be reloaded from server.

        :return: If found, an :class:`Episode` instance
        :raise: :exc:`pytvdbapi.error.TVDBValueError`


        .. Note:: When the :class:`Episode()` is loaded using :func:`get_episode_by_air_date`
            the *season* attribute used to link the episode with a season will be None.
        """
        if type(air_date) not in (datetime.date, ):
            raise error.TVDBValueError(
                "air_date should be of type datetime.date")
        elif language != 'all' and language not in __LANGUAGES__:
            raise error.TVDBValueError(
                u"{0} is not a valid language".format(language))

        context = {
            'seriesid': series_id,
            'airdate': air_date,
            "language": language,
            'mirror': self.mirrors.get_mirror(TypeMask.XML).url,
            'api_key': self.config['api_key']
        }

        url = airdate.format(**context)
        logger.debug(u'Getting episode from {0}'.format(url))

        data = self.loader.load(url, cache)

        data = generate_tree(data)

        # The xml has an "Error" element in it if no episode was found
        if has_element(data, 'Error'):
            raise error.TVDBNotFoundError(u"".format())

        episodes = parse_xml(data, "Episode")

        if len(episodes) == 0:
            raise error.BadData("Bad data received")
        else:
            return Episode(episodes[0], None, self.config)
コード例 #14
0
ファイル: api.py プロジェクト: alvra/pytvdbapi
    def search(self, show, language, cache=True):
        """
        :param show: The show name to search for
        :param language: The language abbreviation to search for. E.g. "en"
        :param cache: If False, the local cache will not be used and the
            resources will be reloaded from server.
        :return: A :class:`Search()` instance
        :raise: TVDBValueError

        Searches the server for a show with the provided show name in the
        provided language. The language should be one of the supported
        language abbreviations or it could be set to *all* to search all
        languages. It will raise :class:`TVDBValueError` if an invalid
        language is provided.

        Searches are always cached within a session to make subsequent
        searches with the same parameters really cheap and fast. If *cache*
        is set to True searches will also be cached across sessions,
        this is recommended to increase speed and to reduce the workload of
        the servers.

        Example::

            >>> from pytvdbapi import api
            >>> db = api.TVDB("B43FF87DE395DF56")
            >>> search = db.search("Dexter", "en")
            >>> for s in search:
            ...     print(s)
            ...
            <Show - Dexter>
            <Show - Cliff Dexter>

        """

        logger.debug("Searching for {0} using language {1}"
            .format(show, language))

        if language != 'all' and language not in self.languages:
            raise error.TVDBValueError(
                "{0} is not a valid language".format(language))

        if (show, language) not in self.search_buffer:
            if sys.version_info < (3, 0):
                show = str(show.encode('utf-8'))

            context = {'series': quote(show), "language": language}
            url = config.get("urls", "search", raw=True)
            data = generate_tree(self.loader.load(url % context, cache))
            shows = [Show(d, self, language)
                     for d in parse_xml(data, "Series")]

            self.search_buffer[(show, language)] = shows

        return Search(self.search_buffer[(show, language)], show, language)
コード例 #15
0
ファイル: api.py プロジェクト: greeeny101/Dreambox.bundle
    def get_series(self, series_id, language, cache=True):
        """
        .. versionadded:: 0.4

        :param series_id: The Show Id to fetch
        :param language: The language abbreviation to search for. E.g. "en"
        :param cache: If False, the local cache will not be used and the
                    resources will be reloaded from server.

        :return: A :class:`Show()` instance
        :raise: :exc:`pytvdbapi.error.TVDBValueError`, :exc:`pytvdbapi.error.TVDBIdError`

        Provided a valid Show ID, the data for the show is fetched and a
        corresponding :class:`Show()` object is returned.

        Example::

            >>> from pytvdbapi import api
            >>> db = api.TVDB("B43FF87DE395DF56")
            >>> show = db.get_series( 79349, "en" )  # Load Dexter
            >>> print(show.SeriesName)
            Dexter
        """

        logger.debug(u"Getting series with id {0} with language {1}".format(series_id, language))

        if language != 'all' and language not in __LANGUAGES__:
            raise error.TVDBValueError(u"{0} is not a valid language".format(language))

        context = {'seriesid': series_id, "language": language,
                   'mirror': self.mirrors.get_mirror(TypeMask.XML).url,
                   'api_key': self.config['api_key']}

        url = __series__.format(**context)
        logger.debug(u'Getting series from {0}'.format(url))

        try:
            data = self.loader.load(url, cache)
        except error.TVDBNotFoundError:
            raise error.TVDBIdError(u"Series id {0} not found".format(series_id))

        if data.strip():
            data = generate_tree(data)
        else:
            raise error.BadData("Bad data received")

        series = parse_xml(data, "Series")

        if len(series) == 0:
            raise error.BadData("Bad data received")
        else:
            return Show(series[0], self, language, self.config)
コード例 #16
0
ファイル: api.py プロジェクト: greeeny101/Dreambox.bundle
    def search(self, show, language, cache=True):
        """
        :param show: The show name to search for
        :param language: The language abbreviation to search for. E.g. "en"
        :param cache: If False, the local cache will not be used and the
            resources will be reloaded from server.
        :return: A :class:`Search()` instance
        :raise: :exc:`pytvdbapi.error.TVDBValueError`

        Searches the server for a show with the provided show name in the
        provided language. The language should be one of the supported
        language abbreviations or it could be set to *all* to search all
        languages. It will raise :class:`pytvdbapi.error.TVDBValueError` if
        an invalid language is provided.

        Searches are always cached within a session to make subsequent
        searches with the same parameters fast. If *cache*
        is set to True searches will also be cached across sessions,
        this is recommended to increase speed and to reduce the workload of
        the servers.

        Example::

            >>> from pytvdbapi import api
            >>> db = api.TVDB("B43FF87DE395DF56")
            >>> result = db.search("House", "en")

            >>> print(result[0])
            <Show - House>

            >>> for show in result:
            ...     print(show) # doctest: +ELLIPSIS
            <Show - House>
            ...
            <Show - House Of Cards (2013)>
            ...
        """

        logger.debug(u"Searching for {0} using language {1}".format(show, language))

        if language != u'all' and language not in __LANGUAGES__:
            raise error.TVDBValueError(u"{0} is not a valid language".format(language))

        if (show, language) not in self.search_buffer or not cache:
            context = {'series': quote(make_bytes(show)), "language": language}
            data = generate_tree(self.loader.load(__search__.format(**context), cache))
            shows = [Show(d, self, language, self.config) for d in parse_xml(data, "Series")]

            self.search_buffer[(show, language)] = shows

        return Search(self.search_buffer[(show, language)], show, language)
コード例 #17
0
    def _populate_data(self, data=None):
        """
        Populates the Show object with data. This will hit the network to
        download the XML data from `thetvdb.com <http://thetvdb.com>`_.
        :class:`Season` and `:class:Episode` objects will be created and
        added as needed.

        .. Note: This function is not intended to be used by clients of the
        API and should only be used internally by the Show class to manage its
        structure.
        """
        logger.debug(u"Populating season data from URL.")

        if data is None:
            context = {
                'mirror': self.api.mirrors.get_mirror(TypeMask.XML).url,
                'api_key': self.config['api_key'],
                'seriesid': self.id,
                'language': self.lang
            }
            url = series.format(**context)
            data = self.api.loader.load(url)
            data = generate_tree(data)

        episodes = [d for d in parse_xml(data, "Episode")]

        show_data = parse_xml(data, "Series")
        assert len(show_data) == 1, u"Should only have 1 Show section"

        self.data.update(
            InsensitiveDictionary(show_data[0], ignore_case=self.ignore_case))

        for episode_data in episodes:
            season_nr = int(episode_data['SeasonNumber'])
            if season_nr not in self.seasons:
                self.seasons[season_nr] = Season(season_nr, self)

            episode_instance = Episode(episode_data, self.seasons[season_nr],
                                       self.config)
            self.seasons[season_nr].append(episode_instance)

        # If requested, load the extra actors data
        if self.config.get('actors', False):
            self.load_actors()

        # if requested, load the extra banners data
        if self.config.get('banners', False):
            self.load_banners()
コード例 #18
0
ファイル: api.py プロジェクト: adamcw/pytvdbapi
    def _populate_data(self, full_data=None):
        """
        Populates the Show object with data. This will hit the network to
        download the XML data from `thetvdb.com <http://thetvdb.com>`_.
        :class:`Season` and `:class:Episode` objects will be created and
        added as needed.

        .. Note: This function is not intended to be used by clients of the
        API and should only be used internally by the Show class to manage its
        structure.
        """
        logger.debug(u"Populating season data from URL.")

        if self.full_data is None:
            context = {'mirror': self.api.mirrors.get_mirror(TypeMask.XML).url,
                        'api_key': self.config['api_key'],
                        'seriesid': self.id,
                        'language': self.lang}
            url = __series__.format(**context)
            data = self.api.loader.load(url)
            data = generate_tree(data)
        else:
            data = self.full_data

        episodes = [d for d in parse_xml(data, "Episode")]

        show_data = parse_xml(data, "Series")
        assert len(show_data) == 1, u"Should only have 1 Show section"

        self.data = merge(self.data, InsensitiveDictionary(show_data[0], ignore_case=self.ignore_case))

        for episode_data in episodes:
            season_nr = int(episode_data['SeasonNumber'])
            if not season_nr in self.seasons:
                self.seasons[season_nr] = Season(season_nr, self)

            episode = Episode(episode_data, self.seasons[season_nr], self.config)
            self.seasons[season_nr].append(episode)

        #If requested, load the extra actors data
        if self.config.get('actors', False):
            self.load_actors()

        #if requested, load the extra banners data
        if self.config.get('banners', False):
            self.load_banners()
コード例 #19
0
ファイル: api.py プロジェクト: fuzzycode/pytvdbapi
    def get_episode_by_air_date(self, series_id, language, air_date, cache=True):
        """
        .. versionadded:: 0.5

        :param series_id: The TVDB series id of the episode
        :param language: The language to search for. Should be a two letter abbreviation e.g. *en*.
        :param air_date: The air date to search for. Should be of type :class:`datetime.date`
        :type air_date: datetime.date
        :param cache: If False, the local cache will not be used and the
                    resources will be reloaded from server.

        :return: If found, an :class:`Episode` instance
        :raise: :exc:`pytvdbapi.error.TVDBValueError`


        .. Note:: When the :class:`Episode()` is loaded using :func:`get_episode_by_air_date`
            the *season* attribute used to link the episode with a season will be None.
        """
        if type(air_date) not in (datetime.date,):
            raise error.TVDBValueError("air_date should be of type datetime.date")
        elif language != 'all' and language not in __LANGUAGES__:
            raise error.TVDBValueError(u"{0} is not a valid language".format(language))

        context = {'seriesid': series_id, 'airdate': air_date, "language": language,
                   'mirror': self.mirrors.get_mirror(TypeMask.XML).url, 'api_key': self.config['api_key']}

        url = airdate.format(**context)
        logger.debug(u'Getting episode from {0}'.format(url))

        data = self.loader.load(url, cache)

        data = generate_tree(data)

        # The xml has an "Error" element in it if no episode was found
        if has_element(data, 'Error'):
            raise error.TVDBNotFoundError(u"".format())

        episodes = parse_xml(data, "Episode")

        if len(episodes) == 0:
            raise error.BadData("Bad data received")
        else:
            return Episode(episodes[0], None, self.config)
コード例 #20
0
ファイル: api.py プロジェクト: alvra/pytvdbapi
    def _populate_data(self):
        """
        Populates the Show object with data. This will hit the network to
        downlaod the XML data from `thetvdb.com <http://thetvdb.com>`_.
        :class:`Season` and `:class:Episode` objects will be created and
        added as needed.

        .. Note: This function is not intended to be used by clients of the
        API and should only be used internally by the Show class to manage its
        structure.
        """
        logger.debug("Populating season data from URL.")

        context = {'mirror': self.api.mirrors.get_mirror(TypeMask.XML).url,
                       'api_key': self.api.config['api_key'],
                       'seriesid': self.id,
                       'language': self.lang}

        url = config.get("urls", "series", raw=True) % context
        data = generate_tree(self.api.loader.load(url))
        episodes = [d for d in parse_xml(data, "Episode")]

        show_data = parse_xml(data, "Series")
        assert len(show_data) == 1, "Should only have 1 Show section"

        self.data = merge(self.data, show_data[0])

        for episode_data in episodes:
            season_nr = int(episode_data['SeasonNumber'])
            if not season_nr in self.seasons:
                self.seasons[season_nr] = Season(season_nr, self)

            episode = Episode(episode_data, self.seasons[season_nr])
            self.seasons[season_nr].append(episode)

        #If requested, load the extra actors data
        if self.api.config['actors']:
            self._load_actors()

        #if requested, load the extra banners data
        if self.api.config['banners']:
            self._load_banners()
コード例 #21
0
ファイル: api.py プロジェクト: toroettg/pytvdbapi
    def _load_banners(self):
        """
        Loads the extended Banner data from `thetvdb.com <http://thetvdb.com>`_
        and adds this to the banner_objects attribute.

        .. Note: This function is not intended to be used by clients of the
            API and should only be used internally by the Show class to
            manage its structure.
        """
        context = {'mirror': self.api.mirrors.get_mirror(TypeMask.XML).url,
                   'api_key': self.config['api_key'],
                   'seriesid': self.id}

        url = __banners__.format(**context)
        logger.debug('Loading Actors data from {0}'.format(url))

        data = generate_tree(self.api.loader.load(url))
        mirror = self.api.mirrors.get_mirror(TypeMask.BANNER).url

        self.banner_objects = [Banner(mirror, b, self) for b in parse_xml(data, "Banner")]
コード例 #22
0
ファイル: api.py プロジェクト: fuzzycode/pytvdbapi
    def __init__(self, api_key, **kwargs):
        self.config = dict()

        # cache old searches to avoid hitting the server
        self.search_buffer = dict()

        # extract all argument and store for later use
        self.config['api_key'] = api_key
        self.config['cache_dir'] = kwargs.get("cache_dir",
                                              make_unicode(os.path.join(tempfile.gettempdir(), __NAME__)))

        self.config['actors'] = kwargs.get('actors', False)
        self.config['banners'] = kwargs.get('banners', False)
        self.config['ignore_case'] = kwargs.get('ignore_case', False)

        # Create the loader object to use
        self.loader = Loader(self.config['cache_dir'], timeout=kwargs.get('timeout', None))

        # Create the list of available mirrors
        tree = generate_tree(self.loader.load(mirrors.format(**self.config)))
        self.mirrors = MirrorList(tree)
コード例 #23
0
ファイル: api.py プロジェクト: alvra/pytvdbapi
    def _load_actors(self):
        """
        Loads the extended Actor data from `thetvdb.com <http://thetvdb.com>`_
        and adds this to the actor_objects attribute.

        .. Note: This function is not intended to be used by clients of the
            API and should only be used internally by the Show class to
            manage its structure.
        """
        context = {'mirror': self.api.mirrors.get_mirror(TypeMask.XML).url,
                   'api_key': self.api.config['api_key'],
                   'seriesid': self.id}
        url = config.get("urls", "actors", raw=True) % context
        logger.debug('Loading Actors data from {0}'.format(url))

        data = generate_tree(self.api.loader.load(url))

        mirror = self.api.mirrors.get_mirror(TypeMask.BANNER).url

        #generate all the Actor objects
        self.actor_objects = [Actor(mirror, d, self)
                               for d in parse_xml(data, 'Actor')]
コード例 #24
0
    def __init__(self, api_key, **kwargs):
        self.config = dict()

        # cache old searches to avoid hitting the server
        self.search_buffer = dict()

        # extract all argument and store for later use
        self.config['api_key'] = api_key
        self.config['cache_dir'] = kwargs.get(
            "cache_dir",
            make_unicode(os.path.join(tempfile.gettempdir(), __NAME__)))

        self.config['actors'] = kwargs.get('actors', False)
        self.config['banners'] = kwargs.get('banners', False)
        self.config['ignore_case'] = kwargs.get('ignore_case', False)

        # Create the loader object to use
        self.loader = Loader(self.config['cache_dir'],
                             timeout=kwargs.get('timeout', None))

        # Create the list of available mirrors
        tree = generate_tree(self.loader.load(mirrors.format(**self.config)))
        self.mirrors = MirrorList(tree)
コード例 #25
0
ファイル: api.py プロジェクト: alvra/pytvdbapi
    def get_episode(self, episode_id, language, cache=True):
        logger.debug("Getting episode with id {0} with language {1}".format(
            episode_id, language))

        if language != 'all' and language not in self.languages:
            raise error.TVDBValueError("{0} is not a valid language".format(
                language))

        context = {'episodeid': episode_id, "language": language,
                   'mirror': self.mirrors.get_mirror(TypeMask.XML).url,
                   'api_key': self.config['api_key']}

        url = config.get("urls", "episode", raw=True) % context

        try:
            data = self.loader.load(url, cache)
        except error.ConnectionError as _error:
            logger.debug("Unable to connect to URL: {0}. {1}".format(url,
                _error))
            raise error.TVDBIdError("No Episode with id {0} found".format(
                episode_id))

        if data.strip():
            data = generate_tree(data)
        else:
            logger.debug("Empty data received for id {0}".format(episode_id))
            raise error.TVDBIdError("No Episode with id {0} found".format(
                episode_id))

        episodes = parse_xml(data, "Episode")
        assert len(episodes) <= 1, "Should not find more than one episodes"

        if len(episodes) >= 1:
            return Episode(episodes[0], None)
        else:
            raise error.TVDBIdError("No Episode with id {0} found".format(
                episode_id))
コード例 #26
0
ファイル: api.py プロジェクト: fuzzycode/pytvdbapi
    def get_series(self, series_id, language, id_type='tvdb', cache=True):
        """
        .. versionadded:: 0.4
        .. versionchanged:: 0.5 Added *id_type* parameter

        :param series_id: The Show Id to fetch
        :param language: The language abbreviation to search for. E.g. "en"
        :param id_type: Information about what kind of id is provided. Should be one of *('tvdb', 'imdb',
            'zap2it')*
        :param cache: If False, the local cache will not be used and the
                    resources will be reloaded from server.

        :return: A :class:`Show()` instance
        :raise: :exc:`pytvdbapi.error.TVDBValueError`, :exc:`pytvdbapi.error.TVDBIdError`

        Provided a valid Show ID, the data for the show is fetched and a
        corresponding :class:`Show()` object is returned.

        Example::

            >>> from pytvdbapi import api
            >>> db = api.TVDB("B43FF87DE395DF56")
            >>> show = db.get_series( 79349, "en" )  # Load Dexter
            >>> print(show.SeriesName)
            Dexter
        """
        if id_type not in ('tvdb', 'imdb', 'zap2it'):
            raise error.TVDBValueError("Invalid id type")
        elif language != 'all' and language not in __LANGUAGES__:
            raise error.TVDBValueError(u"{0} is not a valid language".format(language))

        # Map id type to url template
        __url__ = {'tvdb': series, 'imdb': imdbid, 'zap2it': zap2itid}

        try:
            series_id = text_type(series_id)
        except ValueError:
            raise error.TVDBValueError(
                "Invalid id type, expected {0} or {1}, got {2}".format(text_type, int_types, type(series_id)))

        if id_type == 'imdb':
            series_id = series_id[2:] if series_id.startswith('tt') else series_id
        elif id_type == 'zap2it':
            series_id = series_id if series_id.startswith('EP') else u'EP' + series_id.rjust(8, '0')

        logger.debug(
            u"Getting series with id {0}({2}) with language {1}".format(series_id, language, id_type))

        context = {'seriesid': series_id, "language": language,
                   'mirror': self.mirrors.get_mirror(TypeMask.XML).url,
                   'api_key': self.config['api_key'], 'imdbid': series_id, 'zap2itid': series_id}

        url = __url__[id_type].format(**context)
        logger.debug(u'Getting series from {0}'.format(url))

        try:
            data = self.loader.load(url, cache)
        except error.TVDBNotFoundError:
            raise error.TVDBIdError(u"Series id {0} not found".format(series_id))

        data = generate_tree(data)

        series_data = parse_xml(data, "Series")

        if len(series) == 0:
            raise error.BadData("Bad data received")
        else:
            return Show(series_data[0], self, language, self.config, data)
コード例 #27
0
ファイル: api.py プロジェクト: BenoitZugmeyer/pytvdbapi
    def get_episode(self, episode_id, language, cache=True):
        """
        .. versionadded:: 0.4

        :param episode_id: The Episode Id to fetch
        :param language: The language abbreviation to search for. E.g. "en"
        :param cache: If False, the local cache will not be used and the
                    resources will be reloaded from server.

        :return: An :class:`Episode()` instance
        :raise: :class:`pytvdbapi.error.TVDBIdError` if no episode is found with the given Id


        Given a valid episode Id the corresponding episode data is fetched and
        the :class:`Episode()` instance is returned.

        .. Note:: When the :class:`Episode()` is loaded using :func:`get_episode()`
            the episode attribute will be None.

        Example::

            >>> from pytvdbapi import api
            >>> db = api.TVDB("B43FF87DE395DF56")
            >>> episode = db.get_episode(308834, "en")
            >>> episode.id
            308834

            >>> episode.EpisodeName
            'Crocodile'

        """

        logger.debug("Getting episode with id {0} with language {1}".format(episode_id, language))

        if language != 'all' and language not in __LANGUAGES__:
            raise error.TVDBValueError("{0} is not a valid language".format(language))

        context = {'episodeid': episode_id, "language": language,
                   'mirror': self.mirrors.get_mirror(TypeMask.XML).url,
                   'api_key': self.config['api_key']}

        url = __episode__.format(**context)

        try:
            data = self.loader.load(url, cache)
        except error.TVDBNotFoundError:
            raise error.TVDBIdError("No Episode with id {0} found".format(episode_id))
        except error.ConnectionError as _error:
            logger.debug("Unable to connect to URL: {0}. {1}".format(url, _error))
            raise

        if data.strip():
            data = generate_tree(data)
        else:
            raise error.TVDBIdError("No Episode with id {0} found".format(episode_id))

        episodes = parse_xml(data, "Episode")
        assert len(episodes) <= 1, "Should not find more than one episodes"

        if len(episodes) >= 1:
            return Episode(episodes[0], None, self.config)
        else:
            raise error.TVDBIdError("No Episode with id {0} found".format(episode_id))
コード例 #28
0
    def setUp(self):
        super(TestMirror, self).setUp()

        data = BytesIO(
            open(os.path.join(self.path, "mirrors.xml"), 'rb').read())
        self.mirrors = mirror.MirrorList(xmlhelpers.generate_tree(data))
コード例 #29
0
ファイル: api.py プロジェクト: BenoitZugmeyer/pytvdbapi
    def get_episode(self, episode_id, language, cache=True):
        """
        .. versionadded:: 0.4

        :param episode_id: The Episode Id to fetch
        :param language: The language abbreviation to search for. E.g. "en"
        :param cache: If False, the local cache will not be used and the
                    resources will be reloaded from server.

        :return: An :class:`Episode()` instance
        :raise: :class:`pytvdbapi.error.TVDBIdError` if no episode is found with the given Id


        Given a valid episode Id the corresponding episode data is fetched and
        the :class:`Episode()` instance is returned.

        .. Note:: When the :class:`Episode()` is loaded using :func:`get_episode()`
            the episode attribute will be None.

        Example::

            >>> from pytvdbapi import api
            >>> db = api.TVDB("B43FF87DE395DF56")
            >>> episode = db.get_episode(308834, "en")
            >>> episode.id
            308834

            >>> episode.EpisodeName
            'Crocodile'

        """

        logger.debug("Getting episode with id {0} with language {1}".format(
            episode_id, language))

        if language != 'all' and language not in __LANGUAGES__:
            raise error.TVDBValueError(
                "{0} is not a valid language".format(language))

        context = {
            'episodeid': episode_id,
            "language": language,
            'mirror': self.mirrors.get_mirror(TypeMask.XML).url,
            'api_key': self.config['api_key']
        }

        url = __episode__.format(**context)

        try:
            data = self.loader.load(url, cache)
        except error.TVDBNotFoundError:
            raise error.TVDBIdError(
                "No Episode with id {0} found".format(episode_id))
        except error.ConnectionError as _error:
            logger.debug("Unable to connect to URL: {0}. {1}".format(
                url, _error))
            raise

        if data.strip():
            data = generate_tree(data)
        else:
            raise error.TVDBIdError(
                "No Episode with id {0} found".format(episode_id))

        episodes = parse_xml(data, "Episode")
        assert len(episodes) <= 1, "Should not find more than one episodes"

        if len(episodes) >= 1:
            return Episode(episodes[0], None, self.config)
        else:
            raise error.TVDBIdError(
                "No Episode with id {0} found".format(episode_id))
コード例 #30
0
ファイル: api.py プロジェクト: BenoitZugmeyer/pytvdbapi
    def get_series(self, series_id, language, cache=True):
        """
        .. versionadded:: 0.4

        :param series_id: The Show Id to fetch
        :param language: The language abbreviation to search for. E.g. "en"
        :param cache: If False, the local cache will not be used and the
                    resources will be reloaded from server.

        :return: A :class:`Show()` instance
        :raise: :class:`pytvdbapi.error.TVDBValueError`, :class:`pytvdbapi.error.TVDBIdError`

        Provided a valid Show ID, the data for the show is fetched and a
        corresponding :class:`Show()` object is returned.

        Example::

            >>> from pytvdbapi import api
            >>> db = api.TVDB("B43FF87DE395DF56")
            >>> show = db.get( 79349, "en" )
            >>> show.id
            79349

            >>> show.SeriesName
            'Dexter'

        """

        logger.debug("Getting series with id {0} with language {1}".format(
            series_id, language))

        if language != 'all' and language not in __LANGUAGES__:
            raise error.TVDBValueError(
                "{0} is not a valid language".format(language))

        context = {
            'seriesid': series_id,
            "language": language,
            'mirror': self.mirrors.get_mirror(TypeMask.XML).url,
            'api_key': self.config['api_key']
        }

        url = __series__.format(**context)
        try:
            data = self.loader.load(url, cache)
        except error.TVDBNotFoundError:
            raise error.TVDBIdError(
                "Series id {0} not found".format(series_id))
        except error.ConnectionError as _error:
            logger.debug("Unable to connect to URL: {0}. {1}".format(
                url, _error))
            raise

        if data.strip():
            data = generate_tree(data)
        else:
            raise error.TVDBIdError(
                "No Show with id {0} found".format(series_id))

        series = parse_xml(data, "Series")
        assert len(series) <= 1, "Should not find more than one series"

        if len(series) >= 1:
            return Show(series[0], self, language, self.config)
        else:
            raise error.TVDBIdError(
                "No Show with id {0} found".format(series_id))
コード例 #31
0
    def get_series(self, series_id, language, id_type='tvdb', cache=True):
        """
        .. versionadded:: 0.4
        .. versionchanged:: 0.5 Added *id_type* parameter

        :param series_id: The Show Id to fetch
        :param language: The language abbreviation to search for. E.g. "en"
        :param id_type: Information about what kind of id is provided. Should be one of *('tvdb', 'imdb',
            'zap2it')*
        :param cache: If False, the local cache will not be used and the
                    resources will be reloaded from server.

        :return: A :class:`Show()` instance
        :raise: :exc:`pytvdbapi.error.TVDBValueError`, :exc:`pytvdbapi.error.TVDBIdError`

        Provided a valid Show ID, the data for the show is fetched and a
        corresponding :class:`Show()` object is returned.

        Example::

            >>> from pytvdbapi import api
            >>> db = api.TVDB("B43FF87DE395DF56")
            >>> show = db.get_series( 79349, "en" )  # Load Dexter
            >>> print(show.SeriesName)
            Dexter
        """
        if id_type not in ('tvdb', 'imdb', 'zap2it'):
            raise error.TVDBValueError("Invalid id type")
        elif language != 'all' and language not in __LANGUAGES__:
            raise error.TVDBValueError(
                u"{0} is not a valid language".format(language))

        # Map id type to url template
        __url__ = {'tvdb': series, 'imdb': imdbid, 'zap2it': zap2itid}

        try:
            series_id = text_type(series_id)
        except ValueError:
            raise error.TVDBValueError(
                "Invalid id type, expected {0} or {1}, got {2}".format(
                    text_type, int_types, type(series_id)))

        if id_type == 'imdb':
            series_id = series_id[2:] if series_id.startswith(
                'tt') else series_id
        elif id_type == 'zap2it':
            series_id = series_id if series_id.startswith(
                'EP') else u'EP' + series_id.rjust(8, '0')

        logger.debug(
            u"Getting series with id {0}({2}) with language {1}".format(
                series_id, language, id_type))

        context = {
            'seriesid': series_id,
            "language": language,
            'mirror': self.mirrors.get_mirror(TypeMask.XML).url,
            'api_key': self.config['api_key'],
            'imdbid': series_id,
            'zap2itid': series_id
        }

        url = __url__[id_type].format(**context)
        logger.debug(u'Getting series from {0}'.format(url))

        try:
            data = self.loader.load(url, cache)
        except error.TVDBNotFoundError:
            raise error.TVDBIdError(
                u"Series id {0} not found".format(series_id))

        data = generate_tree(data)

        series_data = parse_xml(data, "Series")

        if len(series) == 0:
            raise error.BadData("Bad data received")
        else:
            return Show(series_data[0], self, language, self.config, data)
コード例 #32
0
    def get_episode(self, language, method="id", cache=True, **kwargs):
        """
        .. versionadded:: 0.4
        .. versionchanged:: 0.5 Added the possibility to get an episode using *default*, *dvd*, and *absolute*
            sort order

        :param episode_id: *Deprecated in 0.5* Use the *episodeid* keyword argument with the *id*
            method instead
        :param language: The language abbreviation to search for. E.g. "en"
        :param cache: If False, the local cache will not be used and the
                    resources will be reloaded from server.
        :param method: (default=id) Specify what method should be used to get the episode. Depending on
            what method is specified, different parameters must be passed as keyword arguments. Should be one
            of (id, default, dvd, absolute).
        :param kwargs: *episodeid*, *seriesid*, *seasonnumber*, *episodenumber* and *absolutenumber*. See
            the examples for information on how to use them.
        :return: An :class:`Episode()` instance
        :raise: :exc:`pytvdbapi.error.TVDBValueError`, :exc:`pytvdbapi.error.BadData`

        Retrieves a single episode. Depending on what method is specified different criteria can be used to
        retrieve the episode.

        Examples:

        Load an episode using the episode id

        >>> from pytvdbapi import api
        >>> db = api.TVDB("B43FF87DE395DF56")
        >>> ep = db.get_episode("en", episodeid=308834)  # id is the default method
        >>> print(ep.EpisodeName)
        Crocodile

        Load an episode using dvd and default sort order

        >>> ep = db.get_episode("en", "dvd", seasonnumber=2, episodenumber=5, seriesid=79349)
        >>> print(ep.EpisodeName)
        The Dark Defender

        >>> ep = db.get_episode("en", "default", seasonnumber=2, episodenumber=6, seriesid=79349)
        >>> print(ep.EpisodeName)
        Dex, Lies, and Videotape

        Load an episode using the absolute number

        >>> ep = db.get_episode("en", "absolute", absolutenumber=19, seriesid=79349)
        >>> print(ep.EpisodeName)
        That Night, A Forest Grew

        Under some circumstances the backend server fails to return a proper **404** file not found error
        response when the requested episode can not be found, but instead returns a valid HTML file with
        the content *404 file not found*. For this reason it is required to check for both
        :exc:`pytvdbapi.error.TVDBValueError` and :exc:`pytvdbapi.error.BadData` to detect an issue
        downloading the episode.

        >>> from pytvdbapi.error import BadData
        >>> from pytvdbapi.error import TVDBNotFoundError
        >>> try:
        ...    ep = db.get_episode("en", episodeid=308834)
        ... except TVDBNotFoundError:
        ...    # this is the standard 404 error code returned from the server
        ...    pass
        ... except BadData:
        ...     # This is when the server returns a 200 code but with a HTML page saying 404 Nothing found
        ...     pass

        .. Note:: When the :class:`Episode()` is loaded using :func:`get_episode()`
            the *season* attribute used to link the episode with a season will be None.
        """
        methods = {
            "default": default_order,
            "dvd": dvd_order,
            "absolute": absolute_order,
            "id": episode
        }

        if language != 'all' and language not in __LANGUAGES__:
            raise error.TVDBValueError(
                u"{0} is not a valid language".format(language))

        context = {
            "language": language,
            'mirror': self.mirrors.get_mirror(TypeMask.XML).url,
            'api_key': self.config['api_key']
        }

        kwargs.update(context)

        try:
            url = methods[method]
        except KeyError:
            raise error.TVDBValueError(
                u"{0} is not a valid get method".format(method))

        try:
            url = url.format(**kwargs)
        except KeyError:
            raise error.TVDBValueError(
                "Missing arguments for method {0}".format(method))

        logger.debug(u'Getting episode from {0}'.format(url))

        data = self.loader.load(url, cache)
        data = generate_tree(data)

        episodes = parse_xml(data, "Episode")

        if len(episodes) == 0:
            raise error.BadData("Bad data received")
        else:
            return Episode(episodes[0], None, self.config)
コード例 #33
0
ファイル: test_mirror.py プロジェクト: alvra/pytvdbapi
    def setUp(self):
        super(TestMirror, self).setUp()

        data = utils.file_loader(os.path.join(self.path, "mirrors.xml"))
        self.mirrors = mirror.MirrorList(xmlhelpers.generate_tree(data))
コード例 #34
0
ファイル: api.py プロジェクト: thefkboss/Dreambox.bundle
    def get_episode(self, episode_id, language, cache=True):
        """
        .. versionadded:: 0.4

        :param episode_id: The Episode Id to fetch
        :param language: The language abbreviation to search for. E.g. "en"
        :param cache: If False, the local cache will not be used and the
                    resources will be reloaded from server.

        :return: An :class:`Episode()` instance
        :raise: :exc:`pytvdbapi.error.TVDBIdError` if no episode is found with the given Id


        Given a valid episode Id the corresponding episode data is fetched and
        the :class:`Episode()` instance is returned.

        Example::

            >>> from pytvdbapi import api
            >>> db = api.TVDB("B43FF87DE395DF56")
            >>> episode = db.get_episode(308834, "en") # Load an episode of dexter
            >>> print(episode.id)
            308834

            >>> print(episode.EpisodeName)
            Crocodile

        .. Note:: When the :class:`Episode()` is loaded using :func:`get_episode()`
            the *season* attribute used to link the episode with a season will be None.
        """

        logger.debug(u"Getting episode with id {0} with language {1}".format(
            episode_id, language))

        if language != 'all' and language not in __LANGUAGES__:
            raise error.TVDBValueError(
                u"{0} is not a valid language".format(language))

        context = {
            'episodeid': episode_id,
            "language": language,
            'mirror': self.mirrors.get_mirror(TypeMask.XML).url,
            'api_key': self.config['api_key']
        }

        url = __episode__.format(**context)
        logger.debug(u'Getting episode from {0}'.format(url))

        try:
            data = self.loader.load(url, cache)
        except error.TVDBNotFoundError:
            raise error.TVDBIdError(
                u"No Episode with id {0} found".format(episode_id))

        if data.strip():
            data = generate_tree(data)
        else:
            raise error.BadData("Bad data received")

        episodes = parse_xml(data, "Episode")

        if len(episodes) == 0:
            raise error.BadData("Bad data received")
        else:
            return Episode(episodes[0], None, self.config)
コード例 #35
0
ファイル: api.py プロジェクト: fuzzycode/pytvdbapi
    def get_episode(self, language, method="id", cache=True, **kwargs):
        """
        .. versionadded:: 0.4
        .. versionchanged:: 0.5 Added the possibility to get an episode using *default*, *dvd*, and *absolute*
            sort order

        :param episode_id: *Deprecated in 0.5* Use the *episodeid* keyword argument with the *id*
            method instead
        :param language: The language abbreviation to search for. E.g. "en"
        :param cache: If False, the local cache will not be used and the
                    resources will be reloaded from server.
        :param method: (default=id) Specify what method should be used to get the episode. Depending on
            what method is specified, different parameters must be passed as keyword arguments. Should be one
            of (id, default, dvd, absolute).
        :param kwargs: *episodeid*, *seriesid*, *seasonnumber*, *episodenumber* and *absolutenumber*. See
            the examples for information on how to use them.
        :return: An :class:`Episode()` instance
        :raise: :exc:`pytvdbapi.error.TVDBValueError`, :exc:`pytvdbapi.error.BadData`

        Retrieves a single episode. Depending on what method is specified different criteria can be used to
        retrieve the episode.

        Examples:

        Load an episode using the episode id

        >>> from pytvdbapi import api
        >>> db = api.TVDB("B43FF87DE395DF56")
        >>> ep = db.get_episode("en", episodeid=308834)  # id is the default method
        >>> print(ep.EpisodeName)
        Crocodile

        Load an episode using dvd and default sort order

        >>> ep = db.get_episode("en", "dvd", seasonnumber=2, episodenumber=5, seriesid=79349)
        >>> print(ep.EpisodeName)
        The Dark Defender

        >>> ep = db.get_episode("en", "default", seasonnumber=2, episodenumber=6, seriesid=79349)
        >>> print(ep.EpisodeName)
        Dex, Lies, and Videotape

        Load an episode using the absolute number

        >>> ep = db.get_episode("en", "absolute", absolutenumber=19, seriesid=79349)
        >>> print(ep.EpisodeName)
        That Night, A Forest Grew

        Under some circumstances the backend server fails to return a proper **404** file not found error
        response when the requested episode can not be found, but instead returns a valid HTML file with
        the content *404 file not found*. For this reason it is required to check for both
        :exc:`pytvdbapi.error.TVDBValueError` and :exc:`pytvdbapi.error.BadData` to detect an issue
        downloading the episode.

        >>> from pytvdbapi.error import BadData
        >>> from pytvdbapi.error import TVDBNotFoundError
        >>> try:
        ...    ep = db.get_episode("en", episodeid=308834)
        ... except TVDBNotFoundError:
        ...    # this is the standard 404 error code returned from the server
        ...    pass
        ... except BadData:
        ...     # This is when the server returns a 200 code but with a HTML page saying 404 Nothing found
        ...     pass

        .. Note:: When the :class:`Episode()` is loaded using :func:`get_episode()`
            the *season* attribute used to link the episode with a season will be None.
        """
        methods = {"default": default_order, "dvd": dvd_order, "absolute": absolute_order, "id": episode}

        if language != 'all' and language not in __LANGUAGES__:
            raise error.TVDBValueError(u"{0} is not a valid language".format(language))

        context = {"language": language,
                   'mirror': self.mirrors.get_mirror(TypeMask.XML).url,
                   'api_key': self.config['api_key']}

        kwargs.update(context)

        try:
            url = methods[method]
        except KeyError:
            raise error.TVDBValueError(u"{0} is not a valid get method".format(method))

        try:
            url = url.format(**kwargs)
        except KeyError:
            raise error.TVDBValueError("Missing arguments for method {0}".format(method))

        logger.debug(u'Getting episode from {0}'.format(url))

        data = self.loader.load(url, cache)
        data = generate_tree(data)

        episodes = parse_xml(data, "Episode")

        if len(episodes) == 0:
            raise error.BadData("Bad data received")
        else:
            return Episode(episodes[0], None, self.config)
コード例 #36
0
ファイル: test_mirror.py プロジェクト: jepugs/pytvdbapi
    def setUp(self):
        super(TestMirror, self).setUp()

        data = BytesIO(open(os.path.join(self.path, "mirrors.xml"), "rb").read())
        self.mirrors = mirror.MirrorList(xmlhelpers.generate_tree(data))
コード例 #37
0
    def setUp(self):
        super(TestMirror, self).setUp()

        data = utils.file_loader(os.path.join(self.path, "mirrors.xml"))
        self.mirrors = mirror.MirrorList(xmlhelpers.generate_tree(data))