Пример #1
0
def before_request():
    if request.endpoint == 'logout':
        return

    current_user = session['current_user'] if 'current_user' in session else ''
    g.user = None
    if current_user:
        g.user = select_user(current_user)

    access_token = session['access_token'] if 'access_token' in session else ''
    g.access_token = access_token
    g.spotify = None
    if access_token:
        expiration = session['expires_at']

        # If auth token has expired, refresh it
        if int(expiration) < int(time()):
            refresh_token = session['refresh_token']
            oauth = get_spotify_oauth()
            
            # Calls to refresh token sometimes fail, so retry
            token_info = None
            retries = 0
            while token_info is None and retries < 3:
                token_info = oauth.refresh_access_token(refresh_token)
                retries += 1

            access_token = token_info['access_token']
            _update_session(
                current_user, access_token, token_info['refresh_token'],
                int(token_info['expires_at']))

        g.spotify = Spotify(access_token)
Пример #2
0
    def test_spotify_bot_oauth(self, oauth_construct):

        oauth = get_spotify_oauth(bot=True)

        self.assertIsInstance(oauth, SpotifyOAuth)
        oauth_construct.assert_called_once_with(self.client_id,
                                                self.client_secret,
                                                self.bot_redirect_uri,
                                                scope=BOT_SCOPES)
Пример #3
0
def hello():
    # If user is logged in, always send to profile
    if g.user:
        return redirect(url_for('profile'))

    return {
        'user': g.user,
        'oauth_url': get_spotify_oauth().get_authorize_url(),
    }
Пример #4
0
def login():
    # If no current login, send user through Spotify OAuth process.
    # If current login, send user to his/her profile.
    if 'current_user' not in session:
        url = request.url
        oauth = get_spotify_oauth()
        code = oauth.parse_response_code(url)
        if code:
            token_info = oauth.get_access_token(code)
            access_token = token_info['access_token']
            refresh_token = token_info['refresh_token']
            expires_at = int(token_info['expires_at'])

            spotify = Spotify(access_token)
            spotify_user = spotify.current_user()
            user_id = spotify_user.get('id')

            user = get_user(user_id)

            # If user logging in w/ Spotify does not yet exist, create it
            if not user:
                user = create_user_from_spotify_user(spotify_user)
                track_new_user(user.id)
            else:
                track_user_login(user_id)

                # If user's image is from Facebook, token may have expired.
                # TODO: This needs to be smarter
                if 'fbcdn.net' in user.image_url:
                    user = update_user_from_spotify_user(user, spotify_user)

            _update_session(user_id, access_token, refresh_token, expires_at)
            session.permanent = True

            # If user was going to a particular destination before logging in,
            # send them there after login.
            if 'next_url' in session:
                app.logger.info('Found next_url in session: %s', session['next_url'])
                next_url = session['next_url'].decode('base64', 'strict')
                app.logger.info('Decoded next_url in session, redirecting: %s', next_url)
                session.pop('next_url')
                return redirect(next_url)

    return redirect(url_for('profile'))
Пример #5
0
def get_botify(bot_id=None):
    if bot_id is None:
        bot_id = get_setting(SPOTIFY_BOT_USERNAME)

    bot = select_bot(bot_id)
    if not bot:
        return None, None

    # If the token has expired or will expire very soon, refresh it
    if bot.expires_at < (int(time()) + 10):
        app.logger.debug('Bot %s access expired. Refreshing.', bot_id)
        oauth = get_spotify_oauth()
        token_info = oauth.refresh_access_token(bot.refresh_token)
        access_token = token_info['access_token']
        refresh_token = token_info['refresh_token']
        expires_at = token_info['expires_at']
        bot = update_bot(bot_id, access_token, refresh_token, expires_at)

    return bot_id, Spotify(bot.access_token)
Пример #6
0
def add_bot():
    _clear_session()
    oauth = get_spotify_oauth(bot=True)
    code = oauth.parse_response_code(request.url)
    if code:
        token_info = oauth.get_access_token(code)
        access_token = token_info['access_token']
        refresh_token = token_info['refresh_token']
        expires_at = int(token_info['expires_at'])

        spotify = Spotify(access_token)
        spotify_user = spotify.current_user()
        bot_id = spotify_user['id']

        app.logger.warn('Create/update bot %s: %s, %s, %s', bot_id,
                        access_token, refresh_token, expires_at)

        create_or_update_bot(bot_id, access_token, refresh_token, expires_at)

        return 'Successfully added bot: %s' % (bot_id)

    return redirect(oauth.get_authorize_url())