tvdbsimple.episode module
This module implements the Episode functionality of TheTVDb API. Allows to retrieve episode detailed info.
# -*- coding: utf-8 -*-
"""
This module implements the Episode functionality of TheTVDb API.
Allows to retrieve episode detailed info.
See [Episodes API section](https://api.thetvdb.com/swagger#!/Episodes)
"""
from .base import TVDB
class Episode(TVDB):
"""
Episode class to retrieve detailed info about an episode.
Requires the episode id.
"""
_BASE_PATH = 'episodes'
_URLS = {
'info': '/{id}'
}
def __init__(self, id, language=''):
"""
Initialize the episode class.
`id` is the TheTVDb episode id. You can also provide `language`,
the language id you want to use to retrieve the info.
"""
super(Episode, self).__init__(id)
self._set_language(language)
def info(self, language=''):
"""
Get the episode information of the episode and set its values to the local attributes.
You can set `language` with the language id to retrieve info in that specific language.
It returns a dictionary with all the episode info.
For example
#!python
>>> import tvdbsimple as tvdb
>>> tvdb.KEYS.API_KEY = 'YOUR_API_KEY'
>>> ep = tvdb.Episode(5330530)
>>> ep.imdbId
'tt4701544'
"""
path = self._get_id_path('info')
self._set_language(language)
response = self._GET(path)
self._set_attrs_to_values(response)
return response
Classes
class Episode
Episode class to retrieve detailed info about an episode. Requires the episode id.
class Episode(TVDB):
"""
Episode class to retrieve detailed info about an episode.
Requires the episode id.
"""
_BASE_PATH = 'episodes'
_URLS = {
'info': '/{id}'
}
def __init__(self, id, language=''):
"""
Initialize the episode class.
`id` is the TheTVDb episode id. You can also provide `language`,
the language id you want to use to retrieve the info.
"""
super(Episode, self).__init__(id)
self._set_language(language)
def info(self, language=''):
"""
Get the episode information of the episode and set its values to the local attributes.
You can set `language` with the language id to retrieve info in that specific language.
It returns a dictionary with all the episode info.
For example
#!python
>>> import tvdbsimple as tvdb
>>> tvdb.KEYS.API_KEY = 'YOUR_API_KEY'
>>> ep = tvdb.Episode(5330530)
>>> ep.imdbId
'tt4701544'
"""
path = self._get_id_path('info')
self._set_language(language)
response = self._GET(path)
self._set_attrs_to_values(response)
return response
Ancestors (in MRO)
- Episode
- tvdbsimple.base.TVDB
- builtins.object
Static methods
def __init__(
self, id, language='')
Initialize the episode class.
id
is the TheTVDb episode id. You can also provide language
,
the language id you want to use to retrieve the info.
def __init__(self, id, language=''):
"""
Initialize the episode class.
`id` is the TheTVDb episode id. You can also provide `language`,
the language id you want to use to retrieve the info.
"""
super(Episode, self).__init__(id)
self._set_language(language)
def get_token(
self, forceNew=False)
Get the existing token or creates it if it doesn't exist. Returns the API token.
If forceNew
is true the function will do a new login to retrieve the token.
def get_token(self, forceNew=False):
"""
Get the existing token or creates it if it doesn't exist.
Returns the API token.
If `forceNew` is true the function will do a new login to retrieve the token.
"""
from . import KEYS
if not KEYS.API_TOKEN or forceNew:
if not KEYS.API_KEY:
raise APIKeyError
if hasattr(self,"USER") and hasattr(self,"USER_KEY"):
data = {"apikey": KEYS.API_KEY, "username": self.USER, "userkey": self.USER_KEY}
else:
data={"apikey": KEYS.API_KEY}
response = requests.request(
'POST', self._get_complete_url('login'),
data=json.dumps(data),
headers=self._headers)
if response.status_code == 200:
KEYS.API_TOKEN = response.json()['token']
else:
error = "Unknown error while authenticating. Check your api key or your user/userkey"
try:
error = response.json()['error']
except:
pass
raise AuthenticationError(error)
return KEYS.API_TOKEN
def info(
self, language='')
Get the episode information of the episode and set its values to the local attributes.
You can set language
with the language id to retrieve info in that specific language.
It returns a dictionary with all the episode info.
For example
#!python
>>> import tvdbsimple as tvdb
>>> tvdb.KEYS.API_KEY = 'YOUR_API_KEY'
>>> ep = tvdb.Episode(5330530)
>>> ep.imdbId
'tt4701544'
def info(self, language=''):
"""
Get the episode information of the episode and set its values to the local attributes.
You can set `language` with the language id to retrieve info in that specific language.
It returns a dictionary with all the episode info.
For example
#!python
>>> import tvdbsimple as tvdb
>>> tvdb.KEYS.API_KEY = 'YOUR_API_KEY'
>>> ep = tvdb.Episode(5330530)
>>> ep.imdbId
'tt4701544'
"""
path = self._get_id_path('info')
self._set_language(language)
response = self._GET(path)
self._set_attrs_to_values(response)
return response
def refresh_token(
self)
Refresh the current token set in the module.
Returns the new obtained valid token for the API.
def refresh_token(self):
"""
Refresh the current token set in the module.
Returns the new obtained valid token for the API.
"""
self._set_token_header()
response = requests.request(
'GET', self._get_complete_url('refresh_token'),
headers=self._headers)
response.raise_for_status()
jsn = response.json()
if 'token' in jsn:
from . import KEYS
KEYS.API_TOKEN = jsn['token']
return KEYS.API_TOKEN
return ''