2021-02-01 01:37:22 +00:00
|
|
|
import os
|
|
|
|
from typing import List
|
|
|
|
|
|
|
|
import pandas as pd
|
|
|
|
|
|
|
|
from spotframework.model.track import PlaylistTrack
|
|
|
|
from spotframework.net.network import Network as SpotNet, NetworkUser
|
2021-02-01 21:43:27 +00:00
|
|
|
from fmframework.net.network import Network as FMNet
|
2021-02-01 01:37:22 +00:00
|
|
|
|
|
|
|
def get_spotnet():
|
|
|
|
return SpotNet(NetworkUser(client_id=os.environ['SPOT_CLIENT'],
|
|
|
|
client_secret=os.environ['SPOT_SECRET'],
|
|
|
|
refresh_token=os.environ['SPOT_REFRESH'])).refresh_access_token()
|
|
|
|
|
2021-02-01 21:43:27 +00:00
|
|
|
def get_fmnet():
|
|
|
|
return FMNet(username='sarsoo', api_key=os.environ['FM_CLIENT'])
|
|
|
|
|
2021-02-03 16:08:06 +00:00
|
|
|
playlist_cache = dict() # low-tech caches for repeated pulling
|
|
|
|
all_playlists = list()
|
2021-02-01 01:37:22 +00:00
|
|
|
def get_playlist(name: str, spotnet: SpotNet):
|
2021-02-03 16:08:06 +00:00
|
|
|
global all_playlists
|
|
|
|
try:
|
|
|
|
return playlist_cache[name]
|
|
|
|
except KeyError:
|
|
|
|
if len(all_playlists) == 0:
|
|
|
|
all_playlists = spotnet.playlists()
|
|
|
|
playlist = [i for i in all_playlists if i.name == name][0]
|
|
|
|
playlist.tracks = spotnet.playlist_tracks(uri=playlist.uri)
|
|
|
|
playlist_cache[name] = playlist
|
|
|
|
return playlist
|
2021-02-01 01:37:22 +00:00
|
|
|
|
|
|
|
def track_frame(tracks: List[PlaylistTrack]):
|
|
|
|
return pd.DataFrame(
|
|
|
|
[
|
|
|
|
[i.track.name, i.track.artists[0].name]
|
|
|
|
for i in tracks
|
|
|
|
],
|
|
|
|
columns = ["track", "artist"]
|
|
|
|
)
|