concise method names

This commit is contained in:
aj 2020-08-12 09:25:50 +01:00
parent 42d7158f27
commit 41ae299be6
6 changed files with 36 additions and 45 deletions

View File

@ -6,15 +6,15 @@ import logging
logger = logging.getLogger(__name__)
def get_chart_of_spotify_tracks(spotnet: SpotNetwork,
fmnet: FmNetwork,
period: FmNetwork.Range,
limit: int,
username: str = None):
def map_lastfm_track_chart_to_spotify(spotnet: SpotNetwork,
fmnet: FmNetwork,
period: FmNetwork.Range,
limit: int,
username: str = None):
logger.info(f'pulling {period.name} chart')
try:
chart = fmnet.get_top_tracks(period=period, username=username, limit=limit)
chart = fmnet.top_tracks(period=period, username=username or fmnet.username, limit=limit)
spotify_chart = []
for track in chart:
@ -22,7 +22,7 @@ def get_chart_of_spotify_tracks(spotnet: SpotNetwork,
spotify_search = spotnet.search(query_types=[Uri.ObjectType.track],
track=track.name,
artist=track.artist.name,
response_limit=5).tracks
response_limit=1).tracks
if len(spotify_search) > 0:
spotify_chart.append(spotify_search[0])
else:

View File

@ -5,7 +5,7 @@ from spotframework.model.uri import Uri
from spotframework.engine.playlistengine import TrackSource, SourceParameter
from spotframework.engine.processor.abstract import AbstractProcessor
from spotfm.chart import get_chart_of_spotify_tracks
from spotfm.chart import map_lastfm_track_chart_to_spotify
from typing import List
import logging
@ -34,9 +34,9 @@ class ChartSource(TrackSource):
# TODO add processor support?
try:
return get_chart_of_spotify_tracks(spotnet=self.net,
fmnet=self.fmnet,
period=params.chart_range,
limit=params.limit)
return map_lastfm_track_chart_to_spotify(spotnet=self.net,
fmnet=self.fmnet,
period=params.chart_range,
limit=params.limit)
except LastFMNetworkException:
logger.exception(f'error occured during chart retrieval')

View File

@ -44,7 +44,7 @@ class Counter:
if playlist is not None:
if playlist.has_tracks() is False:
try:
playlist.tracks = self.spotnet.get_playlist_tracks(uri=playlist.uri)
playlist.tracks = self.spotnet.playlist_tracks(uri=playlist.uri)
except SpotifyNetworkException:
logger.exception(f'error occured during playlist track retrieval')
return 0
@ -53,7 +53,7 @@ class Counter:
if uri.object_type != Uri.ObjectType.playlist:
raise ValueError('uri not a playlist')
try:
playlist = self.spotnet.get_playlist(uri=uri, tracks=True)
playlist = self.spotnet.playlist(uri=uri, tracks=True)
except SpotifyNetworkException:
logger.exception(f'error occured during playlist retrieval')
return 0
@ -91,15 +91,15 @@ class Counter:
if uri.object_type != Uri.ObjectType.track:
raise ValueError('uri not a track')
try:
track = self.spotnet.get_track(uri=uri)
track = self.spotnet.track(uri=uri)
except SpotifyNetworkException:
logger.exception(f'error occured during track retrieval')
return 0
try:
fmtrack = self.fmnet.get_track(name=track.name,
artist=track.artists[0].name,
username=username or self.fmnet.username)
fmtrack = self.fmnet.track(name=track.name,
artist=track.artists[0].name,
username=username or self.fmnet.username)
if fmtrack is not None:
return fmtrack.user_scrobbles
else:
@ -118,15 +118,15 @@ class Counter:
if uri.object_type != Uri.ObjectType.album:
raise ValueError('uri not an album')
try:
album = self.spotnet.get_album(uri=uri)
album = self.spotnet.album(uri=uri)
except SpotifyNetworkException:
logger.exception(f'error occured during album retrieval')
return 0
try:
fmalbum = self.fmnet.get_album(name=album.name,
artist=album.artists[0].name,
username=username or self.fmnet.username)
fmalbum = self.fmnet.album(name=album.name,
artist=album.artists[0].name,
username=username or self.fmnet.username)
if fmalbum is not None:
return fmalbum.user_scrobbles
else:
@ -145,13 +145,13 @@ class Counter:
if uri.object_type != Uri.ObjectType.artist:
raise ValueError('uri not an artist')
try:
artist = self.spotnet.get_artist(uri=uri)
artist = self.spotnet.artist(uri=uri)
except SpotifyNetworkException:
logger.exception(f'error occured during artist retrieval')
return 0
try:
fmartist = self.fmnet.get_artist(name=artist.name, username=username or self.fmnet.username)
fmartist = self.fmnet.artist(name=artist.name, username=username or self.fmnet.username)
if fmartist is not None:
return fmartist.user_scrobbles
else:

View File

@ -30,7 +30,7 @@ class Stats(Cmd):
in_string = input('uri group/uri>')
try:
user_total = self.fmnet.get_user_scrobble_count()
user_total = self.fmnet.user_scrobble_count()
except LastFMNetworkException:
logger.exception(f'error occured during scrobble count retrieval')
user_total = 0

View File

@ -61,8 +61,8 @@ def time_artist(spotnet: SpotNet, artist: str, username: str, fmnet: FmNet = Non
return_tracks: bool = False) -> int:
logger.info(f'timing {artist} for {username}')
fmtracks = LibraryScraper.get_scrobbled_tracks(username=username, artist=artist, whole_track=False,
from_date=from_date, to_date=to_date, date_preset=date_preset)
fmtracks = LibraryScraper.artist_tracks(username=username, artist=artist, whole_track=False,
from_date=from_date, to_date=to_date, date_preset=date_preset)
return time_track_collection(tracks=fmtracks, spotnet=spotnet, username=username, fmnet=fmnet,
return_tracks=return_tracks)
@ -73,8 +73,8 @@ def time_album(spotnet: SpotNet, artist: str, album: str, username: str, fmnet:
return_tracks: bool = False) -> int:
logger.info(f'timing {album} / {artist} for {username}')
fmtracks = LibraryScraper.get_albums_tracks(username=username, artist=artist, album=album, whole_track=False,
from_date=from_date, to_date=to_date, date_preset=date_preset)
fmtracks = LibraryScraper.album_tracks(username=username, artist=artist, album=album, whole_track=False,
from_date=from_date, to_date=to_date, date_preset=date_preset)
return time_track_collection(tracks=fmtracks, spotnet=spotnet, username=username, fmnet=fmnet,
return_tracks=return_tracks)
@ -85,8 +85,8 @@ def time_track(spotnet: SpotNet, artist: str, track: str, username: str, fmnet:
return_tracks: bool = False) -> int:
logger.info(f'timing {track} / {artist} for {username}')
fmtracks = LibraryScraper.get_track_scrobbles(username=username, artist=artist, track=track, whole_track=False,
from_date=from_date, to_date=to_date, date_preset=date_preset)
fmtracks = LibraryScraper.track_scrobbles(username=username, artist=artist, track=track, whole_track=False,
from_date=from_date, to_date=to_date, date_preset=date_preset)
if fmtracks is not None:
if len(fmtracks) == 0:
return 0
@ -112,7 +112,7 @@ def time_track_collection(tracks, spotnet: SpotNet, username: str, fmnet: FmNet
logger.error(f'no track returned for search {track.name} / {track.artist.name} / {username}'
f', pulling last.fm track')
fmtrack = fmnet.get_track(name=track.name, artist=track.artist.name, username=username)
fmtrack = fmnet.track(name=track.name, artist=track.artist.name, username=username)
if fmtrack is not None and fmtrack.duration is not None and fmtrack.duration > 0:
track_pairs.append((track, fmtrack))

View File

@ -1,6 +1,6 @@
from spotframework.net.network import Network as Spotnet, NetworkUser
from fmframework.net.network import Network as Fmnet
from spotfm.timer import time_artist
from spotfm.timer import time, seconds_to_time_str
import logging
import os
@ -44,20 +44,11 @@ while len(fmuser) == 0:
fmnet = Fmnet(username=fmuser, api_key=fmclient)
def convert(seconds):
seconds = seconds % (24 * 3600)
hour = seconds // 3600
seconds %= 3600
minutes = seconds // 60
seconds %= 60
return "%d:%02d:%02d" % (hour, minutes, seconds)
top_artists = fmnet.get_top_artists(period=Fmnet.Range.OVERALL, limit=10)
top_artists = fmnet.top_artists(period=Fmnet.Range.OVERALL, limit=10)
artist_counts = dict()
for artist in top_artists:
artist_counts[artist.name] = time_artist(spotnet=spotnet, fmnet=fmnet, artist=artist.name, username=fmnet.username)
artist_counts[artist.name] = time(spotnet=spotnet, fmnet=fmnet, artist=artist.name, username=fmnet.username)
for name, count in artist_counts.items():
print(name, f'{count}ms,', f'{count/1000}s,', convert(count/1000))
print(name, f'{count}ms,', f'{count/1000}s,', seconds_to_time_str(milliseconds=count))