<commit_before>from flask_login import UserMixin
import spotify
import spotipy
import db_utils
import application as app


class User(UserMixin):
    ''' User class for Flask-Login '''
    def __init__(self, user_id, username=None):
        self.id = int(user_id)
        self.username = username
        self._spotify = None

    @property
    def spotify(self):
        oa_client = spotify.sp_oauth
        # Fetch credentials from database
        if not self._spotify:
            self._spotify = db_utils.spotify_creds_for_user(app.engine,
                                                            self.id)
        # No credentials exist for user
        if self._spotify is None:
            return None
        # Refresh tokens if nescessary
        if oa_client.is_token_expired(self._spotify):
            self._spotify = oa_client.refresh_access_token(self._spotify)
            db_utils.spotify_credentials_upsert(app.engine,
                                                self.id,
                                                self._spotify)
        return spotipy.Spotify(auth=self._spotify['access_token'])


class Playlist(object):
    ''' Playlist object representation '''
    def __init__(self, playlist_id, title=None, duration=0, count=0):
        self.id = playlist_id
        self.title = title
        self.duration = duration
        self.count = count<commit_msg>Don’t save NULL credentials<commit_after>from flask_login import UserMixin
import spotify
import spotipy
import db_utils
import application as app


class User(UserMixin):
    ''' User class for Flask-Login '''
    def __init__(self, user_id, username=None):
        self.id = int(user_id)
        self.username = username
        self._spotify = None

    @property
    def spotify(self):
        oa_client = spotify.sp_oauth
        # Fetch credentials from database
        if not self._spotify:
            self._spotify = db_utils.spotify_creds_for_user(app.engine,
                                                            self.id)
        # No credentials exist for user
        if self._spotify is None:
            return None
        # Refresh tokens if nescessary
        if oa_client.is_token_expired(self._spotify):
            self._spotify = oa_client.refresh_access_token(self._spotify)

        # Save/return new credentials if possible
        if self._spotify:
            db_utils.spotify_credentials_upsert(app.engine,
                                                self.id,
                                                self._spotify)
            return spotipy.Spotify(auth=self._spotify['access_token'])


class Playlist(object):
    ''' Playlist object representation '''
    def __init__(self, playlist_id, title=None, duration=0, count=0):
        self.id = playlist_id
        self.title = title
        self.duration = duration
        self.count = count
{'fraction_non_alphanumeric': 0.07323845886844846, 'fraction_numerical': 0.0013884068031933356, 'mean_word_length': 1.879120879120879, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5470757', 'n_tokens_mistral': 775, 'n_tokens_neox': 761, 'n_words': 185}