From 517accec38d8c4a55e1374d75d6817a5af5859b5 Mon Sep 17 00:00:00 2001 From: aj Date: Fri, 20 Dec 2019 00:35:18 +0000 Subject: [PATCH] added stats console app --- config/uri_groups.json | 57 +++++++++++++++++++++++++++++++++ spotfm/maths/stats.py | 73 ++++++++++++++++++++++++++++++++++++++++++ stats_cmd.py | 49 ++++++++++++++++++++++++++++ 3 files changed, 179 insertions(+) create mode 100644 config/uri_groups.json create mode 100644 spotfm/maths/stats.py create mode 100644 stats_cmd.py diff --git a/config/uri_groups.json b/config/uri_groups.json new file mode 100644 index 0000000..174e377 --- /dev/null +++ b/config/uri_groups.json @@ -0,0 +1,57 @@ +[ + { + "name": "surgical summer", + "members": [ + { + "name": "daytona", + "uri": "spotify:album:07bIdDDe3I3hhWpxU6tuBp" + }, + { + "name": "ye", + "uri": "spotify:album:2Ek1q2haOnxVqhvVKqMvJe" + }, + { + "name": "ksg", + "uri": "spotify:album:6pwuKxMUkNg673KETsXPUV" + }, + { + "name": "nasir", + "uri": "spotify:album:66EwBbt2kPgugo8Wz0SKAw" + }, + { + "name": "k.t.s.e", + "uri": "spotify:album:0mwf6u9KVhZDCNVyIi6JuU" + } + ] + }, + { + "name": "madlib", + "members": [ + { + "name": "pinata", + "uri": "spotify:album:43uErencdmuTRFZPG3zXL1" + }, + { + "name": "bandana - single", + "uri": "spotify:album:5Bc89b9FsoOrEZf0HLS812" + }, + { + "name": "bandana", + "uri": "spotify:album:31KbO7WnDp2AjPdmRTJzdf" + } + ] + }, + { + "name": "freddie", + "members": [ + { + "name": "freddie", + "uri": "spotify:artist:0Y4inQK6OespitzD6ijMwb" + }, + { + "name": "fetti", + "uri": "spotify:album:3JgtFZroTUGoklTtb2xOne" + } + ] + } +] \ No newline at end of file diff --git a/spotfm/maths/stats.py b/spotfm/maths/stats.py new file mode 100644 index 0000000..c70f44a --- /dev/null +++ b/spotfm/maths/stats.py @@ -0,0 +1,73 @@ +from cmd import Cmd +import logging +import json + +from spotframework.net.network import Network as Spotnet +from spotframework.engine.playlistengine import PlaylistEngine +from spotframework.model.uri import Uri +from fmframework.net.network import Network as Fmnet +from spotfm.maths.counter import Counter + +logger = logging.getLogger(__name__) + + +class Stats(Cmd): + intro = 'Stats... ? for help' + prompt = '(stats)> ' + + def __init__(self, spotnet: Spotnet, fmnet: Fmnet): + Cmd.__init__(self) + self.spotnet = spotnet + self.fmnet = fmnet + self.counter = Counter(spotnet=spotnet, fmnet=fmnet) + + def do_count(self, arg): + """count spotify uri on last.fm""" + + in_string = arg + + if in_string is None or len(in_string) < 2: + in_string = input('uri group/uri>') + + user_total = self.fmnet.get_user_scrobble_count() + + total = 0 + try: + uri = Uri(in_string) + total = self.counter.count(uri) + except ValueError: + with open('config/uri_groups.json', 'r') as file_obj: + groups = json.load(file_obj) + + group = next((i for i in groups if i['name'] == in_string), None) + if group is None: + print('group not found') + return + + counts = dict() + for member in group['members']: + try: + uri_obj = Uri(member['uri']) + iter_count = self.counter.count(uri_obj) + + counts.update({member['name']: iter_count}) + total += iter_count + except ValueError: + print(f'malformed uri {uri_obj}') + + [print(f'{name} -> {count:,} scrobbles ({round((count*100)/total)}%)') for name, count in counts.items()] + + print(f'{in_string} -> {total:,} scrobbles ({round((total*100)/user_total, 2)}%)') + + def do_sort(self, arg): + + in_str = arg + + if in_str is None or len(arg) == 0: + in_str = input('playlist>') + + if in_str is None or in_str == '': + return + + engine = PlaylistEngine(self.spotnet) + engine.reorder_playlist_by_added_date(in_str) diff --git a/stats_cmd.py b/stats_cmd.py new file mode 100644 index 0000000..b3ccf80 --- /dev/null +++ b/stats_cmd.py @@ -0,0 +1,49 @@ +from spotfm.maths.stats import Stats + +from spotframework.net.network import Network as Spotnet, NetworkUser +from fmframework.net.network import Network as Fmnet + +import logging +import os + +spotframework_logger = logging.getLogger('spotframework') +fmframework_logger = logging.getLogger('fmframework') +spotfm_logger = logging.getLogger('spotfm') + +log_format = '%(levelname)s %(name)s:%(funcName)s - %(message)s' +formatter = logging.Formatter(log_format) + +stream_handler = logging.StreamHandler() +stream_handler.setFormatter(formatter) + +# spotframework_logger.addHandler(stream_handler) +# fmframework_logger.addHandler(stream_handler) +# spotfm_logger.addHandler(stream_handler) + +spot_client = os.environ.get('SPOT_CLIENT') +spot_secret = os.environ.get('SPOT_SECRET') +spot_access = os.environ.get('SPOT_ACCESS_TOKEN') +spot_refresh = os.environ.get('SPOT_REFRESH_TOKEN') +fmclient = os.environ.get('FM_CLIENT') +fmuser = os.environ.get('FM_USER') + +if spot_access is None and spot_refresh is None: + print('no spotify credentials') + exit(0) + +if fmclient is None: + print('no last.fm credentials') + exit(0) + +spotnet = Spotnet(NetworkUser(client_id=spot_client, + client_secret=spot_secret, + access_token=spot_access, + refresh_token=spot_refresh).refresh_access_token()) + +while len(fmuser) == 0: + fmuser = input('last.fm username> ') + +fmnet = Fmnet(username=fmuser, api_key=fmclient) + +prompt = Stats(spotnet=spotnet, fmnet=fmnet) +prompt.cmdloop()