-
Notifications
You must be signed in to change notification settings - Fork 1
/
Metadata.pm
99 lines (73 loc) · 2.13 KB
/
Metadata.pm
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
package Plugins::Phishin::Metadata;
use strict;
use Tie::Cache::LRU;
use Slim::Formats::RemoteMetadata;
use Slim::Utils::Cache;
use Slim::Utils::Log;
use Plugins::Phishin::Plugin;
use constant ARTIST => 'Phish';
use constant CACHE_PREFIX => 'phishin_meta';
use constant CACHE_TTL => 30 * 86400;
my $log = logger('plugin.phishin');
my $cache = Slim::Utils::Cache->new();
# In-memory cache for the most often used tracks. Should cover a full show.
tie my %memCache, 'Tie::Cache::LRU', 50;
sub init {
my $class = shift;
Slim::Formats::RemoteMetadata->registerProvider(
match => qr{https?://phish\.in/audio},
func => \&provider,
);
}
sub provider {
my ( $client, $url ) = @_;
return __PACKAGE__->getMetadataFor($url);
}
sub getMetadataFor {
my ( $class, $url ) = @_;
# read from memory cache first, we're called often by the web UI
my $meta = $memCache{$url};
if (!$meta) {
$meta = $cache->get(CACHE_PREFIX . $url);
# if we found data in the cache, keep a copy in memory, too
if ($meta) {
$memCache{$url} = $meta;
}
else {
$meta = {};
}
}
$meta->{cover} ||= Plugins::Phishin::Plugin->_pluginDataFor('icon');
main::DEBUGLOG && $log->is_debug && $log->debug("Found metadata for $url: " . Data::Dump::dump($meta));
return $meta;
}
sub setMetadata {
my ( $class, $track, $show ) = @_;
if (!$track->{mp3}) {
$log->warn("Metadata is missing audio stream URL? " . $track);
main::INFOLOG && $log->is_info && $log->info(Data::Dump::dump($track));
return;
}
# consider the show details the album information
my $album = $show->{date} || '';
if (my $venue = $show->{venue}) {
$album = ($album ? "$album - " : '') . $venue->{name};
if ($track->{set_name}) {
$album .= ' (' . $track->{set_name} . ')';
}
}
my $meta = {
title => $track->{title},
artist => ARTIST,
album => $album,
cover => Plugins::Phishin::Plugin->_pluginDataFor('icon'),
url => $track->{mp3},
tracknum => $track->{position},
secs => (delete $track->{duration}) / 1000,
};
$meta->{duration} = $meta->{secs};
$memCache{$meta->{url}} = $meta;
$cache->set(CACHE_PREFIX . $meta->{url}, $meta);
return $meta;
}
1;