forked from intralanman/pianobar-eventcmd
/
eventcmd
executable file
·108 lines (86 loc) · 2.7 KB
/
eventcmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use LWP::UserAgent;
open DEBUG, '>>', '/tmp/eventcmd.log';
sub debug { print DEBUG shift . "\n"; }
sub osascript { system 'osascript', map { ('-e', $_) } split(/\n/, $_[0]); }
my $local_cover_art = '';
my %song_attrs;
if ( $ARGV[0] eq 'songstart') {
while ( <STDIN> ) {
chomp;
next if $_ =~ m/^station/;
next if $_ =~ m/^$/;
my ($key, $val) = m/^([[:alnum:]]+)=(.*)$/;
$song_attrs{$key} = $val;
if ( $key eq 'coverArt' && length($val) > 0 ) {
( $local_cover_art = $val ) =~ s|^.*/([[:alnum:]_]+\.jpg)$|$1|;
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(GET => $val);
my $res = $ua->request($req);
if ($res->is_success) {
open(COVER_ART, '>', "/tmp/$local_cover_art");
print COVER_ART $res->content;
close COVER_ART;
}
}
debug("$_");
}
my $appname = 'pandora';
my $title = 'Now Playing';
my $message = "'$song_attrs{title}' by '$song_attrs{artist}' on '$song_attrs{album}'";
my $image = "/tmp/$local_cover_art";
my $sticky = 0;
my $priority = 0;
my $uname = `uname` ;
chomp($uname);
if ( $uname eq 'Linux') {
eval 'use Net::DBus;';
eval 'use Net::DBus::Dumper;';
my $bus = Net::DBus->session;
my $service = $bus->get_service("org.freedesktop.Notifications");
my $object = $service->get_object("/org/freedesktop/Notifications", "org.freedesktop.Notifications");
$object->Notify($appname, $$, $image, $title, $message, [], {}, -1);
} elsif ( $uname eq 'Darwin' ) {
osascript <<END;
tell application "System Events"
set isRunning to (count of (every process whose bundle identifier is "com.Growl.GrowlHelperApp")) > 0
do shell script "echo Growl Is Not Running!"
end tell
if isRunning then
tell application id "com.Growl.GrowlHelperApp"
set the allNotificationsList to ¬
{"PianoBar", "PianoBar"}
set the enabledNotificationsList to ¬
{"PianoBar", "PianoBar"}
tell application "System Events"
set imgfile to file "/tmp/$local_cover_art"
end tell
set imgfd to open for access imgfile
set img to read imgfd as "TIFF"
close access imgfd
register as application ¬
"PianoBar" all notifications allNotificationsList ¬
default notifications enabledNotificationsList ¬
-- icon of application "/tmp/$local_cover_art"
-- Send a Notification…
notify with name ¬
"PianoBar" title ¬
"$title" description ¬
"$message" application name ¬
"PianoBar" image img
end tell
end if
END
} else {
print "I don't know how to run on your OS, sorry\n";
exit 1;
}
debug Dumper(\%song_attrs);
} else {
debug(Dumper(\%song_attrs));
debug(Dumper(\@ARGV));
}
close DEBUG;