Ejemplo n.º 1
0
def put_playlists(select=None):
    """
    Adds playlist items to generated spotify playlists 
    Accepts optional array of which categories to place songs into 
    Returns True if successful else None  
    """

    playlist_ids = get_playlist_ids()

    others_help.print_alert('put_playlists(): Gathering uris for tracks...')

    if not select:
        select = [category for category in melon_data.get_categories()]

    processed = []
    for category in select:
        msg = f'Do you want to process "{category}" (yes) or (no)? '
        if others_help.validate_choices(['yes', 'no'], msg=msg) == 'yes':
            search_results = spotify_run.search_tracks([category])
            save_results(search_results, 'pre-results')
            process_results([category])
            processed.append(category)

    if len(processed) == 0:
        raise Exception('Must process at least one category of songs.')

    uris = get_processed_results(processed)

    tokens_help.refresh_token()
    access_token, _ = tokens_help.get_tokens()
    headers = {
        'Authorization': f'Authorization: Bearer {access_token}',
    }

    others_help.print_alert('put_playlists(): Adding top songs for charts...')

    for chart in processed:
        playlist_id = playlist_ids.get(chart, None)
        playlist_uris = uris.get(chart, None)

        if not all([playlist_id, playlist_uris]):
            raise Exception(
                f'{chart} is not a valid chart category or is not in list of playlist uris.'
            )

        playlist_uris = ','.join(playlist_uris)

        endpoint = f'https://api.spotify.com/v1/playlists/{playlist_id}/tracks?uris={playlist_uris}'

        api_help.requests_general('put', endpoint=endpoint, headers=headers)

        others_help.print_alert(f'Top Songs added for... "{chart}" chart')

    return True
Ejemplo n.º 2
0
def get_id(): 
    """
    Gets the spotify user id 
    No params
    Returns string of spotify user id 
    """
    
    tokens_help.refresh_token() # token access lasts only 1 hr for Spotify API 

    access_token, _ = tokens_help.get_tokens() 

    endpoint = 'https://api.spotify.com/v1/me'

    headers = {
        'Authorization': f'Authorization: Bearer {access_token}',
    }

    response = api_help.requests_general('get', endpoint, headers=headers)

    user_id = response.get('id', None) 

    if not user_id: 
        raise Exception('User id missing in response in user_id().')
    
    return user_id
Ejemplo n.º 3
0
def refresh_token():
    """
    Refreshes user authentication token if appropriate auth token exists 
    No params 
    Returns True if successful else None 
    """

    _, refresh_token = get_tokens()

    grant_type = 'refresh_token'

    endpoint = 'https://accounts.spotify.com/api/token'

    client_id, client_secret = api_help.get_auth()

    data = {
        'grant_type': grant_type,
        'refresh_token': refresh_token,
        'client_id': client_id,
        'client_secret': client_secret,
    }

    response = api_help.requests_general('post', endpoint, data)

    access_token = response['access_token']

    tokens = {
        'access_token': access_token,
        'refresh_token': refresh_token,
    }

    post_tokens(tokens)

    return True
Ejemplo n.º 4
0
def create_playlists():
    """
    Creates spotify playlists with preset playlist names and descriptions, saves important information to info dir
    Accepts no params 
    Returns None 
    """

    tokens_help.refresh_token(
    )  # refreshes token // token expires very quickly (~1 hr), assuming user is not spamming, justified to do so

    access_token, _ = tokens_help.get_tokens()

    content_type = 'application/json'

    headers = {
        'Authorization': f'Authorization: Bearer {access_token}',
        'Content-Type': content_type,
    }

    user_id = spotify_run.get_id()

    endpoint = f'https://api.spotify.com/v1/users/{user_id}/playlists'

    playlists = get_set_playlists()

    playlist_info = {}

    for category in playlists:
        name, description = playlists.get(category)

        data = {
            'name': name,
            'description': description,
        }

        data = json.dumps(data)

        response = api_help.requests_general('post',
                                             endpoint,
                                             data=data,
                                             headers=headers)

        playlist_info[category] = response.get('uri')

    return playlist_info
Ejemplo n.º 5
0
def auto_search_track(track): 
    """
    Get response of track search -- DRY 
    Accepts string param of track 
    Returns response 
    """

    tokens_help.refresh_token() 

    access_token, _ = tokens_help.get_tokens() 

    track_form = '%20'.join(track.split())

    item_type = 'track'

    endpoint = f'https://api.spotify.com/v1/search?q={track_form}&type={item_type}'

    headers = {
        'Authorization': f'Authorization: Bearer {access_token}'
    }

    response = api_help.requests_general('get', endpoint, headers=headers)

    return response