def save_track_info(sp: spotipy.client.Spotify, track): track_uri = sp._get_id("track", track["uri"]) os.makedirs("track_info", exist_ok=True) filename = f"track_info/{track_uri}" with open(filename, "w") as f: contents = json.dumps(track, indent=4) f.write(contents)
def get_track_info(sp: spotipy.client.Spotify, track_uri: str): track_uri = sp._get_id("track", track_uri) filename = f"track_info/{track_uri}" with open(filename, "r") as f: contents = f.read() track = json.loads(contents) return track
def get_audio_features(sp: spotipy.client.Spotify, track_uri: str): track_uri = sp._get_id("track", track_uri) os.makedirs("audio_features", exist_ok=True) filename = f"audio_features/{track_uri}" try: with open(filename, "r") as f: contents = f.read() data = json.loads(contents) except FileNotFoundError: features = sp.audio_features([track_uri]) data = features[0] with open(filename, "w") as f: contents = json.dumps(data, indent=4) f.write(contents) return data