Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
- Stricter selection of a random invidious instance (with api_host =>…
Browse files Browse the repository at this point in the history
… "auto").

Ensure that a randomly selected invidious actually works, by making a search request with keyword "test".

Additionally, in the selection of instances, only include instances with "weeklyRatio = success" and "dailyRatios[0] = "success" (in addition to "statusClass = sucess").
  • Loading branch information
trizen committed Nov 4, 2020
1 parent 6387340 commit 09ce8a8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
35 changes: 32 additions & 3 deletions lib/WWW/StrawViewer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ sub select_good_invidious_instances {

my %ignored = (
'yewtu.be' => 1,
'invidious.tube' => 1,
'invidiou.site' => 1,
'invidious.xyz' => 1,
'vid.mint.lgbt' => 1,
Expand All @@ -567,7 +568,15 @@ sub select_good_invidious_instances {

my @candidates =
grep { not $ignored{$_->[0]} }
grep { ref($_->[1]{monitor}) eq 'HASH' ? ($_->[1]{monitor}{statusClass} eq 'success') : $args{lax} }
grep {
$args{lax} ? 1 : eval { $_->[1]{monitor}{dailyRatios}[0]{label} eq 'success' }
}
grep {
$args{lax} ? 1 : eval { $_->[1]{monitor}{weeklyRatio}{label} eq 'success' }
}
grep {
$args{lax} ? 1 : eval { $_->[1]{monitor}{statusClass} eq 'success' }
}
grep { lc($_->[1]{type} // '') eq 'https' } @$instances;

if ($self->get_debug) {
Expand All @@ -581,21 +590,41 @@ sub select_good_invidious_instances {
return @candidates;
}

sub pick_random_instance {
sub pick_good_random_instance {
my ($self) = @_;
my @candidates = $self->select_good_invidious_instances();

if (not @candidates) {
@candidates = $self->select_good_invidious_instances(lax => 1);
}

require List::Util;
require WWW::StrawViewer::Utils;

state $yv_utils = WWW::StrawViewer::Utils->new();

foreach my $instance (List::Util::shuffle(@candidates)) {

ref($instance) eq 'ARRAY' or next;

my $uri = $instance->[1]{uri} // next;
$uri =~ s{/+\z}{}; # remove trailing '/'

local $self->{api_host} = $uri;
my $results = $self->search_videos('test');

if ($yv_utils->has_entries($results)) {
return $instance;
}
}

$candidates[rand @candidates];
}

sub pick_and_set_random_instance {
my ($self) = @_;

my $instance = $self->pick_random_instance() // return;
my $instance = $self->pick_good_random_instance() // return;

ref($instance) eq 'ARRAY' or return;

Expand Down
3 changes: 2 additions & 1 deletion lib/WWW/StrawViewer/Utils.pm
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ Returns true if a given result has entries.
sub has_entries {
my ($self, $result) = @_;

$result // return 0;

if (ref($result->{results}) eq 'HASH') {

foreach my $type (qw(comments videos playlists)) {
Expand All @@ -247,7 +249,6 @@ sub has_entries {
}

return 1; # maybe?
#ref($result) eq 'HASH' and ($result->{results}{pageInfo}{totalResults} > 0);
}

=head2 normalize_video_title($title, $fat32safe)
Expand Down

0 comments on commit 09ce8a8

Please sign in to comment.