Пример #1
0
def update_token(user_id, access_token, refresh_token, expires_at):
    """ Update token for user with specified LB user ID.

    Args:
        user_id (int): the ListenBrainz row ID of the user
        access_token (str): the new access token,
        refresh_token (str): the new token used to refresh access tokens,
        expires_at (int): the unix timestamp at which the access token expires

    Returns:
        the new token in dict form
    """
    token_expires = utils.unix_timestamp_to_datetime(expires_at)
    with db.engine.connect() as connection:
        connection.execute(
            sqlalchemy.text("""
            UPDATE spotify_auth
               SET user_token = :user_token
                 , refresh_token = :refresh_token
                 , token_expires = :token_expires
             WHERE user_id = :user_id
        """), {
                "user_token": access_token,
                "refresh_token": refresh_token,
                "token_expires": token_expires,
                "user_id": user_id,
            })
Пример #2
0
def create_spotify(user_id, user_token, refresh_token, token_expires_ts,
                   record_listens, permission):
    """ Add a row to the spotify table for specified user with corresponding
    Spotify tokens and information.

    Args:
        user_id (int): the ListenBrainz row ID of the user
        user_token (str): the Spotify access token used to access the user's Spotify listens.
        refresh_token (str): the token used to refresh Spotify access tokens once they expire
        token_expires_ts (int): the unix timestamp at which the user_token will expire
        record_listens (bool): True if user wishes to import listens from Spotify, False otherwise
        permission (str): the scope of the permissions granted to us by the user as a space seperated string
    """
    token_expires = utils.unix_timestamp_to_datetime(token_expires_ts)
    with db.engine.connect() as connection:
        connection.execute(
            sqlalchemy.text("""
            INSERT INTO spotify_auth (user_id, user_token, refresh_token, token_expires, record_listens, permission)
                 VALUES (:user_id, :user_token, :refresh_token, :token_expires, :record_listens, :permission)
            """), {
                "user_id": user_id,
                "user_token": user_token,
                "refresh_token": refresh_token,
                "token_expires": token_expires,
                "record_listens": record_listens,
                "permission": permission,
            })
def update_token(user_id: int, service: ExternalServiceType, access_token: str,
                 refresh_token: str, expires_at: int):
    """ Update the token for user with specified LB user ID and external service.

    Args:
        user_id: the ListenBrainz row ID of the user
        service: the service for which the token should be updated
        access_token: the new access token
        refresh_token: the new token used to refresh access tokens
        expires_at: the unix timestamp at which the access token expires
    """
    token_expires = utils.unix_timestamp_to_datetime(expires_at)
    with db.engine.connect() as connection:
        connection.execute(
            sqlalchemy.text("""
            UPDATE external_service_oauth
               SET access_token = :access_token
                 , refresh_token = :refresh_token
                 , token_expires = :token_expires
                 , last_updated = now()
             WHERE user_id = :user_id AND service = :service
        """), {
                "access_token": access_token,
                "refresh_token": refresh_token,
                "token_expires": token_expires,
                "user_id": user_id,
                "service": service.value
            })
Пример #4
0
def update_token(user_id, access_token, refresh_token, expires_at):
    """ Update token for user with specified LB user ID.

    Args:
        user_id (int): the ListenBrainz row ID of the user
        access_token (str): the new access token,
        refresh_token (str): the new token used to refresh access tokens,
        expires_at (int): the unix timestamp at which the access token expires

    Returns:
        the new token in dict form
    """
    token_expires = utils.unix_timestamp_to_datetime(expires_at)
    with db.engine.connect() as connection:
        connection.execute(sqlalchemy.text("""
            UPDATE spotify_auth
               SET user_token = :user_token
                 , refresh_token = :refresh_token
                 , token_expires = :token_expires
             WHERE user_id = :user_id
        """), {
            "user_token": access_token,
            "refresh_token": refresh_token,
            "token_expires": token_expires,
            "user_id": user_id
        })
Пример #5
0
def save_token(user_id: int, service: ExternalServiceType, access_token: str,
               refresh_token: str, token_expires_ts: int, record_listens: bool,
               scopes: List[str]):
    """ Add a row to the external_service_oauth table for specified user with corresponding tokens and information.

    Args:
        user_id: the ListenBrainz row ID of the user
        service: the service for which the token can be used
        access_token: the access token used to access the user's listens
        refresh_token: the token used to refresh access tokens once they expire
        token_expires_ts: the unix timestamp at which the user_token will expire
        record_listens: True if user wishes to import listens, False otherwise
        scopes: the oauth scopes
    """
    token_expires = utils.unix_timestamp_to_datetime(token_expires_ts)
    with db.engine.connect() as connection:
        result = connection.execute(
            sqlalchemy.text("""
            INSERT INTO external_service_oauth
            (user_id, service, access_token, refresh_token, token_expires, scopes)
            VALUES
            (:user_id, :service, :access_token, :refresh_token, :token_expires, :scopes)
            ON CONFLICT (user_id, service)
            DO UPDATE SET
                user_id = EXCLUDED.user_id,
                service = EXCLUDED.service,
                access_token = EXCLUDED.access_token,
                refresh_token = EXCLUDED.refresh_token,
                token_expires = EXCLUDED.token_expires,
                scopes = EXCLUDED.scopes
            RETURNING id
            """), {
                "user_id": user_id,
                "service": service.value,
                "access_token": access_token,
                "refresh_token": refresh_token,
                "token_expires": token_expires,
                "scopes": scopes,
            })

        if record_listens:
            external_service_oauth_id = result.fetchone()['id']
            connection.execute(
                sqlalchemy.text("""
                INSERT INTO listens_importer
                (external_service_oauth_id, user_id, service)
                VALUES
                (:external_service_oauth_id, :user_id, :service)
                ON CONFLICT (user_id, service) DO UPDATE SET
                    external_service_oauth_id = EXCLUDED.external_service_oauth_id,
                    user_id = EXCLUDED.user_id,
                    service = EXCLUDED.service
                """), {
                    "external_service_oauth_id": external_service_oauth_id,
                    "user_id": user_id,
                    "service": service.value
                })
Пример #6
0
def update_latest_listened_at(user_id, timestamp):
    """ Update the timestamp of the last listen imported for the user with
    specified LB user ID.

    Args:
        user_id (int): the ListenBrainz row ID of the user
        timestamp (int): the unix timestamp of the latest listen imported for the user
    """
    with db.engine.connect() as connection:
        connection.execute(sqlalchemy.text("""
            UPDATE spotify_auth
               SET latest_listened_at = :timestamp
             WHERE user_id = :user_id
            """), {
                'user_id': user_id,
                'timestamp': utils.unix_timestamp_to_datetime(timestamp),
            })
Пример #7
0
def update_latest_listened_at(user_id, timestamp):
    """ Update the timestamp of the last listen imported for the user with
    specified LB user ID.

    Args:
        user_id (int): the ListenBrainz row ID of the user
        timestamp (int): the unix timestamp of the latest listen imported for the user
    """
    with db.engine.connect() as connection:
        connection.execute(
            sqlalchemy.text("""
            UPDATE spotify_auth
               SET latest_listened_at = :timestamp
             WHERE user_id = :user_id
            """), {
                'user_id': user_id,
                'timestamp': utils.unix_timestamp_to_datetime(timestamp),
            })
Пример #8
0
def update_latest_listened_at(user_id: int, service: ExternalServiceType, timestamp: int):
    """ Update the timestamp of the last listen imported for the user with
    specified LB user ID.

    Args:
        user_id (int): the ListenBrainz row ID of the user
        service (data.model.ExternalServiceType): service to update latest listen timestamp for
        timestamp (int): the unix timestamp of the latest listen imported for the user
    """
    with db.engine.connect() as connection:
        connection.execute(sqlalchemy.text("""
            UPDATE listens_importer
               SET last_updated = now()
                 , latest_listened_at = :timestamp
             WHERE user_id = :user_id
               AND service = :service
            """), {
                'user_id': user_id,
                'service': service.value,
                'timestamp': utils.unix_timestamp_to_datetime(timestamp),
            })
Пример #9
0
def create_spotify(user_id, user_token, refresh_token, token_expires_ts):
    """ Add a row to the spotify table for specified user with corresponding
    Spotify tokens and information.

    Args:
        user_id (int): the ListenBrainz row ID of the user
        user_token (str): the Spotify access token used to access the user's Spotify listens.
        refresh_token (str): the token used to refresh Spotify access tokens once they expire
        token_expires_ts (int): the unix timestamp at which the user_token will expire
    """
    token_expires = utils.unix_timestamp_to_datetime(token_expires_ts)
    with db.engine.connect() as connection:
        connection.execute(sqlalchemy.text("""
            INSERT INTO spotify_auth (user_id, user_token, refresh_token, token_expires)
                 VALUES (:user_id, :user_token, :refresh_token, :token_expires)
            """), {
                "user_id": user_id,
                "user_token": user_token,
                "refresh_token": refresh_token,
                "token_expires": token_expires,
            })
Пример #10
0
def update_latest_listened_at(user_id: int, service: ExternalServiceType, timestamp: Union[int, float]):
    """ Update the timestamp of the last listen imported for the user with
    specified LB user ID.

    Args:
        user_id (int): the ListenBrainz row ID of the user
        service (data.model.ExternalServiceType): service to update latest listen timestamp for
        timestamp (int): the unix timestamp of the latest listen imported for the user
    """
    with db.engine.connect() as connection:
        connection.execute(sqlalchemy.text("""
            INSERT INTO listens_importer (user_id, service, last_updated, latest_listened_at)
                 VALUES (:user_id, :service, now(), :timestamp)
            ON CONFLICT (user_id, service)
              DO UPDATE 
                    SET last_updated = now()
                      , latest_listened_at = :timestamp
            """), {
                'user_id': user_id,
                'service': service.value,
                'timestamp': utils.unix_timestamp_to_datetime(timestamp),
            })
Пример #11
0
def create_spotify(user_id, user_token, refresh_token, token_expires_ts):
    """ Add a row to the spotify table for specified user with corresponding
    Spotify tokens and information.

    Args:
        user_id (int): the ListenBrainz row ID of the user
        user_token (str): the Spotify access token used to access the user's Spotify listens.
        refresh_token (str): the token used to refresh Spotify access tokens once they expire
        token_expires_ts (int): the unix timestamp at which the user_token will expire
    """
    token_expires = utils.unix_timestamp_to_datetime(token_expires_ts)
    with db.engine.connect() as connection:
        connection.execute(
            sqlalchemy.text("""
            INSERT INTO spotify_auth (user_id, user_token, refresh_token, token_expires)
                 VALUES (:user_id, :user_token, :refresh_token, :token_expires)
            """), {
                "user_id": user_id,
                "user_token": user_token,
                "refresh_token": refresh_token,
                "token_expires": token_expires,
            })
Пример #12
0
 def test_unix_timestamp_to_datetime(self):
     t = int(time.time())
     x = utils.unix_timestamp_to_datetime(t)
     self.assertIsInstance(x, datetime)
     self.assertEqual(int(x.strftime('%s')), t)