Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 21

Thread: A bash script to control spotify for linux via dbus

  1. #11
    Join Date
    Apr 2007
    Location
    linköping, sweden
    Beans
    119
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: A bash script to control spotify for linux via dbus

    As an exercise I translated the script from bash to perl.

    It is somewhat nicer in perl imho, so here is that version aswell:
    Code:
    #!/usr/bin/perl
    
    use 5.010;
    use strict;
    use warnings;
    use File::Basename;
    use Net::DBus;
    
      # Figure out some dbus stuff
      unless ( defined $ENV{'DBUS_SESSION_BUS_ADDRESS'} ) { 
    	&set_DBUS_SESSION_BUS_ADDRESS;
    	#die "Don't know which dbus to attach to.\nMake sure environment variable DBUS_SESSION_BUS_ADDRESS is set.";
    	}
      #my $bus = 		Net::DBus->find;
      my $bus = 		Net::DBus->session;
      my $spotify =		$bus->get_service("org.mpris.MediaPlayer2.spotify");
      my $player =		$spotify->get_object("/org/mpris/MediaPlayer2", "org.mpris.MediaPlayer2.Player");
      my $application =	$spotify->get_object("/org/mpris/MediaPlayer2", "org.mpris.MediaPlayer2");
      my $getorset =	$spotify->get_object("/org/mpris/MediaPlayer2", "org.freedesktop.DBus.Properties");
    
    # Handle command line argument
    if (scalar @ARGV == 0) { &print_help; }
    given ($ARGV[0]) {
    #	when ('play') { 	$player->Play(); }		#Does not work for some reason.
    	when ('pause') { 	$player->Pause(); }
    	when ('playpause') { 	$player->PlayPause(); }
    	when ('next') { 	$player->Next(); }
    	when ('previous') { 	$player->Previous(); }
    	when ('stop') { 	$player->Stop(); }
    	when ('playstatus') { print $getorset->Get("org.mpris.MediaPlayer2.Player", "PlaybackStatus") . "\n"; }
    	when (m/\bspotify:(artist|album|track):[0-9a-zA-z]{22}\b/) { $player->OpenUri($_); }
    	when ('metadata-debug') { &print_debug_metadata; }
    	default { &print_help; }
    }
    	
    
    # Print the help text
    sub print_help {
    	print "USAGE: " . basename($0) . " {pause|playpause|next|previous|stop|playstatus|metadata-debug|<spotify URI>}\n\n";
    	print "\t" . "pause"		. "\t\t"	. "Causes spotify to pause current playback."		. "\n";
    	print "\t" . "playpause"	. "\t"		. "Causes spotify to pause or play current playback."	. "\n";
    	print "\t" . "next"		. "\t\t"	. "Goes to next song."					. "\n";
    	print "\t" . "previous"		. "\t"		. "Goes to previous song."				. "\n";
    	print "\t" . "stop"		. "\t\t"	. "Stops playback."					. "\n";
    	print "\t" . "playstatus"	. "\t"		. "Prints current playstatus (Playing/Paused)."		. "\n";
    	print "\t" . "<spotify URI>"	. "\t"		. "Starts playing supplied URI."			. "\n";
    	print "\t" . "metadata-debug"	. "\t"		. "Shows available data on currently playing song."	. "\n";
    	print "\t" 			. "\t\t"	. "Fairly unformatted, thus \"debug\" data."		. "\n";
    	print 													  "\n";
    	print "EXAMPLES:\t"	. basename($0) . " playpause" 							. "\n";
    	print "\t\t" 		. basename($0) . " spotify:track:5XXAq1r5r73ZyBS0XAiGw0"			. "\n";
    	
    	exit;
    }
    
    # Print some raw metadata
    sub print_debug_metadata {
    	# Dereference the metadata hashref by copying it to a local hash
    	my %metadata = %{ $getorset->Get("org.mpris.MediaPlayer2.Player", "Metadata") };
    
    	# Print all metadata
    	print "Now Playing:\n";
    	for (keys %metadata) {
    		print $_ . ":\t" . $metadata{$_} . "\n" unless ($_ eq 'xesam:artist');
    	}
    
    	# Dereference the artist arrayref by copying it to local array
    	my @artistarray = @{ $metadata{'xesam:artist'} };
    
    	# Print all artists.
    	foreach my $artist (@artistarray) {
    		print "artist: \t" . $artist . "\n";
    	}
    }
    
    sub set_DBUS_SESSION_BUS_ADDRESS {
    	my $curruser	= `whoami`; chomp $curruser;
    	my $procname	= 'spotify';
    	my $pid		= `pgrep -o -u $curruser $procname`; chomp $pid;
    	my $environ	= '/proc/' . $pid . '/environ';
    	my $dbussession = `grep -z DBUS_SESSION_BUS_ADDRESS $environ`; $dbussession =~ s/^DBUS_SESSION_BUS_ADDRESS=//;
    
    	$ENV{'DBUS_SESSION_BUS_ADDRESS'} = $dbussession;
    }
    Attached Files Attached Files

  2. #12
    Join Date
    Dec 2011
    Beans
    3

    Re: A bash script to control spotify for linux via dbus

    Last edited by fysicalplant; December 7th, 2011 at 04:15 AM.

  3. #13
    Join Date
    Dec 2011
    Beans
    3

    Re: A bash script to control spotify for linux via dbus

    http://www.mabishu.com/blog/2010/11/...ify-for-linux/

    use keyboard shortcuts (System Settings>Keyboard>Shortcuts>Custom Shortcuts) or compiz command shortcuts to create shortcuts for the lines here. just change the end for PlayPause, Next, and Previous

    IMPORTANT: Before creating shortcuts using the System Settings, be sure to check Sound and Media and disable those shortcuts first (click and use Backspace to disable a shortcut). If you happen to have created the shortcut before disabling the old one, simply reassign the old one, then disable using backspace, then re-enable the new one. Not sure if this is necessary using Compiz.
    Last edited by fysicalplant; December 7th, 2011 at 04:09 AM.

  4. #14
    Join Date
    Apr 2007
    Location
    linköping, sweden
    Beans
    119
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: A bash script to control spotify for linux via dbus

    Quote Originally Posted by fysicalplant View Post
    http://www.mabishu.com/blog/2010/11/...ify-for-linux/

    use compiz command shortcuts with the lines here. just change the end for playpause next and previous
    I'd like to point out that the script can be used for other things than just binding your keyboard media keys.

    Controlling spotify through ssh for instance...

  5. #15
    Join Date
    Feb 2008
    Location
    denmark
    Beans
    7
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: A bash script to control spotify for linux via dbus

    a bit late to the party. but just wanted to say thank you!. been looking for something like this.

  6. #16
    Join Date
    Oct 2007
    Beans
    3

    Re: A bash script to control spotify for linux via dbus

    Just a note... If anybody gets the following error...

    Code:
    Can't locate Net/DBus.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at /home/kerrick/scripts/spotify-dbus.pl line 7.
    BEGIN failed--compilation aborted at /home/kerrick/scripts/spotify-dbus.pl line 7.
    ...then you need to install libnet-dbus-perl and it'll work fine. This can be done by entering the following command as root or sudo:

    Code:
    sudo apt-get install libnet-dbus-perl
    Thanks to OP for the fantastic script!

  7. #17
    Join Date
    Jan 2007
    Location
    Bonn, Germany
    Beans
    37
    Distro
    Ubuntu

    Re: A bash script to control spotify for linux via dbus

    @azzid: thanks a lot, great script, well done!
    A witty saying proves nothing. -- Voltaire

  8. #18
    Join Date
    Sep 2008
    Location
    Lund
    Beans
    5

    Re: A bash script to control spotify for linux via dbus

    To be able to add playlists via this script add the following line at line 87.
    Code:
    when (m/\bspotify:user:[0-9a-zA-z]*:playlist:[0-9a-zA-z]{22}\b/) { $player->OpenUri($_); }
    Thanks for the script, it's exactly what you need and nothing more.

  9. #19
    Join Date
    Apr 2007
    Location
    linköping, sweden
    Beans
    119
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: A bash script to control spotify for linux via dbus

    Quote Originally Posted by e04mk View Post
    To be able to add playlists via this script add the following line at line 87.
    Code:
    when (m/\bspotify:user:[0-9a-zA-z]*:playlist:[0-9a-zA-z]{22}\b/) { $player->OpenUri($_); }
    Thanks for the script, it's exactly what you need and nothing more.
    Nice! Thanks! I added something similar at home.
    Never did find that "golden regexp" to describe all possible spotify uri:s.

  10. #20
    Join Date
    Nov 2012
    Beans
    3

    Re: A bash script to control spotify for linux via dbus

    This didnt work for me. Have they changed anything?
    Does Gnome-Spotify work in Unity?

Page 2 of 3 FirstFirst 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •