Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Any way to automate this ? #62

Open
Nigel1992 opened this issue Oct 12, 2023 · 0 comments
Open

Any way to automate this ? #62

Nigel1992 opened this issue Oct 12, 2023 · 0 comments

Comments

@Nigel1992
Copy link

Nigel1992 commented Oct 12, 2023

I want to automatically make backups of my Spotify every X days.
Is there a way to do this using your code or perhaps my Python code ?

The below code finds 6707 out of my 8000+ songs.
Any idea maybe?
from spotipy.oauth2 import SpotifyOAuth
import json
import time
import requests

# Set up your Spotify API credentials
client_id = 'id'
client_secret = 'secret'
redirect_uri = 'redirect_uri'  # This is the local server address
username = 'username/email'

# Authenticate with the Spotify API
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri, scope='playlist-read playlist-read-private playlist-modify-public playlist-modify-private user-library-read user-library-modify'))

# Function to retrieve user's username
def get_user_username():
    user_info = sp.current_user()
    return user_info['id']

# Function to retrieve all tracks within a playlist (including paginated tracks)
def get_all_playlist_tracks(playlist_id):
    all_tracks = []
    limit = 100  # Set to the maximum limit

    tracks = sp.playlist_tracks(playlist_id, limit=limit)
    all_tracks.extend([track['track']['name'] for track in tracks['items']])

    while tracks['next']:
        tracks = sp.next(tracks)
        all_tracks.extend([track['track']['name'] for track in tracks['items']])

    return all_tracks

# Function to save data to a JSON file
def save_to_file(data, filename):
    with open(filename, 'w', encoding='utf-8') as file:
        json.dump(data, file, ensure_ascii=False, indent=4)

# Get the current user's username
username = get_user_username()

# Get a list of the user's playlists
playlists = sp.user_playlists(username)

# Initialize a counter for total tracks
total_tracks = 0

# Initialize a set to keep track of processed playlist IDs
processed_playlists = set()

# Start an infinite loop
while True:
    # Check if all playlists have been processed
    if len(processed_playlists) == len(playlists['items']):
        break

    # Backup all playlists with a 100 ms delay
    backup_data = {}
    for playlist in playlists['items']:
        if playlist['id'] not in processed_playlists:
            playlist_name = playlist['name']
            playlist_id = playlist['id']
            tracks = get_all_playlist_tracks(playlist_id)
            backup_data[playlist_name] = tracks
            total_tracks += len(tracks)
            processed_playlists.add(playlist_id)  # Mark playlist as processed
            time.sleep(0.1)

    # Save all playlist and track information to a JSON file
    save_to_file(backup_data, 'spotify_backup.json')

# Display the total number of tracks found
print(f'Total tracks found: {total_tracks}')
print('Backup of all playlists has been saved to spotify_backup.json')```
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant