Skip to content

Latest commit

 

History

History
182 lines (130 loc) · 6.86 KB

README.pod

File metadata and controls

182 lines (130 loc) · 6.86 KB

NAME

dbus-scrobbler - scrobble from an MPRIS-compliant media player's D-Bus interface

SYNOPSIS

./dbus-scrobbler
Found a running Banshee, connecting…
Now listening…
[2015-08-23 10:35:51] Detected track change.
You need to visit:
http://www.last.fm/api/auth/?api_key=dfab9b1c7357c55028c84b9a8fb68880&token=b4fb0bd84f55ce143885a31756cc859c
Press Enter when you have done so and authorized this app…
[2015-08-23 10:36:18] Successfully updated nowplaying: Pet Shop Boys - Left to My Own Devices.
[2015-08-23 10:36:18] Detected track change.
[2015-08-23 10:36:18] Successfully scrobbled: Pet Shop Boys - Left to My Own Devices.
[2015-08-23 10:40:40] Detected track change.
[2015-08-23 10:40:41] Successfully updated nowplaying: Pet Shop Boys - Where the Streets Have No Name.

DESCRIPTION

I noticed that scrobbling from my media player (Banshee) broke a few weeks ago. Banshee is written in Mono, which I have no idea about, and I'm pretty sure the problem is with Banshee, not Last.fm (although Last.fm did change their site around).

Many modern media players such as Banshee, VLC, Audacity, XMMS2, Spotify, … now support a thing called the Multimedia Player Remote Interfacing Specification (MPRIS). It's a D-Bus interface so that third-party programs can control and inspect the media players in some documented way. I decided to use it to scrobble from. That's what this does.

dbus-scrobbler:

  • Looks on your user session's D-Bus for an existing Banshee process to listen to.

    If it doesn't find one then it waits for one to appear.

  • Asks for authorization with Last.fm the first time this is necessary.

    You'll be asked to visited the Last.fm site to authorize this application. This session key will be cached on the filesystem forever, so you should only need to do this once.

  • Updates the currently playing track on each track change and then scrobbles the previous track.

SESSION KEY AND AUTHORIZATION

Updating the currently-playing track and scrobbling a track both require authorization with Last.fm's API. The first time that dbus-scrobbler is run and has occasion to send an update to Last.fm, it will look for the File::HomeDir::my_home()/.config/dbus-scrobbler/sessionkey file.

If this doesn't exist or is empty then a URL for the user to authorize the app will be printed, and (assuming successful authorization then takes place), the resulting session key will be stored in the file. As this session key is valid for an infinite period of time and allows modification of your Last.fm account you should keep it safe.

If you need to force a re-authorization then you could delete the File::HomeDir::my_home()/.config/dbus-scrobbler/sessionkey file.

TECHNICAL DETAILS

D-Bus Interaction

dbus-scrobbler initially connects to your user's session D-Bus and looks for the bus name org.mpris.Mediaplayer2.banshee. As per the MPRIS specification, each media player should request a unique bus name which begins with org.mpris.MediaPlayer2.

If dbus-scrobbler does not find any such bus name then it assumes that Banshee is not running yet. It connects to the org.freedesktop.DBus.NameOwnerChanged signal and waits for a signal that indicates that org.mpris.Mediaplayer2.banshee is now available.

Once available, the /org/mpris/MediaPlayer2 object is used to connect to the org.freedesktop.DBus.Properties.PropertiesChanged signal. This signal fires when the track changes (and when other events happen) and delivers a hash reference that looks like this:

{
    'Metadata' => {
        'mpris:trackid'     => '/org/bansheeproject/Banshee/Track/11334727',
        'xesam:url'         => 'file:///srv/specialbrew.localnet/srv/tank/Sounds/Songs/Andy/Lossy/Justice%20Vs%20Simian/Playlist_%20Dance/02%20-%20We%20Are%20Your%20Friends%20(Radio%20Edit).mp3',
        'xesam:genre'       => [
            'Pop',
        ],
        'xesam:albumArtist' => [
            'Various Artists',
        ],
        'xesam:album'       => 'Playlist: Dance',
        'mpris:artUrl'      => 'file:///home/andy/.cache/media-art/album-8bbcd5e02f50ce9e5c74c93bdae8b372.jpg',
        'xesam:title'       => 'We Are Your Friends (Radio Edit)',
        'mpris:length'      => 159739000,
        'xesam:trackNumber' => 2,
        'xesam:artist'      => [
            'Justice Vs Simian',
        ],
    },
},

SEE ALSO

Net::LastFMAPI, from which all of the code for interacting with Last.fm's API was stolen.

BUGS

Some of the most egregious shortcomings:

  • Hard-coded to support Banshee.

    Multiple media players support MPRIS, but I don't know enough about D-Bus yet to work out how to wait for them all to appear on the bus, so at the moment this just looks for Banshee. It should be very very simple to alter it to work with another player though.

  • Should run as a daemon.

    It really shouldn't be foreground on the terminal. It should be automatically launched by the user's login process.

  • Should have proper logging.

    Instead of a bunch of crappy prints this should really have logging with multiple priorities, possibly to syslog, and at least with an option to run in foreground and log to stderr.

  • D-Bus stuff could no doubt be better.

    I've knocked this together in one evening because my scrobbling was broken. I didn't know anything about D-Bus before that, other than that it's a message bus. So the chances are I have done some really stupid things.

  • Should use own API credentials.

    Last.fm's API key application page was broken when I was writing this, so I just copied Net::LastFMAPI's credentials. That works fine but I should really use proper credentials once the page works again.

  • Does not retry on transient errors.

    A proper scrobbler would queue scrobbles to disk if they couldn't be submitted, in order to retry them later. At the moment dbus-scrobbler just throws them away.

  • Should be kinder to Last.fm.

    Last.fm's API documentation says that scrobbles should only be submitted after 50% of the track has played, or 4 minutes has elapsed, whichever comes first. There are a couple of situations—especially if you click around in your playlist a lot or keep restarting the player—in which an extra scrobble or two can be submitted.

Please give feedback or issues at the GitHub site, https://github.com/grifferz/dbus-scrobbler.

AUTHOR

Andy Smith <[email protected]>

COPYRIGHT

Copyright © 2015 Andy Smith. All rights reserved. This application is free software. It may be used, redistributed and/or modified under the terms of the Perl Artistic License. Please see the LICENSE file for more details.

Most of the Last.fm API code copied from Steev Eeriumn's Net::LastFMAPI, under the terms of the Perl Artistic License.