189 lines
5.5 KiB
Python
189 lines
5.5 KiB
Python
from DataBase import *
|
|
from SpotifyWebAPI import fetch_data
|
|
import cmd
|
|
|
|
playlists = get_data('playlistTracks')
|
|
top_artists = get_data('top_artists')
|
|
top_tracks = get_data('top_tracks')
|
|
user_tracks = get_data('tracks')
|
|
link_patterns = [get_data('link_pattern_tags'), get_data('link_pattern_tags'), get_data('link_pattern_tags')]
|
|
local_library = get_data("local_library")
|
|
user_tracks_by_id = {track['track']['id']: track['track'] for track in user_tracks}
|
|
|
|
|
|
def get_playlist_names(pl):
|
|
return [name for name, items in pl.items()]
|
|
|
|
|
|
def print_playlist_tracks(pl_id: int):
|
|
pls = list(playlists)
|
|
name = pls[pl_id]
|
|
pl = playlists[name]
|
|
print(name)
|
|
for track in pl:
|
|
track_data = track['track']
|
|
track_name = track_data['name']
|
|
artists = get_artists_str(track_data['artists'])
|
|
print(f" '{track_name}' - '{artists}' ")
|
|
|
|
|
|
def print_top_artists():
|
|
for artist in top_artists:
|
|
name = artist['name']
|
|
print(f" '{name}'")
|
|
|
|
|
|
def print_top_tracks():
|
|
for track in top_tracks:
|
|
track_name = track['name']
|
|
artists = get_artists_str(track['artists'])
|
|
print(f" '{track_name}' - '{artists}' ")
|
|
|
|
|
|
def print_stats():
|
|
print(f" tracks - {len(user_tracks)}")
|
|
print(f" playlists - {len(playlists)}")
|
|
|
|
for name, tracks in playlists.items():
|
|
print(f" '{name}' - {len(tracks)}")
|
|
|
|
print(f" top tracks - {len(top_tracks)}")
|
|
print(f" top artists - {len(top_artists)}")
|
|
|
|
|
|
def print_tracks():
|
|
track_idx = 0
|
|
for trackItem in user_tracks:
|
|
track = trackItem['track']
|
|
print(f" {track_idx}: '{track['name']}' by '{get_artists_str(track['artists'])}'")
|
|
track_idx += 1
|
|
|
|
|
|
def sort_remote_tracks_by_popularity(track_ids):
|
|
out = []
|
|
for top_track in top_tracks:
|
|
if top_track['id'] in track_ids:
|
|
out.append(top_track['id'])
|
|
return out
|
|
|
|
|
|
def print_link_stats(link_pattern):
|
|
resolved_reversed = {}
|
|
resolved = []
|
|
unresolved_local = []
|
|
unresolved_remote = []
|
|
|
|
for local_id, links in link_pattern.items():
|
|
if len(links['items']):
|
|
resolved.append(local_id)
|
|
resolved_reversed[links['items'][0]] = local_id
|
|
else:
|
|
unresolved_local.append(local_id)
|
|
|
|
for track in user_tracks:
|
|
track_id = track['track']['id']
|
|
if track_id not in resolved_reversed:
|
|
unresolved_remote.append(track_id)
|
|
|
|
print(f"Local Tracks: {len(local_library)}")
|
|
print(f"Remote Tracks: {len(user_tracks)}")
|
|
|
|
unresolved_remote = sort_remote_tracks_by_popularity(unresolved_remote)
|
|
unresolved_remote = unresolved_remote[0:min(len(unresolved_remote), 100)]
|
|
|
|
print(f"\nUnresolved tracks from the remote: {len(unresolved_remote)}")
|
|
index = 0
|
|
for remote_id in unresolved_remote:
|
|
remote_track = user_tracks_by_id[remote_id]
|
|
print(f" {index}: '{remote_track['name']}' by '{get_artists_str(remote_track['artists'])}'")
|
|
index += 1
|
|
|
|
print(f"\nResolved tracks: {len(resolved)}")
|
|
index = 0
|
|
for link in resolved:
|
|
remote_id = link_pattern[link]['items'][0]
|
|
local_track = local_library[link]
|
|
remote_track = user_tracks_by_id[remote_id]
|
|
|
|
print(f" {index}: '{local_track['name']}' by '{local_track['artist']}'", end=' ----> ')
|
|
print(f"'{remote_track['name']}' by '{get_artists_str(remote_track['artists'])}'")
|
|
index += 1
|
|
|
|
return
|
|
print(f"\nUnresolved tracks from the local: {len(unresolved_local)}")
|
|
index = 0
|
|
for link in unresolved_local:
|
|
local_track = local_library[link]
|
|
print(f" {index}: '{local_track['name']}' by '{local_track['artist']}'")
|
|
index += 1
|
|
|
|
|
|
class Interpreter(cmd.Cmd):
|
|
intro = "Welcome to the interactive command loop. Type help or ? to list commands.\n"
|
|
prompt = "(spotify) "
|
|
|
|
def do_link_patterns(self, arg):
|
|
"""prints available linking patterns"""
|
|
print(f"0 - fuzzy tags")
|
|
print(f"1 - spotify")
|
|
print(f"2 - fuzzy")
|
|
|
|
def do_link_pattern(self, arg):
|
|
"""prints available linking patterns"""
|
|
pattern_id = int(arg.split()[0])
|
|
print_link_stats(link_patterns[pattern_id])
|
|
|
|
def do_playlists(self, arg):
|
|
"""prints user playlists"""
|
|
pl_id = 0
|
|
for pl in get_playlist_names(playlists):
|
|
print(f"{pl_id} - {pl}")
|
|
pl_id += 1
|
|
|
|
def do_playlist_tracks(self, arg):
|
|
"""prints playlist [name]"""
|
|
try:
|
|
pl_id = int(arg.split()[0])
|
|
print_playlist_tracks(pl_id)
|
|
except IndexError:
|
|
print("Invalid input")
|
|
|
|
def do_top_artists(self, arg):
|
|
"""Top artists"""
|
|
print_top_artists()
|
|
|
|
def do_top_tracks(self, arg):
|
|
"""Top tracks"""
|
|
print_top_tracks()
|
|
|
|
def do_stat(self, arg):
|
|
"""Print stats"""
|
|
print_stats()
|
|
|
|
def do_tracks(self, arg):
|
|
"""Print tracks"""
|
|
print_tracks()
|
|
|
|
def do_fetch(self, arg):
|
|
"""Fetch all spotify data"""
|
|
try:
|
|
fetch_data()
|
|
|
|
global playlists, top_artists, top_tracks, user_tracks
|
|
|
|
playlists = get_data('playlistTracks')
|
|
top_artists = get_data('top_artists')
|
|
top_tracks = get_data('top_tracks')
|
|
user_tracks = get_data('tracks')
|
|
|
|
except ValueError:
|
|
print("Can not fetch the data.")
|
|
|
|
def do_exit(self, arg):
|
|
"""Exit the command loop: exit"""
|
|
print("Goodbye!")
|
|
return True
|
|
|
|
|
|
if __name__ == '__main__':
|
|
Interpreter().cmdloop()
|