-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPodcastProtocolHandler.pm
190 lines (147 loc) · 4.68 KB
/
PodcastProtocolHandler.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package Plugins::Deezer::PodcastProtocolHandler;
use strict;
use Scalar::Util qw(blessed);
use Slim::Utils::Cache;
use Slim::Utils::Log;
use Slim::Utils::Misc;
use Slim::Utils::Prefs;
use Slim::Utils::Scanner::Remote;
use Plugins::Deezer::Plugin;
use Plugins::Deezer::API;
use base qw(Slim::Player::Protocols::HTTPS);
my $prefs = preferences('plugin.deezer');
my $serverPrefs = preferences('server');
my $log = logger('plugin.deezer');
my $cache = Slim::Utils::Cache->new;
# https://www.deezer.com/episode/611754312
# is there a link for podcast?
my $URL_REGEX = qr{^https://(?:\w+\.)?deezer.com/(episode)/([a-z\d-]+)}i;
my $URI_REGEX = qr{^deezerpodcast://(\d+)}i;
Slim::Player::ProtocolHandlers->registerURLHandler($URL_REGEX, __PACKAGE__);
Slim::Player::ProtocolHandlers->registerURLHandler($URI_REGEX, __PACKAGE__);
sub formatOverride {
my ($class, $song) = @_;
my ($format) = $song->streamUrl =~ /\.(mp3|flc|flac|mp4|wav)/;
return $format =~ s/flac/flc/r;
}
sub new {
my ($class, $args) = @_;
$args->{url} = $args->{song}->streamUrl unless $args->{redir};
return $class->SUPER::new( $args );
}
# Avoid scanning
sub scanUrl {
my ( $class, $url, $args ) = @_;
$args->{cb}->( $args->{song}->currentTrack() );
}
=comment
# Source for AudioScrobbler
sub audioScrobblerSource {
my ( $class, $client, $url ) = @_;
# P = Chosen by the user
return 'P';
}
=cut
sub explodePlaylist {
my ( $class, $client, $url, $cb ) = @_;
my ($id) = $url =~ $URL_REGEX;
($id) = $url =~ $URI_REGEX unless $id;
return $cb->() unless $id;
main::INFOLOG && $log->is_info && $log->info("Getting $url id:$id");
return $cb->( [ $url ] );
}
sub getNextTrack {
my ( $class, $song, $successCb, $errorCb ) = @_;
my $client = $song->master();
my $url = $song->track->url;
my $id = _getId($url);
Plugins::Deezer::Plugin::getAPIHandler($client)->getEpisodesUrl( sub {
my $result = shift;
return $errorCb->($@) unless $result;
my $streamUrl = $result->{EPISODE_DIRECT_STREAM_URL};
$song->streamUrl($streamUrl);
my $format = $class->formatOverride($song);
# force the CT in the track as it might not been set which then
# might prevent parseRemoteHeader from working
$song->track->content_type($format);
main::INFOLOG && $log->is_info && $log->info("Streaming $format $url using $streamUrl");
# try to parse url
Slim::Utils::Scanner::Remote::parseRemoteHeader(
$song->track, $streamUrl, $format,
sub {
$client->currentPlaylistUpdateTime( Time::HiRes::time() );
Slim::Control::Request::notifyFromArray( $client, [ 'newmetadata' ] );
$successCb->();
},
sub {
my ($self, $error) = @_;
$log->warn( "could not find $format header $error" );
$successCb->();
}
);
}, $id );
}
=comment
sub trackInfoURL {
my ( $class, $client, $url ) = @_;
return [ {
title => "this is a title",
type => 'text',
} ];
}
=cut
my @pendingMeta = ();
sub getMetadataFor {
my ( $class, $client, $url ) = @_;
return {} unless $url;
my $song = $client->playingSong();
my $icon = Plugins::Deezer::Plugin->_pluginDataFor('icon');
my $defaultMeta = {
bitrate => 'N/A',
type => Plugins::Deezer::API::getFormat(),
icon => $icon,
cover => $icon,
};
my $episode = _getId($url);
return $defaultMeta unless $episode;
# episode identifier is unique across podcasts
my $id = $episode;
my $meta = $cache->get('deezer_episode_meta_' . $id);
# if metadata is in cache and is full
if ($meta) {
$meta->{album} = $meta->{podcast}->{title} if ref $meta->{podcast};
return $meta if $meta->{_complete};
}
my $now = time();
# first cleanup old requests in case some got lost
@pendingMeta = grep { $_->{time} + 60 > $now } @pendingMeta;
# only proceed if our request is not pending and we have less than 10 in parallel
if ( !(grep { $_->{id} eq $id } @pendingMeta) && scalar(@pendingMeta) < 10 ) {
push @pendingMeta, {
id => $id,
time => $now,
};
main::DEBUGLOG && $log->is_debug && $log->debug("adding metadata query for $episode");
Plugins::Deezer::Plugin::getAPIHandler($client)->episode(sub {
my $meta = shift;
@pendingMeta = grep { $_->{id} != $id } @pendingMeta;
return unless $meta;
main::INFOLOG && $log->is_info && $log->info("updating metadata for $id");
main::DEBUGLOG && $log->is_debug && $log->debug(Data::Dump::dump($meta));
return if @pendingMeta;
# Update the playlist time so the web will refresh, etc
$client->currentPlaylistUpdateTime( Time::HiRes::time() );
Slim::Control::Request::notifyFromArray( $client, [ 'newmetadata' ] );
}, $id );
}
return $meta || $defaultMeta;
}
sub _getId {
my ($id) = $_[0] =~ m|deezerpodcast://(\d+)|;
return $id;
}
sub getPlayingId {
my ($client, $url) = @_;
return _getId($url);
}
1;