added stats console app

This commit is contained in:
aj 2019-12-20 00:35:18 +00:00
parent 2f44c3b655
commit 517accec38
3 changed files with 179 additions and 0 deletions

57
config/uri_groups.json Normal file
View File

@ -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"
}
]
}
]

73
spotfm/maths/stats.py Normal file
View File

@ -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)

49
stats_cmd.py Normal file
View File

@ -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()