Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scrobble_filter to provide a configurable list of URI schemes not… #29

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ found at ``~/.config/mopidy/mopidy.conf``::
[scrobbler]
username = alice
password = secret
backend_blacklist = list

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's worth considering a word other than "blacklist". How about "ignorelist"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree.

We also have some "ignore"/"exclude" lists in the config of other extensions, like Mopidy-Local and Mopidy-Files. I'd like this to be aligned with the naming there.


The following configuration values are available:

- ``scrobbler/enabled``: If the scrobbler extension should be enabled or not.
- ``scrobbler/username``: Your Last.fm username.
- ``scrobbler/password``: Your Last.fm password.
- ``scrobbler/backend_blacklist``: List of uri schemes to not scrobble - eg spotify


Project resources
Expand Down
1 change: 1 addition & 0 deletions mopidy_scrobbler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def get_config_schema(self):
schema = super(Extension, self).get_config_schema()
schema['username'] = config.String()
schema['password'] = config.Secret()
schema['backend_blacklist'] = config.List(optional=True)
return schema

def setup(self, registry):
Expand Down
1 change: 1 addition & 0 deletions mopidy_scrobbler/ext.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
enabled = true
username =
password =
backend_blacklist =

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you're missing a newline at the end of the file here.

12 changes: 12 additions & 0 deletions mopidy_scrobbler/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ def __init__(self, config, core):
self.lastfm = None
self.last_start_time = None

def check_uri_scheme(self, uri):
uri_scheme = uri.split(':')[0]
if uri_scheme in self.config['scrobbler']['backend_blacklist']:
logger.info('Not scrobbling track from %s', uri_scheme)
return True
else:
return False

def on_start(self):
try:
self.lastfm = pylast.LastFMNetwork(
Expand All @@ -43,6 +51,8 @@ def on_start(self):

def track_playback_started(self, tl_track):
track = tl_track.track
if self.check_uri_scheme(track.uri):
return
artists = ', '.join(sorted([a.name for a in track.artists]))
duration = track.length and track.length // 1000 or 0
self.last_start_time = int(time.time())
Expand All @@ -60,6 +70,8 @@ def track_playback_started(self, tl_track):

def track_playback_ended(self, tl_track, time_position):
track = tl_track.track
if self.check_uri_scheme(track.uri):
return
artists = ', '.join(sorted([a.name for a in track.artists]))
duration = track.length and track.length // 1000 or 0
time_position = time_position // 1000
Expand Down
2 changes: 2 additions & 0 deletions tests/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def test_get_default_config(self):
self.assertIn('enabled = true', config)
self.assertIn('username =', config)
self.assertIn('password =', config)
self.assertIn('backend_blacklist', config)

def test_get_config_schema(self):
ext = Extension()
Expand All @@ -24,6 +25,7 @@ def test_get_config_schema(self):

self.assertIn('username', schema)
self.assertIn('password', schema)
self.assertIn('backend_blacklist', schema)

def test_setup(self):
ext = Extension()
Expand Down
24 changes: 18 additions & 6 deletions tests/test_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def setUp(self):
'scrobbler': {
'username': 'alice',
'password': 'secret',
'backend_blacklist': ['spotify']
}
}
self.frontend = frontend_lib.ScrobblerFrontend(
Expand Down Expand Up @@ -48,6 +49,7 @@ def test_track_playback_started_updates_now_playing(self, pylast_mock):
artists = [models.Artist(name='ABC'), models.Artist(name='XYZ')]
album = models.Album(name='The Collection')
track = models.Track(
uri='local:track:1234567890',
name='One Two Three',
artists=artists,
album=album,
Expand All @@ -68,7 +70,7 @@ def test_track_playback_started_updates_now_playing(self, pylast_mock):

def test_track_playback_started_has_default_values(self, pylast_mock):
self.frontend.lastfm = mock.Mock(spec=pylast.LastFMNetwork)
track = models.Track()
track = models.Track(uri='local:track:1234567890')
tl_track = models.TlTrack(track=track, tlid=17)

self.frontend.track_playback_started(tl_track)
Expand All @@ -86,7 +88,7 @@ def test_track_playback_started_catches_pylast_error(self, pylast_mock):
pylast_mock.NetworkError = pylast.NetworkError
self.frontend.lastfm.update_now_playing.side_effect = (
pylast.NetworkError(None, 'foo'))
track = models.Track()
track = models.Track(uri='local:track:1234567890')
tl_track = models.TlTrack(track=track, tlid=17)

self.frontend.track_playback_started(tl_track)
Expand All @@ -97,6 +99,7 @@ def test_track_playback_ended_scrobbles_played_track(self, pylast_mock):
artists = [models.Artist(name='ABC'), models.Artist(name='XYZ')]
album = models.Album(name='The Collection')
track = models.Track(
uri='local:track:1234567890',
name='One Two Three',
artists=artists,
album=album,
Expand All @@ -119,7 +122,7 @@ def test_track_playback_ended_scrobbles_played_track(self, pylast_mock):
def test_track_playback_ended_has_default_values(self, pylast_mock):
self.frontend.last_start_time = 123
self.frontend.lastfm = mock.Mock(spec=pylast.LastFMNetwork)
track = models.Track(length=180432)
track = models.Track(length=180432, uri='local:track:1234567890')
tl_track = models.TlTrack(track=track, tlid=17)

self.frontend.track_playback_ended(tl_track, 150000)
Expand All @@ -135,7 +138,7 @@ def test_track_playback_ended_has_default_values(self, pylast_mock):

def test_does_not_scrobble_tracks_shorter_than_30_sec(self, pylast_mock):
self.frontend.lastfm = mock.Mock(spec=pylast.LastFMNetwork)
track = models.Track(length=20432)
track = models.Track(length=20432, uri='local:track:1234567890')
tl_track = models.TlTrack(track=track, tlid=17)

self.frontend.track_playback_ended(tl_track, 20432)
Expand All @@ -144,7 +147,7 @@ def test_does_not_scrobble_tracks_shorter_than_30_sec(self, pylast_mock):

def test_does_not_scrobble_if_played_less_than_half(self, pylast_mock):
self.frontend.lastfm = mock.Mock(spec=pylast.LastFMNetwork)
track = models.Track(length=180432)
track = models.Track(length=180432, uri='local:track:1234567890')
tl_track = models.TlTrack(track=track, tlid=17)

self.frontend.track_playback_ended(tl_track, 60432)
Expand All @@ -153,13 +156,22 @@ def test_does_not_scrobble_if_played_less_than_half(self, pylast_mock):

def test_does_scrobble_if_played_not_half_but_240_sec(self, pylast_mock):
self.frontend.lastfm = mock.Mock(spec=pylast.LastFMNetwork)
track = models.Track(length=880432)
track = models.Track(length=880432, uri='local:track:1234567890')
tl_track = models.TlTrack(track=track, tlid=17)

self.frontend.track_playback_ended(tl_track, 241432)

self.assertEqual(self.frontend.lastfm.scrobble.call_count, 1)

def test_does_not_scrobble_if_uri_scheme_filtered(self, pylast_mock):
self.frontend.lastfm = mock.Mock(spec=pylast.LastFMNetwork)
track = models.Track(length=880432, uri='spotify:track:1234567890')
tl_track = models.TlTrack(track=track, tlid=17)

self.frontend.track_playback_ended(tl_track, 241432)

self.assertEqual(self.frontend.lastfm.scrobble.call_count, 0)

def test_track_playback_ended_catches_pylast_error(self, pylast_mock):
self.frontend.lastfm = mock.Mock(spec=pylast.LastFMNetwork)
pylast_mock.NetworkError = pylast.NetworkError
Expand Down