/
rpnotify
executable file
·105 lines (99 loc) · 4.36 KB
/
rpnotify
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
#!/usr/bin/perl
#
# Copyright (C) 2012-2015 Dominic Hargreaves <dom@earth.li>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
use strict;
use warnings;
use Gtk2::Notify;
use LWP::UserAgent;
use Readonly;
use XML::Simple qw(:strict);
use Log::Log4perl qw(:easy);
use HTML::Entities;
Readonly my $PLAYLIST_URL => 'http://www2.radioparadise.com/chumby.xml';
Readonly my $MORE_INFO_URL_BASE => 'http://www.radioparadise.com/content.php?name=songinfo&song_id=';
Readonly my $SCRIPT_NAME => 'rpnotify';
Readonly my $SCRIPT_VERSION => '0.2';
Readonly my $SCRIPT_URL => 'https://github.com/jmdh/rpnotify';
Readonly my $DEBUG_ENABLE => 0;
Readonly my $NOTIFY_FOR => 10000; # milliseconds
# This seems to need tweaking to avoid fetching the playlist
# before it's been updated
Readonly my $EXTRA_DELAY => 5; # seconds
Log::Log4perl->easy_init($DEBUG_ENABLE ? $DEBUG : $INFO);
Gtk2::Notify->init($SCRIPT_NAME);
my $ua = LWP::UserAgent->new;
$ua->agent("$SCRIPT_NAME $SCRIPT_VERSION <$SCRIPT_URL>");
my $last_song;
while (1) {
my ($wait_until, $sleep_for);
my $response = $ua->get($PLAYLIST_URL);
if ($response->is_success) {
my $playlist = XMLin(
$response->content, ForceArray => ['song'], KeyAttr => ['song']);
my $current_song = $playlist->{song}->[0];
if (defined $last_song && $current_song->{songid} &&
$current_song->{songid} == $last_song) {
WARN "Song hasn't changed, not notifying";
$wait_until = time;
} else {
if (defined $current_song->{title} &&
defined $current_song->{artist} &&
defined $current_song->{album}) {
my $album = ref $current_song->{album} eq ref {} ?
'(no album)' :
$current_song->{album};
my $notification = Gtk2::Notify->new(
"Radio Paradise: Now Playing ($current_song->{playtime}):",
encode_entities($current_song->{title}) . "\n" .
encode_entities($current_song->{artist}) . "\n" .
encode_entities($album) . "\n" .
encode_entities("<a href=\"" . $MORE_INFO_URL_BASE .
$current_song->{songid} . "\">more info</a>", '&')
);
$notification->set_timeout($NOTIFY_FOR);
INFO "Sending notification about song $current_song->{songid}";
INFO "Title: " . $current_song->{title};
INFO "Artist: " . $current_song->{artist};
INFO "Album: " . $album;
INFO "Info: " . $MORE_INFO_URL_BASE . $current_song->{songid};
$notification->show;
} else {
WARN "Missing expected elements, not notifying";
}
$last_song = $current_song->{songid} // 0;
$wait_until = $current_song->{refresh_time};
}
unless ($wait_until) {
WARN "Defaulting to waiting for 5 minutes";
$wait_until = time + 300;
}
$sleep_for = $wait_until - time + rand(3) + $EXTRA_DELAY;
if ($sleep_for < 5) {
INFO "Would have slept for less than 5 seconds; overriding";
$sleep_for = 5;
}
} else {
WARN "Error fetching playlist: " . $response->status_line;
$sleep_for = 60;
}
INFO "Sleeping for $sleep_for seconds";
sleep $sleep_for;
}