initial commit with counter and count playlist function

This commit is contained in:
aj 2019-10-03 21:12:43 +01:00
commit 2528af06b1
5 changed files with 78 additions and 0 deletions

9
.gitignore vendored Normal file
View File

@ -0,0 +1,9 @@
venv
env
__pycache__
*.csv
.idea
.spot
.fm
scratch.py

4
README.md Normal file
View File

@ -0,0 +1,4 @@
spotfm
=============
utility functions sitting on top of [spotframework](https://github.com/Sarsoo/spotframework) and [fmframework](https://github.com/Sarsoo/pyspotframework)

4
spotfm/__init__.py Normal file
View File

@ -0,0 +1,4 @@
import logging
logger = logging.getLogger(__name__)
logger.setLevel('DEBUG')

0
spotfm/maths/__init__.py Normal file
View File

61
spotfm/maths/counter.py Normal file
View File

@ -0,0 +1,61 @@
from spotframework.net.network import Network as SpotifyNetwork
from spotframework.model.playlist import SpotifyPlaylist
from spotframework.model.track import SpotifyTrack
from spotframework.model.uri import Uri
from fmframework.net.network import Network as FMNetwork
import logging
logger = logging.getLogger(__name__)
class Counter:
def __init__(self,
spotnet: SpotifyNetwork,
fmnet: FMNetwork):
self.spotnet = spotnet
self.fmnet = fmnet
def count(self, uri: Uri):
if uri.object_type == Uri.ObjectType.playlist:
return self.count_playlist(uri=uri)
else:
logger.error('cannot process uri')
def count_playlist(self, username: str = None, uri: Uri = None, playlist: SpotifyPlaylist = None):
if uri is None and playlist is None:
raise ValueError('no input playlist to count')
if playlist is not None:
if playlist.has_tracks() is False:
playlist.tracks = self.spotnet.get_playlist_tracks(uri=playlist.uri)
if uri is not None:
playlist = self.spotnet.get_playlist(uri=uri)
scrobble_count = 0
tracks = []
for song in playlist.tracks:
if isinstance(song, SpotifyTrack):
if song.uri not in [i.uri for i in tracks]:
tracks.append(song)
for song in tracks:
if username is not None:
fm_track = self.fmnet.get_track(name=song.name,
artist=song.artists[0].name,
username=username)
else:
fm_track = self.fmnet.get_track(name=song.name,
artist=song.artists[0].name,
username=self.fmnet.username)
if fm_track:
scrobble_count += fm_track.user_scrobbles
else:
logger.error(f'no last.fm track returned for {song}')
return scrobble_count