fix tvdbapi

This commit is contained in:
jxxghp
2025-01-07 14:36:37 +08:00
parent 23650657cd
commit 433712aa80
+19 -68
View File
@@ -1,4 +1,3 @@
""" """
Simple-to-use Python interface to The TVDB's API (thetvdb.com) Simple-to-use Python interface to The TVDB's API (thetvdb.com)
""" """
@@ -6,19 +5,20 @@
__author__ = "dbr/Ben" __author__ = "dbr/Ben"
__version__ = "3.1.0" __version__ = "3.1.0"
import sys import getpass
import hashlib
import logging
import os import os
import sys
import tempfile
import time import time
import types import types
import getpass
import tempfile
import warnings import warnings
import logging from typing import Optional, Union
import hashlib
import requests import requests
import requests_cache import requests_cache
from requests_cache.backends.base import _to_bytes, _DEFAULT_HEADERS from requests_cache.backends.base import _to_bytes, _DEFAULT_HEADERS # noqa
IS_PY2 = sys.version_info[0] == 2 IS_PY2 = sys.version_info[0] == 2
@@ -176,7 +176,8 @@ class ConsoleUI(BaseUI):
"""Interactively allows the user to select a show from a console based UI """Interactively allows the user to select a show from a console based UI
""" """
def _displaySeries(self, allSeries, limit=6): @staticmethod
def _displaySeries(allSeries, limit: Optional[int] = 6):
"""Helper function, lists series with corresponding ID """Helper function, lists series with corresponding ID
""" """
if limit is not None: if limit is not None:
@@ -267,6 +268,7 @@ class ShowContainer(dict):
""" """
def __init__(self): def __init__(self):
super().__init__()
self._stack = [] self._stack = []
self._lastgc = time.time() self._lastgc = time.time()
@@ -336,42 +338,6 @@ class Show(dict):
Search terms are converted to lower case (unicode) strings. Search terms are converted to lower case (unicode) strings.
# Examples
These examples assume t is an instance of Tvdb():
>>> t = Tvdb()
>>>
To search for all episodes of Scrubs with a bit of data
containing "my first day":
>>> t['Scrubs'].search("my first day")
[<Episode 01x01 - u'My First Day'>]
>>>
Search for "My Name Is Earl" episode named "Faked His Own Death":
>>> t['My Name Is Earl'].search('Faked My Own Death', key='episodeName')
[<Episode 01x04 - u'Faked My Own Death'>]
>>>
To search Scrubs for all episodes with "mentor" in the episode name:
>>> t['scrubs'].search('mentor', key='episodeName')
[<Episode 01x02 - u'My Mentor'>, <Episode 03x15 - u'My Tormented Mentor'>]
>>>
# Using search results
>>> results = t['Scrubs'].search("my first")
>>> print results[0]['episodeName']
My First Day
>>> for x in results: print x['episodeName']
My First Day
My First Step
My First Kill
>>>
""" """
results = [] results = []
for cur_season in self.values(): for cur_season in self.values():
@@ -386,6 +352,7 @@ class Season(dict):
def __init__(self, show=None): def __init__(self, show=None):
"""The show attribute points to the parent show """The show attribute points to the parent show
""" """
super().__init__()
self.show = show self.show = show
def __repr__(self): def __repr__(self):
@@ -420,6 +387,7 @@ class Episode(dict):
def __init__(self, season=None): def __init__(self, season=None):
"""The season attribute points to the parent season """The season attribute points to the parent season
""" """
super().__init__()
self.season = season self.season = season
def __repr__(self): def __repr__(self):
@@ -540,7 +508,7 @@ class Tvdb:
self, self,
interactive=False, interactive=False,
select_first=False, select_first=False,
cache=True, cache: Union[str, bool, requests.Session] = True,
banners=False, banners=False,
actors=False, actors=False,
custom_ui=None, custom_ui=None,
@@ -690,7 +658,7 @@ class Tvdb:
LOG.debug("Using specified requests.Session") LOG.debug("Using specified requests.Session")
self.session = cache self.session = cache
try: try:
self.session.get self.session.get # noqa
except AttributeError: except AttributeError:
raise ValueError( raise ValueError(
( (
@@ -776,7 +744,7 @@ class Tvdb:
cache_key = self.session.cache.create_key( cache_key = self.session.cache.create_key(
fake_session_for_key.prepare_request(requests.Request('GET', url)) fake_session_for_key.prepare_request(requests.Request('GET', url))
) )
except Exception: except Exception: # noqa
# FIXME: Can this just check for hasattr(self.session, "cache") instead? # FIXME: Can this just check for hasattr(self.session, "cache") instead?
pass pass
@@ -956,6 +924,7 @@ class Tvdb:
banners_resp = self._getetsrc(self.config['url_seriesBanner'] % sid) banners_resp = self._getetsrc(self.config['url_seriesBanner'] % sid)
banners = {} banners = {}
for cur_banner in banners_resp.keys(): for cur_banner in banners_resp.keys():
btype = None
banners_info = self._getetsrc(self.config['url_seriesBannerInfo'] % (sid, cur_banner)) banners_info = self._getetsrc(self.config['url_seriesBannerInfo'] % (sid, cur_banner))
for banner_info in banners_info: for banner_info in banners_info:
bid = banner_info.get('id') bid = banner_info.get('id')
@@ -981,32 +950,14 @@ class Tvdb:
LOG.debug("Transforming %s to %s" % (k, new_key)) LOG.debug("Transforming %s to %s" % (k, new_key))
new_url = self.config['url_artworkPrefix'] % v new_url = self.config['url_artworkPrefix'] % v
banners[btype][btype2][bid][new_key] = new_url banners[btype][btype2][bid][new_key] = new_url
if btype:
banners[btype]['raw'] = banners_info banners[btype]['raw'] = banners_info
self._setShowData(sid, "_banners", banners) self._setShowData(sid, "_banners", banners)
def _parseActors(self, sid): def _parseActors(self, sid):
"""Parsers actors XML, from """Parsers actors XML, from
http://thetvdb.com/api/[APIKEY]/series/[SERIES ID]/actors.xml http://thetvdb.com/api/[APIKEY]/series/[SERIES ID]/actors.xml
Actors are retrieved using t['show name]['_actors'], for example:
>>> t = Tvdb(actors = True)
>>> actors = t['scrubs']['_actors']
>>> type(actors)
<class 'tvdb_api.Actors'>
>>> type(actors[0])
<class 'tvdb_api.Actor'>
>>> actors[0]
<Actor u'John C. McGinley'>
>>> sorted(actors[0].keys())
[u'id', u'image', u'imageAdded', u'imageAuthor', u'lastUpdated', u'name', u'role',
u'seriesId', u'sortOrder']
>>> actors[0]['name']
u'John C. McGinley'
>>> actors[0]['image']
u'http://thetvdb.com/banners/actors/43638.jpg'
Any key starting with an underscore has been processed (not the raw Any key starting with an underscore has been processed (not the raw
data from the XML) data from the XML)
""" """