Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- #Used so, on exit, the mood will be reset to "Now Playing: Nothing"
- $SIG{'INT' } = 'interrupt'; $SIG{'QUIT'} = 'interrupt';
- $SIG{'HUP' } = 'interrupt'; $SIG{'TRAP'} = 'interrupt';
- $SIG{'ABRT'} = 'interrupt'; $SIG{'STOP'} = 'interrupt';
- sub nowPlaying
- {
- if (`ps -C banshee` =~ m/banshee/ && `banshee --query-current-state` =~ m/playing/) {
- $title = `banshee --query-title`;
- chop $title;
- $title =~ s/title: //;
- $artist = `banshee --query-artist`;
- chop $artist;
- $artist =~ s/artist: //;
- return "$title by $artist"
- }else{
- return "Nothing"
- }
- }
- sub interrupt {
- $skype->Invoke("SET PROFILE MOOD_TEXT Now Playing: Nothing");
- exit(1);
- }
- # This is a very basic example on how to use the Skype API via DBus using perl.
- # Written by jlh at gmx dot ch. I declare this to be public domain, use it for
- # whatever you like.
- # This requires the module Net::DBus to be installed.
- use strict;
- use warnings;
- package SkypeAPI; # -----------------------------------------------------------
- # We need two objects to communicate with Skype, one for the client -> Skype
- # direction, and one for Skype -> client direction. This class (SkypeAPI)
- # inherits from Net::DBus::Object and represents the DBus /com/Skype/Client
- # object that Skype uses to notify us about events (by calling its Notify
- # method). The other object, which is Skype's /com/Skype object, is held in
- # $self->{invoker}; we call its Invoke method to tell Skype our commands.
- # This class by itself only does the handshake with Skype and then merely
- # prints all notifications it receives to the terminal. If you want to do
- # something useful, then you have to subclass this and override the Notify
- # method to do whatever you want.
- use base 'Net::DBus::Object';
- use Net::DBus;
- sub new {
- my ($class, $name) = @_;
- my $bus = Net::DBus->session;
- # export a service and the object /com/Skype/Client, so Skype can
- # invoke the 'Notify' method on it in order to communicate with us.
- my $exp_service = $bus->export_service("com.Skype.API") or die;
- my $self = $class->SUPER::new($exp_service, '/com/Skype/Client') or die;
- bless $self, $class;
- # get a handle to Skype's /com/Skype object, so we can invoke the
- # 'Invoke' method on it to communicate with Skype.
- my $service = $bus->get_service("com.Skype.API") or die;
- $self->{invoker} = $service->get_object("/com/Skype") or die;
- # setup is done, let's shake hands
- my $r = $self->Invoke("NAME $name");
- die "Handshake failed: $r" unless $r eq 'OK';
- $r = $self->Invoke("PROTOCOL 5");
- die "Handshake failed: $r" unless $r eq 'PROTOCOL 5';
- return $self;
- }
- sub Notify {
- # only print to terminal. override this in a subclass for something
- # more useful.
- my ($self, $string) = @_;
- print "-> $string\n";
- # be careful with what you return here! DBus will try to serialize it,
- # returning it to skype. you should explicitely return something
- # simple to avoid to leak something unserializable, causing odd errors.
- return 0;
- }
- sub Invoke {
- # this doesn't print $string, so that subclasses can call it without
- # that side effect. subclass it yourself if you want it to do that.
- my ($self, $string) = @_;
- return $self->{invoker}->Invoke($string);
- }
- package main; # ---------------------------------------------------------------
- my $skype = SkypeAPI->new('NowPlayingForLinux');
- my $np = nowPlaying();
- $skype->Invoke("SET PROFILE MOOD_TEXT Now Playing: $np");
- while(1){
- my $npt = nowPlaying();
- if($npt ne $np){
- $np = $npt;
- $skype->Invoke("SET PROFILE MOOD_TEXT Now Playing: $np");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement