-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
srinfo
executable file
·78 lines (68 loc) · 1.92 KB
/
srinfo
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
#!/usr/bin/perl
# vim:ft=perl:
# abstract: sveriges radio info fetcher
use XML::Simple;
use LWP::Simple;
use Data::Dumper;
use Getopt::Long;
# FIXME This really, really needs a rewrite
our ($optList,$optChan);
$optChan = 2576; # 1606 => P3 Street
# 2576 => Din Gata
if(!@ARGV) {
print printAll(), "\n";
}
GetOptions('list' => \$optList,
'chan:s' => \$optChan,
'help' => \$help,
'np' => sub {print printAll(), "\n";},
);
# 1606 => P3 Street
listOld() if $optList;
if($help) {
print << "USAGE";
$0 [OPTIONS]
OPTIONS
-np show now playing for \$chanID
-c channel id
-l show playback history
-h show this help
USAGE
}
sub listOld {
my $url = qq{http://api.sr.se/api/playlist/playlist.aspx?programid=$optChan};
my $oldInfoC = get($url) or die;
my $x2 = XML::Simple->new;
my $oldXML = $x2->XMLin($oldInfoC);
while(my ($c, $d) = each %$oldXML) {
while(my ($id,$songInfo) = each %$d) {
while(my($infoTag,$infoData) = each %$songInfo) {
printf("%9s: %s\n", $infoTag, $infoData);
}
}
}
exit 0;
}
sub printAll {
my $url = "http://api.sr.se/api/rightnowinfo/Rightnowinfo.aspx?unit=$optChan";
my $content = get($url) or die;
my $x = XML::Simple->new;
my $np = $x->XMLin($content);
my $artist = $np->{Channel}->{Artist} // 'unknown';
my $title = $np->{Channel}->{SongTitle} // 'unknown';
my $next = $np->{Channel}->{NextSong} // 'unknown';
my $chan = $np->{Channel}->{Name} // 'unknown';
my $output;
if(!$artist and !$title) {
$output = $np->{Channel}->{Song} // 'No info';
return $output;
}
$output = sprintf("np: %s - %s | Next: %s (%s)",
ref($artist) ? Dumper $artist : $artist,
ref($title) ? Dumper $title : $title,
ref($next) ? Dumper $next : $next,
ref($chan) ? Dumper $chan : $chan,
);
return $output;
exit 0;
}