Top

tvdbsimple.languages module

This module implements the Language functionality of TheTVDb API. Allows to retrieve the languages list and info.

See Languages API section

# -*- coding: utf-8 -*-

"""
This module implements the Language functionality of TheTVDb API.
Allows to retrieve the languages list and info.

See [Languages API section](https://api.thetvdb.com/swagger#!/Languages)
"""
import sys
if sys.version_info > (2, 8):
    from builtins import dict
from .base import TVDB

class Languages(TVDB):
    """
    Languages class to retrieve the languages list and info.
    """
    _BASE_PATH = 'languages'
    _URLS = {
        'all': '',
        'language': '/{lid}'
    }
    LANGUAGES = {}
    _ALL_PARSED = False

    def all(self):
        """
        Get the full languages list and set it to all attribute.

        Returns a list of languages with their info.

        For example

            #!python
            >>> import tvdbsimple as tvdb
            >>> tvdb.KEYS.API_KEY = 'YOUR_API_KEY'
            >>> lng = tvdb.Languages()
            >>> response = lng.all()
            >>> lng.all[0]['englishName']
            'Chinese'

        """
        if self._ALL_PARSED:
            return self.LANGUAGES.values()
            
        path = self._get_path('all')
        
        response = self._GET(path)
        self._set_attrs_to_values({'all': response})
        for lang in response:
            if 'id' in lang:
                self.LANGUAGES[lang['id']] = lang
        self._ALL_PARSED = True
        return response

    def language(self, id):
        """
        Get the language info for a specific language id.
        
        `id` is the specific language id to retrieve.

        Returns a dict rwith all the language info.

        For example

            #!python
            >>> import tvdbsimple as tvdb
            >>> tvdb.KEYS.API_KEY = 'YOUR_API_KEY'
            >>> lng = tvdb.Languages()
            >>> response = lng.language(7)
            >>> response['englishName']
            'English'

        """
        if id in self.LANGUAGES:
            return self.LANGUAGES[id]

        path = self._get_path('language').format(lid=id)
        
        response = self._GET(path)
        if 'id' in response:
            self.LANGUAGES[response['id']] = response
        return response

    def __iter__(self):
        for i in self.all():
            yield i

    def __getitem__(self, id):
        return self.language(id)

    def __setitem__(self):
        raise Exception('Function Disabled')
        
    def __delitem__(self):
        raise Exception('Function Disabled')

Classes

class Languages

Languages class to retrieve the languages list and info.

class Languages(TVDB):
    """
    Languages class to retrieve the languages list and info.
    """
    _BASE_PATH = 'languages'
    _URLS = {
        'all': '',
        'language': '/{lid}'
    }
    LANGUAGES = {}
    _ALL_PARSED = False

    def all(self):
        """
        Get the full languages list and set it to all attribute.

        Returns a list of languages with their info.

        For example

            #!python
            >>> import tvdbsimple as tvdb
            >>> tvdb.KEYS.API_KEY = 'YOUR_API_KEY'
            >>> lng = tvdb.Languages()
            >>> response = lng.all()
            >>> lng.all[0]['englishName']
            'Chinese'

        """
        if self._ALL_PARSED:
            return self.LANGUAGES.values()
            
        path = self._get_path('all')
        
        response = self._GET(path)
        self._set_attrs_to_values({'all': response})
        for lang in response:
            if 'id' in lang:
                self.LANGUAGES[lang['id']] = lang
        self._ALL_PARSED = True
        return response

    def language(self, id):
        """
        Get the language info for a specific language id.
        
        `id` is the specific language id to retrieve.

        Returns a dict rwith all the language info.

        For example

            #!python
            >>> import tvdbsimple as tvdb
            >>> tvdb.KEYS.API_KEY = 'YOUR_API_KEY'
            >>> lng = tvdb.Languages()
            >>> response = lng.language(7)
            >>> response['englishName']
            'English'

        """
        if id in self.LANGUAGES:
            return self.LANGUAGES[id]

        path = self._get_path('language').format(lid=id)
        
        response = self._GET(path)
        if 'id' in response:
            self.LANGUAGES[response['id']] = response
        return response

    def __iter__(self):
        for i in self.all():
            yield i

    def __getitem__(self, id):
        return self.language(id)

    def __setitem__(self):
        raise Exception('Function Disabled')
        
    def __delitem__(self):
        raise Exception('Function Disabled')

Ancestors (in MRO)

  • Languages
  • tvdbsimple.base.TVDB
  • builtins.object

Class variables

var LANGUAGES

Static methods

def __init__(

self, id=0, user=None, key=None)

Initialize the base class.

You can provide id that is the item id used for url creation. You can also provide user, that is the username for login. You can also provide key, that is the userkey needed to authenticate with the user, you can find it in the account info under account identifier., the language id you want to use to retrieve the info.

def __init__(self, id=0, user=None, key=None):
    """
    Initialize the base class.
    
    You can provide `id` that is the item id used for url creation. You can also 
    provide `user`, that is the username for login. 
    You can also provide `key`, that is the userkey needed to 
    authenticate with the user, you can find it in the 
    [account info](http://thetvdb.com/?tab=userinfo) under account identifier., 
    the language id you want to use to retrieve the info.
    """
    self._ID = id
    self.USER = user
    """Stores username if available"""
    self.USER_KEY = key
    """Stores user-key if available"""

def all(

self)

Get the full languages list and set it to all attribute.

Returns a list of languages with their info.

For example

#!python
>>> import tvdbsimple as tvdb
>>> tvdb.KEYS.API_KEY = 'YOUR_API_KEY'
>>> lng = tvdb.Languages()
>>> response = lng.all()
>>> lng.all[0]['englishName']
'Chinese'
def all(self):
    """
    Get the full languages list and set it to all attribute.
    Returns a list of languages with their info.
    For example
        #!python
        >>> import tvdbsimple as tvdb
        >>> tvdb.KEYS.API_KEY = 'YOUR_API_KEY'
        >>> lng = tvdb.Languages()
        >>> response = lng.all()
        >>> lng.all[0]['englishName']
        'Chinese'
    """
    if self._ALL_PARSED:
        return self.LANGUAGES.values()
        
    path = self._get_path('all')
    
    response = self._GET(path)
    self._set_attrs_to_values({'all': response})
    for lang in response:
        if 'id' in lang:
            self.LANGUAGES[lang['id']] = lang
    self._ALL_PARSED = True
    return response

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 language(

self, id)

Get the language info for a specific language id.

id is the specific language id to retrieve.

Returns a dict rwith all the language info.

For example

#!python
>>> import tvdbsimple as tvdb
>>> tvdb.KEYS.API_KEY = 'YOUR_API_KEY'
>>> lng = tvdb.Languages()
>>> response = lng.language(7)
>>> response['englishName']
'English'
def language(self, id):
    """
    Get the language info for a specific language id.
    
    `id` is the specific language id to retrieve.
    Returns a dict rwith all the language info.
    For example
        #!python
        >>> import tvdbsimple as tvdb
        >>> tvdb.KEYS.API_KEY = 'YOUR_API_KEY'
        >>> lng = tvdb.Languages()
        >>> response = lng.language(7)
        >>> response['englishName']
        'English'
    """
    if id in self.LANGUAGES:
        return self.LANGUAGES[id]
    path = self._get_path('language').format(lid=id)
    
    response = self._GET(path)
    if 'id' in response:
        self.LANGUAGES[response['id']] = 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 ''