Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions plugin/ProtocolHandler.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,8 @@ sub getMetadataFor {
if ( $trackURL =~ m{youtube:/*(.+)} ) {
my $trackId = $class->getId($trackURL);

if ( $trackId && !$cache->get("yt:meta-$trackId") ) {
# skip ids we already gave up on, dont hammer the api
if ( $trackId && !$cache->get("yt:meta-$trackId") && !$cache->get("yt:meta-fail-$trackId") ) {
push @need, $trackId;
} elsif (!$trackId) {
$log->warn("No id found: $trackURL");
Expand All @@ -1170,7 +1171,7 @@ sub getMetadataFor {
if (scalar @need && !$abort) {
my $list = join( ',', @need );
main::INFOLOG && $log->is_info && $log->info( "Need to fetch metadata for: $list");
_getBulkMetadata($client, $pageCall, $list);
_getBulkMetadata($client, $pageCall, $list, \@need);
} else {
$client->master->pluginData(fetchingYTMeta => 0);
unless ($abort) {
Expand Down Expand Up @@ -1198,13 +1199,19 @@ sub getMetadataFor {
}

sub _getBulkMetadata {
my ($client, $cb, $ids) = @_;
my ($client, $cb, $ids, $requestedIds) = @_;

Plugins::YouTube::API->getVideoDetails( sub {
my $result = shift;

if ( !$result || $result->{error} || !$result->{pageInfo}->{totalResults} || !scalar @{$result->{items}} ) {
$log->error($result->{error} || 'Failed to grab track information');
# mark these failed for 5 min. enough to break the request storm,
# short enough that a fixed api key or transient blip recovers
# soon after.
if ($requestedIds) {
$cache->set("yt:meta-fail-$_", 1, 300) for @$requestedIds;
}
$cb->(1) if defined $cb;
return;
}
Expand Down Expand Up @@ -1241,6 +1248,15 @@ sub _getBulkMetadata {
$cache->set("yt:meta-" . $item->{id}, $meta, 86400);
}

# ids we asked for but didnt get back are usually deleted or private.
# cache as failed for a day so the UI poll stops asking.
if ($requestedIds) {
my %got = map { $_->{id} => 1 } @{$result->{items}};
for my $id (@$requestedIds) {
$cache->set("yt:meta-fail-$id", 1, 86400) unless $got{$id};
}
}

$cb->() if defined $cb;

}, $ids);
Expand Down