[Python] Getting Song Stream Counts from Streaming Services to Auto-Update in Excel Sheet

Discussão em 'Python' iniciado por Stack, Junho 27, 2025.

  1. Stack

    Stack Membro Participativo

    It would be helpful to have a few columns in an Excel sheet that auto-update daily with the number of streams that a bunch of songs (also housed in spreadsheet) have on different streaming services (different columns for Spotify/Apple music/YouTube/etc.).

    My first thought was to use these services' APIs along with Openpyxl to write some Python scripts that accomplish the task listed above. I wrote one that works for Spotify which I'll post below so you can get an idea of where I'm at (it finds the songs' popularities from a column full of the Spotify song ids that I got from another script I wrote).

    The Apple Music API seems to require a paid subscription to the Apple development program, and I have found some other APIs that seem like they might do the job (ie https://docs.songstats.com/). Are these APIs the best way to do this, or is there a more effective and efficient way I should be looking at since in the end I'm not really coding an app and don't need all the other fancy bells and whistles incorporated into the APIs?

    import openpyxl
    import requests
    import base64

    # Spotify API credentials
    CLIENT_ID = (not posting these here haha)
    CLIENT_SECRET = ""

    # Function to get an access token from Spotify
    def get_access_token():
    url = 'https://accounts.spotify.com/api/token'
    credentials = f'{CLIENT_ID}:{CLIENT_SECRET}'
    encoded_credentials = base64.b64encode(credentials.encode('utf-8')).decode('utf-8')
    headers = {'Authorization': f'Basic {encoded_credentials}'}
    data = {'grant_type': 'client_credentials'}
    response = requests.post(url, data=data, headers=headers)
    return response.json()['access_token']

    # Function to get song popularity from Spotify
    def get_song_popularity(song_uri, access_token):
    if song_uri is None:
    return None

    song_id = song_uri.split(':')[-1] # Extract the last part of the URI as the song ID
    url = f'https://api.spotify.com/v1/tracks/{song_id}'
    headers = {'Authorization': f'Bearer {access_token}'}
    response = requests.get(url, headers=headers)
    data = response.json()

    if 'popularity' in data:
    return data['popularity']
    else:
    return None

    # Function to process the Excel file and update with song popularity
    def process_excel(excel_file_path):
    wb = openpyxl.load_workbook(excel_file_path)
    ws = wb.active

    access_token = get_access_token()

    # Adding a new column header
    ws.cell(row=1, column=4, value='Popularity')

    for row_idx, cell in enumerate(ws['C'][1:], start=2): # Assuming URIs are in column A starting from the second row
    song_uri = cell.value
    song_popularity = get_song_popularity(song_uri, access_token)

    if song_popularity is not None:
    ws.cell(row=row_idx, column=4, value=song_popularity)

    wb.save(excel_file_path)

    if __name__ == '__main__':
    excel_file_path = 'streams.xlsx'
    process_excel(excel_file_path)


    (SpotifyAPI annoyingly does not let you access raw song streams, but rather a "popularity index" which is an integer from 0-100 which ironically uses streams somewhere in the algorithm to obtain the final number, but I guess Spotify just wants to be convoluted. If anyone has ideas on how to access streams directly, let me know).

    Continue reading...

Compartilhe esta Página