Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.58 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. #Used so, on exit, the mood will be reset to "Now Playing: Nothing"
  4. $SIG{'INT' } = 'interrupt';  $SIG{'QUIT'} = 'interrupt';
  5. $SIG{'HUP' } = 'interrupt';  $SIG{'TRAP'} = 'interrupt';
  6. $SIG{'ABRT'} = 'interrupt';  $SIG{'STOP'} = 'interrupt';
  7.  
  8. sub nowPlaying
  9. {
  10.     if (`ps -C banshee` =~ m/banshee/ && `banshee --query-current-state` =~ m/playing/) {
  11.         $title = `banshee --query-title`;
  12.         chop $title;
  13.         $title =~ s/title: //;
  14.         $artist = `banshee --query-artist`;
  15.         chop $artist;
  16.         $artist =~ s/artist: //;
  17.         return "$title by $artist"
  18.     }else{
  19.         return "Nothing"
  20.     }
  21. }
  22.  
  23. sub interrupt {
  24.     $skype->Invoke("SET PROFILE MOOD_TEXT Now Playing: Nothing");
  25.     exit(1);
  26. }
  27.  
  28. # This is a very basic example on how to use the Skype API via DBus using perl.
  29. # Written by jlh at gmx dot ch.  I declare this to be public domain, use it for
  30. # whatever you like.
  31.  
  32. # This requires the module Net::DBus to be installed.
  33.  
  34. use strict;
  35. use warnings;
  36.  
  37. package SkypeAPI; # -----------------------------------------------------------
  38.  
  39. # We need two objects to communicate with Skype, one for the client -> Skype
  40. # direction, and one for Skype -> client direction.  This class (SkypeAPI)
  41. # inherits from Net::DBus::Object and represents the DBus /com/Skype/Client
  42. # object that Skype uses to notify us about events (by calling its Notify
  43. # method).  The other object, which is Skype's /com/Skype object, is held in
  44. # $self->{invoker}; we call its Invoke method to tell Skype our commands.
  45.  
  46. # This class by itself only does the handshake with Skype and then merely
  47. # prints all notifications it receives to the terminal.  If you want to do
  48. # something useful, then you have to subclass this and override the Notify
  49. # method to do whatever you want.
  50.  
  51. use base 'Net::DBus::Object';
  52. use Net::DBus;
  53.  
  54. sub new {
  55.     my ($class, $name) = @_;
  56.  
  57.     my $bus = Net::DBus->session;
  58.  
  59.     # export a service and the object /com/Skype/Client, so Skype can
  60.     # invoke the 'Notify' method on it in order to communicate with us.
  61.     my $exp_service = $bus->export_service("com.Skype.API") or die;
  62.     my $self = $class->SUPER::new($exp_service, '/com/Skype/Client') or die;
  63.     bless $self, $class;
  64.  
  65.     # get a handle to Skype's /com/Skype object, so we can invoke the
  66.     # 'Invoke' method on it to communicate with Skype.
  67.     my $service = $bus->get_service("com.Skype.API") or die;
  68.     $self->{invoker} = $service->get_object("/com/Skype") or die;
  69.  
  70.     # setup is done, let's shake hands
  71.     my $r = $self->Invoke("NAME $name");
  72.     die "Handshake failed: $r" unless $r eq 'OK';
  73.     $r = $self->Invoke("PROTOCOL 5");
  74.     die "Handshake failed: $r" unless $r eq 'PROTOCOL 5';
  75.  
  76.     return $self;
  77. }
  78.  
  79. sub Notify {
  80.     # only print to terminal.  override this in a subclass for something
  81.     # more useful.
  82.     my ($self, $string) = @_;
  83.     print "-> $string\n";
  84.     # be careful with what you return here!  DBus will try to serialize it,
  85.     # returning it to skype.  you should explicitely return something
  86.     # simple to avoid to leak something unserializable, causing odd errors.
  87.     return 0;
  88. }
  89.  
  90. sub Invoke {
  91.     # this doesn't print $string, so that subclasses can call it without
  92.     # that side effect.  subclass it yourself if you want it to do that.
  93.     my ($self, $string) = @_;
  94.     return $self->{invoker}->Invoke($string);
  95. }
  96.  
  97. package main; # ---------------------------------------------------------------
  98.  
  99. my $skype = SkypeAPI->new('NowPlayingForLinux');
  100.  
  101. my $np = nowPlaying();
  102. $skype->Invoke("SET PROFILE MOOD_TEXT Now Playing: $np");
  103. while(1){
  104.     my $npt = nowPlaying();
  105.     if($npt ne $np){
  106.         $np = $npt;
  107.         $skype->Invoke("SET PROFILE MOOD_TEXT Now Playing: $np");
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement