Exemplo n.º 1
0
def callback():
    error = request.args.get('error_reason', None)
    code = request.args.get('code', None)
    state = request.args.get('state', None)
    stored_state = session.get('deezer_auth_state', None)

    if error is not None or state is None or state != stored_state:
        current_app.logger.error('Could not authenticate deezer user:\n' +
                                 'error: {}\n'.format(error) +
                                 'state: {}\n'.format(state) +
                                 'stored_state: {}\n'.format(stored_state))
        return render_template(
            'show_message.html',
            message='There was an error while trying to authenticate you.'
                    'Please, try again.'), 503
    else:
        session.pop('deezer_auth_state', None)
        try:
            models.request_access_token(code)
            influxdb.count('deezer.logins')
            return redirect(url_for('deezer.index'))
        except Exception as e:
            current_app.logger.error(
                'Could not authenticate deezer user: {}'.format(e))
            return render_template(
                'show_message.html',
                message='There was an error while trying to authenticate you.'
                        'Please, try again.'), 503
Exemplo n.º 2
0
def callback():
    error = request.args.get('error', None)
    code = request.args.get('code', None)
    state = request.args.get('state', None)
    stored_state = session.get('spotify_auth_state', None)

    if error is not None or state is None or state != stored_state:
        current_app.logger.error('Could not authenticate spotify user:\n' +
                                 'error: {}\n'.format(error) +
                                 'state: {}\n'.format(state) +
                                 'stored_state: {}\n'.format(stored_state))
        return render_template(
            'show_message.html',
            message='There was an error while trying to authenticate you.'
                    'Please, try again.'), 503
    else:
        session.pop('spotify_auth_state', None)

        try:
            token_data = models.get_access_token_from_code(code)
            models.save_token_data_in_session(token_data)
            influxdb.count('spotify.logins')
            return redirect(url_for('spotify.index'))
        except Exception as e:
            current_app.logger.error(
                'Could not authenticate spotify user: {}'.format(e))
            return render_template(
                'show_message.html',
                message='There was an error while trying to authenticate you.'
                        'Please, try again.'), 503
Exemplo n.º 3
0
def refresh_access_token(refresh_token):
    influxdb.count('mobile_api.refresh_access_token_requests')
    payload = {'refresh_token': refresh_token, 'grant_type': 'refresh_token'}

    headers = {'Authorization': 'Basic {}'.format(SPOTIFY_AUTHORIZATION_DATA)}

    try:
        post_request = requests.post('https://accounts.spotify.com/api/token',
                                     data=payload,
                                     headers=headers)

        if post_request.status_code == 200:
            response_data = json.loads(post_request.text)
            access_token = response_data['access_token']
            expires_in = response_data['expires_in']

            return jsonify(access_token=access_token, expires_in=expires_in)
        else:
            raise RuntimeError('Could not get authentication token')
    except Exception as e:
        current_app.logger.error(
            'Could not authenticate spotify user: {}'.format(e))
        return jsonify(
            error='There was an error while trying to authenticate you.'
            'Please, try again.'), 503
Exemplo n.º 4
0
def refresh_access_token(refresh_token):
    influxdb.count('mobile_api.refresh_access_token_requests')
    payload = {
        'refresh_token': refresh_token,
        'grant_type': 'refresh_token'
    }

    headers = {
        'Authorization': 'Basic {}'.format(SPOTIFY_AUTHORIZATION_DATA)
    }

    try:
        post_request = requests.post('https://accounts.spotify.com/api/token',
                                     data=payload, headers=headers)

        if post_request.status_code == 200:
            response_data = json.loads(post_request.text)
            access_token = response_data['access_token']
            expires_in = response_data['expires_in']

            return jsonify(
                access_token=access_token,
                expires_in=expires_in)
        else:
            raise RuntimeError('Could not get authentication token')
    except Exception as e:
        current_app.logger.error(
            'Could not authenticate spotify user: {}'.format(e))
        return jsonify(
            error='There was an error while trying to authenticate you.'
                  'Please, try again.'), 503
Exemplo n.º 5
0
def download_song(song_name, format):
    influxdb.count('mobile_api.download_song_requests')

    if format not in SUPPORTED_FORMATS:
        current_app.logger.warning(
            'User tried to download a song in unsupported format.\n' +
            'Song: {}\n'.format(song_name) + 'Format: {}\n'.format(format))
        return jsonify(reason='Unsupported format'), 400

    if not songs.has_song_format(song_name, format):
        provider = request.args.get('provider', SUPPORTED_PROVIDERS[0])
        if provider not in SUPPORTED_PROVIDERS:
            current_app.logger.warning(
                'User tried to download a song with unsupported provider.\n' +
                'Song: {}\n'.format(song_name) +
                'Format: {}\n'.format(format) +
                'Provider: {}\n'.format(provider))
            return jsonify(reason='Unsupported provider'), 400

        song = {'name': song_name}
        songs.download_song.delay(song, format=format, provider=provider)
        return jsonify(refresh_after=30,
                       message='Your song has started downloading.')

    influxdb.count('mobile_api.downloaded_songs')
    song = songs.get_song(song_name)

    return send_file(song['files'][format],
                     as_attachment=True,
                     attachment_filename='{}.{}'.format(song['name'], format),
                     mimetype=MIMETYPES[format])
Exemplo n.º 6
0
def index():
    influxdb.count('youtube.index_page_visits')

    playlists = models.get_playlists()
    return render_template('show_playlists.html',
                           service='youtube',
                           title='Youtube playlists',
                           playlists=playlists)
Exemplo n.º 7
0
def index():
    influxdb.count('spotify.index_page_visits')

    playlists = models.get_playlists()
    return render_template('show_playlists.html',
                           service='spotify',
                           title='Spotify playlists',
                           playlists=playlists)
Exemplo n.º 8
0
def logout():
    session.pop('deezer_access_token', None)
    session.pop('deezer_expires_at', None)
    session.pop('deezer_user_id', None)

    influxdb.count('deezer.logouts')

    return redirect(url_for('views.index'))
Exemplo n.º 9
0
def logout():
    session.pop('spotify_access_token', None)
    session.pop('spotify_refresh_token', None)
    session.pop('spotify_expires_at', None)
    session.pop('spotify_username', None)

    influxdb.count('spotify.logouts')

    return redirect(url_for('views.index'))
Exemplo n.º 10
0
def logout():
    session.pop('spotify_access_token', None)
    session.pop('spotify_refresh_token', None)
    session.pop('spotify_expires_at', None)
    session.pop('spotify_username', None)

    influxdb.count('spotify.logouts')

    return redirect(url_for('views.index'))
Exemplo n.º 11
0
def index():
    influxdb.count('deezer.index_page_visits')

    playlists = models.get_playlists()
    return render_template(
        'show_playlists.html',
        service='deezer',
        title='Deezer playlists',
        playlists=playlists
    )
Exemplo n.º 12
0
def index():
    influxdb.count('spotify.index_page_visits')

    playlists = models.get_playlists()
    return render_template(
        'show_playlists.html',
        service='spotify',
        title='Spotify playlists',
        playlists=playlists
    )
Exemplo n.º 13
0
def index():
    influxdb.count('youtube.index_page_visits')

    playlists = models.get_playlists()
    return render_template(
        'show_playlists.html',
        service='youtube',
        title='Youtube playlists',
        playlists=playlists
    )
Exemplo n.º 14
0
def refresh_access_token(refresh_token):
    influxdb.count('spotify.refresh_token_requests')

    try:
        refresh_token = unquote(refresh_token)
        tokens = models.get_access_token_from_refresh_token(refresh_token)
        return jsonify(**tokens)
    except Exception as e:
        current_app.logger.error(
            'Could not refresh access token: {}'.format(e))
        return jsonify(error='Unable to refresh token.'), 401
Exemplo n.º 15
0
def refresh_access_token(refresh_token):
    influxdb.count('spotify.refresh_token_requests')

    try:
        refresh_token = unquote(refresh_token)
        tokens = models.get_access_token_from_refresh_token(refresh_token)
        return jsonify(**tokens)
    except Exception as e:
        current_app.logger.error(
            'Could not refresh access token: {}'.format(e))
        return jsonify(error='Unable to refresh token.'), 401
Exemplo n.º 16
0
def get_access_token(code):
    influxdb.count('deezer.access_token_requests')

    try:
        tokens = models.get_access_token_from_code(code)
        return jsonify(**tokens)
    except Exception as e:
        current_app.logger.error(
            'Could not authenticate deezer user: {}'.format(e))
        return jsonify(
            error='There was an error while trying to authenticate you.'
                  'Please, try again.'), 503
Exemplo n.º 17
0
def refresh_access_token(refresh_token):
    influxdb.count('youtube.refresh_token_requests')

    try:
        refresh_token = unquote(refresh_token)
        credentials = models.refresh_tokens(refresh_token)
        return jsonify(access_token=credentials['access_token'],
                       expires_in=credentials['expires_in'])
    except Exception as e:
        current_app.logger.error(
            'Could not authenticate youtube user: {}'.format(e))
        return jsonify(error='Unable to refresh token.'), 503
Exemplo n.º 18
0
def login():
    influxdb.count('youtube.login_attempts')

    flow = client.OAuth2WebServerFlow(
        YOUTUBE_CLIENT_ID,
        YOUTUBE_CLIENT_SECRET,
        scope='https://www.googleapis.com/auth/youtube.readonly',
        redirect_uri=YOUTUBE_REDIRECT_URI)

    auth_uri = flow.step1_get_authorize_url()

    return redirect(auth_uri)
Exemplo n.º 19
0
def download_playlist():
    influxdb.count('spotify.download_playlist_requests')

    playlist_id = request.form.get('playlist_id')
    if playlist_id is None:
        return render_template(
            'show_message.html', message='Invalid request'), 400
    format = request.form.get('format', SUPPORTED_FORMATS[0])
    provider = request.form.get('provider', SUPPORTED_PROVIDERS[0])

    if format not in SUPPORTED_FORMATS:
        current_app.logger.warning(
            'User tried to download a playlist in unsupported format.\n' +
            'Playlist: {}\n'.format(playlist_id) +
            'Format: {}\n'.format(format)
        )
        return render_template(
            'show_message.html', message='Unsupported format'), 400

    if provider not in SUPPORTED_PROVIDERS:
        current_app.logger.warning(
            'User tried to download a playlist with unsupported provider.\n' +
            'Playlist: {}\n'.format(playlist_id) +
            'Format: {}\n'.format(format) +
            'Provider: {}\n'.format(provider)
        )
        return render_template(
            'show_message.html', message='Unsupported provider'), 400

    playlist = models.get_playlist(playlist_id)
    if not playlists.has_playlist('spotify', playlist_id, format):
        playlists.download_playlist.delay(
            playlist, 'spotify', format=format, provider=provider)
        return render_template('show_message.html',
                               message='Your playlist is getting downloaded')

    playlist_checksum = playlists.checksum(playlist['tracks'])
    playlist_data = playlists.get_playlist('spotify', playlist_id, format)

    if playlist_data['checksum'] != playlist_checksum:
        playlists.download_playlist.delay(
            playlist, 'spotify', format=format, provider=provider)
        return render_template('show_message.html',
                               message='Your playlist is getting downloaded')

    influxdb.count('spotify.downloaded_playlists')
    return send_file(
        playlist_data['path'],
        as_attachment=True,
        attachment_filename='{}.zip'.format(playlist['name']),
        mimetype='application/zip'
    )
Exemplo n.º 20
0
def download_playlist():
    influxdb.count('deezer.download_playlist_requests')

    playlist_id = request.form.get('playlist_id')
    if playlist_id is None:
        return render_template(
            'show_message.html', message='Invalid request'), 400
    format = request.form.get('format', SUPPORTED_FORMATS[0])
    provider = request.form.get('provider', SUPPORTED_PROVIDERS[0])

    if format not in SUPPORTED_FORMATS:
        current_app.logger.warning(
            'User tried to download a playlist in unsupported format.\n' +
            'Playlist: {}\n'.format(playlist_id) +
            'Format: {}\n'.format(format)
        )
        return render_template(
            'show_message.html', message='Unsupported format'), 400

    if provider not in SUPPORTED_PROVIDERS:
        current_app.logger.warning(
            'User tried to download a playlist with unsupported provider.\n' +
            'Playlist: {}\n'.format(playlist_id) +
            'Format: {}\n'.format(format) +
            'Provider: {}\n'.format(provider)
        )
        return render_template(
            'show_message.html', message='Unsupported provider'), 400

    playlist = models.get_playlist(playlist_id)
    if not playlists.has_playlist('deezer', playlist_id, format):
        playlists.download_playlist.delay(
            playlist, 'deezer', format=format, provider=provider)
        return render_template('show_message.html',
                               message='Your playlist is getting downloaded')

    playlist_checksum = playlists.checksum(playlist['tracks'])
    playlist_data = playlists.get_playlist('deezer', playlist_id, format)

    if playlist_data['checksum'] != playlist_checksum:
        playlists.download_playlist.delay(
            playlist, 'deezer', format=format, provider=provider)
        return render_template('show_message.html',
                               message='Your playlist is getting downloaded')

    influxdb.count('deezer.downloaded_playlists')
    return send_file(
        playlist_data['path'],
        as_attachment=True,
        attachment_filename='{}.zip'.format(playlist['name']),
        mimetype='application/zip'
    )
Exemplo n.º 21
0
def login():
    influxdb.count('youtube.login_attempts')

    flow = client.OAuth2WebServerFlow(
        YOUTUBE_CLIENT_ID,
        YOUTUBE_CLIENT_SECRET,
        scope='https://www.googleapis.com/auth/youtube.readonly',
        redirect_uri=YOUTUBE_REDIRECT_URI
    )

    auth_uri = flow.step1_get_authorize_url()

    return redirect(auth_uri)
Exemplo n.º 22
0
def refresh_access_token(refresh_token):
    influxdb.count('youtube.refresh_token_requests')

    try:
        refresh_token = unquote(refresh_token)
        credentials = models.refresh_tokens(refresh_token)
        return jsonify(
            access_token=credentials['access_token'],
            expires_in=credentials['expires_in']
        )
    except Exception as e:
        current_app.logger.error(
            'Could not authenticate youtube user: {}'.format(e))
        return jsonify(error='Unable to refresh token.'), 503
Exemplo n.º 23
0
def login():
    influxdb.count('deezer.login_attempts')

    state = get_random_str(16)
    session['deezer_auth_state'] = state
    query_parameters = {
        'app_id': DEEZER_APP_ID,
        'redirect_uri': DEEZER_REDIRECT_URI,
        'perms': 'basic_access',
        'state': state,
    }

    query_parameters = '&'.join(['{}={}'.format(key, urllib.parse.quote(val))
                                 for key, val in query_parameters.items()])
    auth_url = 'https://connect.deezer.com/oauth/auth.php?' + query_parameters
    return redirect(auth_url)
Exemplo n.º 24
0
def login():
    influxdb.count('spotify.login_attempts')

    state = get_random_str(16)
    session['spotify_auth_state'] = state
    query_parameters = {
        'response_type': 'code',
        'redirect_uri': SPOTIFY_REDIRECT_URI,
        'scope': 'user-library-read',
        'state': state,
        'client_id': SPOTIFY_CLIENT_ID
    }

    query_parameters = '&'.join(['{}={}'.format(key, urllib.parse.quote(val))
                                 for key, val in query_parameters.items()])
    auth_url = 'https://accounts.spotify.com/authorize/?' + query_parameters
    return redirect(auth_url)
Exemplo n.º 25
0
def download_song(song_name, format):
    influxdb.count('youtube.download_song_requests')

    if format not in SUPPORTED_FORMATS:
        current_app.logger.warning(
            'User tried to download a song in unsupported format.\n' +
            'Song: {}\n'.format(song_name) +
            'Format: {}\n'.format(format)
        )
        return render_template(
            'show_message.html', message='Unsupported format'), 400

    if not songs.has_song_format(song_name, format):
        provider = request.args.get('provider', SUPPORTED_PROVIDERS[0])
        if provider not in SUPPORTED_PROVIDERS:
            current_app.logger.warning(
                'User tried to download a song with unsupported provider.\n' +
                'Song: {}\n'.format(song_name) +
                'Format: {}\n'.format(format) +
                'Provider: {}\n'.format(provider)
            )
            return render_template(
                'show_message.html', message='Unsupported provider'), 400

        song = {'name': song_name}

        youtube_id = request.args.get('provider_id', None)
        if youtube_id is not None and youtube_id != '':
            song['youtube'] = youtube_id

        songs.download_song.delay(
            song, format=format, provider=provider)
        return render_template(
            'show_message.html', refresh_after=30,
            message='Your song has started downloading.'
                    'This page will automatically refresh after 30 seconds.')

    influxdb.count('youtube.downloaded_songs')
    song = songs.get_song(song_name)
    return send_file(
        song['files'][format],
        as_attachment=True,
        attachment_filename='{}.{}'.format(song['name'], format),
        mimetype=MIMETYPES[format]
    )
Exemplo n.º 26
0
def callback():
    code = request.args.get('code', None)

    if code is None:
        return render_template(
            'show_message.html',
            message='There was an error while trying to authenticate you.'
            'Please, try again.'), 503

    flow = client.OAuth2WebServerFlow(
        YOUTUBE_CLIENT_ID,
        YOUTUBE_CLIENT_SECRET,
        scope='https://www.googleapis.com/auth/youtube.readonly',
        redirect_uri=YOUTUBE_REDIRECT_URI)
    credentials = flow.step2_exchange(code)
    session['credentials'] = credentials.to_json()
    influxdb.count('youtube.logins')
    return redirect(url_for('youtube.index'))
Exemplo n.º 27
0
def login():
    influxdb.count('spotify.login_attempts')

    state = get_random_str(16)
    session['spotify_auth_state'] = state
    query_parameters = {
        'response_type': 'code',
        'redirect_uri': SPOTIFY_REDIRECT_URI,
        'scope': 'user-library-read',
        'state': state,
        'client_id': SPOTIFY_CLIENT_ID
    }

    query_parameters = '&'.join([
        '{}={}'.format(key, urllib.parse.quote(val))
        for key, val in query_parameters.items()
    ])
    auth_url = 'https://accounts.spotify.com/authorize/?' + query_parameters
    return redirect(auth_url)
Exemplo n.º 28
0
def callback():
    code = request.args.get('code', None)

    if code is None:
        return render_template(
            'show_message.html',
            message='There was an error while trying to authenticate you.'
                    'Please, try again.'), 503

    flow = client.OAuth2WebServerFlow(
        YOUTUBE_CLIENT_ID,
        YOUTUBE_CLIENT_SECRET,
        scope='https://www.googleapis.com/auth/youtube.readonly',
        redirect_uri=YOUTUBE_REDIRECT_URI
    )
    credentials = flow.step2_exchange(code)
    session['credentials'] = credentials.to_json()
    influxdb.count('youtube.logins')
    return redirect(url_for('youtube.index'))
Exemplo n.º 29
0
def get_access_token(code):
    influxdb.count('youtube.access_token_requests')

    try:
        code = unquote(code)
        flow = client.OAuth2WebServerFlow(
            YOUTUBE_CLIENT_ID,
            YOUTUBE_CLIENT_SECRET,
            scope='https://www.googleapis.com/auth/youtube.readonly',
            redirect_uri=YOUTUBE_REDIRECT_URI)
        credentials = flow.step2_exchange(code)
        return jsonify(access_token=credentials.access_token,
                       refresh_token=credentials.refresh_token,
                       token_expiry=credentials.token_expiry)
    except Exception as e:
        current_app.logger.error(
            'Could not authenticate youtube user: {}'.format(e))
        return jsonify(
            error='There was an error while trying to authenticate you.'
            'Please, try again.'), 503
Exemplo n.º 30
0
def download_song(song_name, format):
    influxdb.count('youtube.download_song_requests')

    if format not in SUPPORTED_FORMATS:
        current_app.logger.warning(
            'User tried to download a song in unsupported format.\n' +
            'Song: {}\n'.format(song_name) + 'Format: {}\n'.format(format))
        return render_template('show_message.html',
                               message='Unsupported format'), 400

    if not songs.has_song_format(song_name, format):
        provider = request.args.get('provider', SUPPORTED_PROVIDERS[0])
        if provider not in SUPPORTED_PROVIDERS:
            current_app.logger.warning(
                'User tried to download a song with unsupported provider.\n' +
                'Song: {}\n'.format(song_name) +
                'Format: {}\n'.format(format) +
                'Provider: {}\n'.format(provider))
            return render_template('show_message.html',
                                   message='Unsupported provider'), 400

        song = {'name': song_name}

        youtube_id = request.args.get('provider_id', None)
        if youtube_id is not None and youtube_id != '':
            song['youtube'] = youtube_id

        songs.download_song.delay(song, format=format, provider=provider)
        return render_template(
            'show_message.html',
            refresh_after=30,
            message='Your song has started downloading.'
            'This page will automatically refresh after 30 seconds.')

    influxdb.count('youtube.downloaded_songs')
    song = songs.get_song(song_name)
    return send_file(song['files'][format],
                     as_attachment=True,
                     attachment_filename='{}.{}'.format(song['name'], format),
                     mimetype=MIMETYPES[format])
Exemplo n.º 31
0
def get_access_token(code):
    influxdb.count('youtube.access_token_requests')

    try:
        code = unquote(code)
        flow = client.OAuth2WebServerFlow(
            YOUTUBE_CLIENT_ID,
            YOUTUBE_CLIENT_SECRET,
            scope='https://www.googleapis.com/auth/youtube.readonly',
            redirect_uri=YOUTUBE_REDIRECT_URI
        )
        credentials = flow.step2_exchange(code)
        return jsonify(
            access_token=credentials.access_token,
            refresh_token=credentials.refresh_token,
            token_expiry=credentials.token_expiry
        )
    except Exception as e:
        current_app.logger.error(
            'Could not authenticate youtube user: {}'.format(e))
        return jsonify(
            error='There was an error while trying to authenticate you.'
                  'Please, try again.'), 503
Exemplo n.º 32
0
def download_song(song_name, format):
    influxdb.count('mobile_api.download_song_requests')

    if format not in SUPPORTED_FORMATS:
        current_app.logger.warning(
            'User tried to download a song in unsupported format.\n' +
            'Song: {}\n'.format(song_name) +
            'Format: {}\n'.format(format)
        )
        return jsonify(reason='Unsupported format'), 400

    if not songs.has_song_format(song_name, format):
        provider = request.args.get('provider', SUPPORTED_PROVIDERS[0])
        if provider not in SUPPORTED_PROVIDERS:
            current_app.logger.warning(
                'User tried to download a song with unsupported provider.\n' +
                'Song: {}\n'.format(song_name) +
                'Format: {}\n'.format(format) +
                'Provider: {}\n'.format(provider)
            )
            return jsonify(reason='Unsupported provider'), 400

        song = {'name': song_name}
        songs.download_song.delay(song, format=format, provider=provider)
        return jsonify(
            refresh_after=30,
            message='Your song has started downloading.')

    influxdb.count('mobile_api.downloaded_songs')
    song = songs.get_song(song_name)

    return send_file(
        song['files'][format],
        as_attachment=True,
        attachment_filename='{}.{}'.format(song['name'], format),
        mimetype=MIMETYPES[format]
    )
Exemplo n.º 33
0
def logout():
    session.pop('credentials', None)

    influxdb.count('youtube.logouts')

    return redirect(url_for('views.index'))
Exemplo n.º 34
0
def index():
    influxdb.count('index_page_visits')

    return render_template('index.html')
Exemplo n.º 35
0
def logout():
    session.pop('credentials', None)

    influxdb.count('youtube.logouts')

    return redirect(url_for('views.index'))