Example #1
0
def get_tvdb_api( ) -> dict:
    """
    Returns the :py:class:`dictionary <dict>` of TVDB_ API credentials (see  :ref:`TVDB API configuation <The Television Database (TVDB) API>`), taken from the ``tvdb`` configuration service in the ``plexconfig`` table. The form of the dictionary is,

    .. code-block:: python

       {
         'username' : USERNAME, # the TVDB API user name
         'apikey'   : APIKEY,   # the TVDB API key
         'userkey'  : USERKEY   # THE TVDB API user key
       }

    :returns: the :py:class:`dictionary <dict>` of TVDB_ API credentials.
    :rtype: dict
    :raises: ValuerError: if the TVDB_ API credentials are not found in the ``plexconfig`` table.
    
    """
    query = session.query( PlexConfig ).filter( PlexConfig.service == 'tvdb' )
    val = query.first( )
    if val is None:
        raise ValueError("ERROR, NO TVDB API CREDENTIALS FOUND")
    data = val.data
    return { 'username' : data['username'],
             'apikey' : data['apikey'],
             'userkey' : data['userkey'] }
Example #2
0
def save_tvdb_api( username: str, apikey: str, userkey: str, verify = True ):
    """
    Saves the information on the TVDB_ API access into the ``tvdb`` configuration service in the ``plexconfig`` table. Details of how to set up the configuration in :ref:`TVDB API configuation <The Television Database (TVDB) API>`, which uses ``plex_config_gui.py``.

    :param str username: the TVDB_ API username.
    :param str apikey: the TVDB_ API key.
    :param str userkey: the TVDB_ API user key.
    :param bool verify: optional argument, whether to verify SSL connections. Default is ``True``.

    :returns: a satus :py:class:`string <str>`. If successful, returns ``"SUCCESS"``. If unsuccessful, returns ``"FAILURE, COULD NOT SAVE TVDB API STUFF"``.
    :rtype: str
    """
    #
    ## first check if works
    isValid = check_tvdb_api( username, apikey, userkey, verify = verify )
    if not isValid: return 'FAILURE, COULD NOT SAVE TVDB API STUFF'
    query = session.query( PlexConfig ).filter( PlexConfig.service == 'tvdb' )
    val = query.first( )
    if val is not None:
        session.delete( val )
        session.commit( )
    newval = PlexConfig( service = 'tvdb',
                         data = {
                             'apikey' : apikey,
                             'username' : username,
                             'userkey' : userkey } )
    session.add( newval )
    session.commit( )
    return 'SUCCESS'
Example #3
0
def get_credentials():
    """
    Returns the rsync'ing setup from the SQLite3_ configuration database as a :py:class:`dict` in the following form.

    .. code-block:: python

      { 'local_dir' : XXXX,
        'sshpath' : YYYY@ZZZZ,
        'password' : AAAA,
        'subdir' : BBBB }

    :returns: the :py:class:`dict` of the rsync'ing setup if in the SQLite3_ configuration database, otherwise ``None``.

    .. seealso::
    
      * :py:meth:`check_credentials <plexcore.plexcore_rsync.check_credentials>`.
      * :py:meth:`push_credentials <plexcore.plexcore_rsync.push_credentials>`.
    """
    val = session.query(PlexConfig).filter(
        PlexConfig.service == 'rsync').first()
    if val is None:
        logging.debug('ERROR, RSYNC configuration does not exist.')
        return None
    data = val.data
    local_dir = data['localdir']
    sshpath = data['sshpath']
    subdir = data['subdir']
    datan = {'local_dir': local_dir, 'sshpath': sshpath}
    if 'password' not in data:
        datan['password'] = None
    else:
        datan['password'] = data['password']
    if subdir is None: datan['subdir'] = None
    else: datan['subdir'] = subdir.strip()
    return datan
Example #4
0
def push_credentials(local_dir, sshpath, password, subdir=None):
    """
    Push the rsync'ing setup (``local_dir``, ``sshpath``, ``password``, and ``subdir``), if working, into the SQLite3_ configuration database.
    
    :param str local_dir: the local directory, on the Plex_ server, into which to download files from the remote SSH server.
    :param str sshpath: the full path with username and host name for the SSH server. Format is ``'username@hostname'``.
    :param str password: the password to connect as the SSH server.
    :param str subdir: if not ``None``, the subdirectory on the remote SSH server from which to download files.
    
    :returns: if successful, return ``'SUCCESS'``. If not, return error messages.
    
    .. seealso::
    
      * :py:meth:`check_credentials <plexcore.plexcore_rsync.check_credentials>`.
      * :py:meth:`get_credentials <plexcore.plexcore_rsync.get_credentials>`.
    """

    #
    ## validation to see if the data is valid
    #
    ## first, does local directory exist?
    status = check_credentials(local_dir, sshpath, password, subdir=subdir)
    if status != 'SUCCESS':
        return "ERROR, COULD NOT SET RSYNC SSH CONNECTION CREDENTIALS"

    #
    ## success
    val = session.query(PlexConfig).filter(
        PlexConfig.service == 'rsync').first()
    if val is not None:
        session.delete(val)
        session.commit()
    newval = PlexConfig(service='rsync',
                        data={
                            'localdir': os.path.abspath(local_dir.strip()),
                            'sshpath': sshpath.strip(),
                            'subdir': subdir,
                            'password': password
                        })
    session.add(newval)
    session.commit()
    mystr_split = [
        'local directory to download to: %s' %
        os.path.abspath(local_dir.strip()),
        'SSH path from which to get files: %s' % sshpath.strip(),
    ]
    if subdir is not None:
        mystr_split.append(
            'sub directory on local machine from which to get files: %s' %
            subdir)
        logging.debug('\n'.join(mystr_split))
    if subdir is not None:
        mystr_split.append(
            'sub directory on local machine from which to get files: %s' %
            subdir)
    logging.debug('\n'.join(mystr_split))
    return 'SUCCESS'
Example #5
0
def get_tmdb_api():
    """
    Returns the TMDB_ API key found in the SQLite3_ configuration database (specifically the ``tmdb`` service column in the ``plexconfig`` table). Otherwise returns ``None`` if not found.
    
    :returns: the TMDB_ API key.
    :rtype: str
    """
    query = session.query(PlexConfig).filter(PlexConfig.service == 'tmdb')
    val = query.first()
    if val is None:
        raise ValueError("ERROR, NO TMDB API CREDENTIALS FOUND")
    return val.data['apikey']
Example #6
0
def save_tmdb_api(apikey):
    """
    Saves the provided TMDB_ API key into the ``plexconfig`` table, under the ``tmdb`` service, in the SQLite3_ configuration database.

    :param str apikey: the TMDB_ API key.

    .. _TMDB: https://www.themoviedb.org/documentation/api?language=en-US
    .. _SQLite3: https://www.themoviedb.org/documentation/api?language=en-US
    """
    query = session.query(PlexConfig).filter(PlexConfig.service == 'tmdb')
    val = query.first()
    if val is not None:
        session.delete(val)
        session.commit()
    newval = PlexConfig(service='tmdb', data={'apikey': apikey.strip()})
    session.add(newval)
    session.commit()