Skip to content

Commit 17fb213

Browse files
committed
- Added the prefer_invidious config-option (disabled by default).
When enabled, invidious instances will be preferred for getting the YouTube results. By default, the YouTube website is parsed directly, which is usually faster. - Also added the `--invidious` command-line option, which controls the `prefer_invidious` config-option. Example: pipe-viewer --invidious search terms is equivalent with: straw-viewer search terms From now on, `straw-viewer` is considered deprecated in favor of `pipe-viewer`.
1 parent 41830da commit 17fb213

File tree

4 files changed

+62
-28
lines changed

4 files changed

+62
-28
lines changed

bin/gtk-pipe-viewer

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#-------------------------------------------------------
1616
# GTK Pipe Viewer
1717
# Fork: 30 October 2020
18-
# Edit: 22 February 2021
18+
# Edit: 23 February 2021
1919
# https://github.com/trizen/pipe-viewer
2020
#-------------------------------------------------------
2121

@@ -176,21 +176,22 @@ my %CONFIG = (
176176
hpaned_position => 420,
177177

178178
# Pipe options
179-
split_videos => 1,
180-
m4a_audio => 1,
181-
dash => 1, # may load slow
182-
prefer_mp4 => 0,
183-
prefer_av1 => 0,
184-
ignore_av1 => 0,
185-
maxResults => 10,
186-
hfr => 1, # true to prefer high frame rate (HFR) videos
187-
resolution => 'best',
188-
videoDimension => undef,
189-
videoLicense => undef,
190-
region => undef,
191-
192-
comments_width => 80, # wrap comments longer than `n` characters
193-
comments_order => 'top', # valid values: time, relevance
179+
split_videos => 1,
180+
m4a_audio => 1,
181+
dash => 1, # may load slow
182+
prefer_mp4 => 0,
183+
prefer_av1 => 0,
184+
ignore_av1 => 0,
185+
prefer_invidious => 0,
186+
maxResults => 10,
187+
hfr => 1, # true to prefer high frame rate (HFR) videos
188+
resolution => 'best',
189+
videoDimension => undef,
190+
videoLicense => undef,
191+
region => undef,
192+
193+
comments_width => 80, # wrap comments longer than `n` characters
194+
comments_order => 'top', # valid values: time, relevance
194195

195196
# API
196197
api_host => "auto",
@@ -864,11 +865,11 @@ sub apply_configuration {
864865

865866
foreach my $option_name (
866867
qw(
867-
comments_order
868868
maxResults videoDimension videoLicense
869869
region debug http_proxy user_agent
870870
timeout cookie_file ytdl ytdl_cmd
871871
api_host prefer_mp4 prefer_av1
872+
comments_order prefer_invidious
872873
)
873874
) {
874875

@@ -2543,7 +2544,6 @@ sub display_results {
25432544
if (ref($items) ne 'ARRAY') {
25442545

25452546
my $current_instance = $yv_obj->get_api_host();
2546-
$yv_obj->pick_and_set_random_instance(); # set a random invidious instance
25472547

25482548
die "Probably $current_instance is down.\n"
25492549
. "\nTry changing the `api_host` in configuration file:\n\n"

bin/pipe-viewer

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#-------------------------------------------------------
1616
# pipe-viewer
1717
# Fork: 30 October 2020
18-
# Edit: 22 February 2021
18+
# Edit: 23 February 2021
1919
# https://github.com/trizen/pipe-viewer
2020
#-------------------------------------------------------
2121

@@ -212,6 +212,7 @@ my %CONFIG = (
212212
prefer_mp4 => 0,
213213
prefer_av1 => 0,
214214
ignore_av1 => 0,
215+
prefer_invidious => 0,
215216
fat32safe => $constant{win32},
216217
fullscreen => 0,
217218
show_video_info => 1,
@@ -811,6 +812,7 @@ usage: $execname [options] ([url] | [keywords])
811812
--custom-playlist-layout=s : custom layout format for playlists
812813
813814
* Other
815+
--invidious! : prefer invidious instances over parsing YouTube
814816
--api=s : set an API host from https://api.invidious.io/
815817
--api=auto : use a random instance of invidious
816818
--cookies=s : file to read cookies from and dump cookie
@@ -1113,7 +1115,7 @@ sub apply_configuration {
11131115
http_proxy page comments_order
11141116
subscriptions_order user_agent
11151117
cookie_file timeout ytdl ytdl_cmd
1116-
prefer_mp4 prefer_av1
1118+
prefer_mp4 prefer_av1 prefer_invidious
11171119
)
11181120
) {
11191121

@@ -1558,6 +1560,8 @@ sub parse_arguments {
15581560
'prefer-av1!' => \$opt{prefer_av1},
15591561
'ignore-av1!' => \$opt{ignore_av1},
15601562

1563+
'invidious|prefer-invidious!' => \$opt{prefer_invidious},
1564+
15611565
'custom-layout-format=s' => \$opt{custom_layout_format},
15621566
'custom-channel-layout-format=s' => \$opt{custom_channel_layout_format},
15631567
'custom-playlist-layout-format=s' => \$opt{custom_playlist_layout_format},
@@ -4439,6 +4443,10 @@ Prefer videos in MP4 (AVC) format.
44394443
44404444
Try to enable this option if the videos are lagging or dropping frames.
44414445
4446+
=head2 prefer_invidious
4447+
4448+
Prefer invidious instances over parsing the YouTube website directly.
4449+
44424450
=head2 region
44434451
44444452
ISO 3166 country code (default: "US").

lib/WWW/PipeViewer.pm

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@ my %valid_options = (
8080
ytdl_cmd => {valid => qr/\w/, default => "youtube-dl"},
8181

8282
# Booleans
83-
env_proxy => {valid => [1, 0], default => 1},
84-
escape_utf8 => {valid => [1, 0], default => 0},
85-
prefer_mp4 => {valid => [1, 0], default => 0},
86-
prefer_av1 => {valid => [1, 0], default => 0},
83+
env_proxy => {valid => [1, 0], default => 1},
84+
escape_utf8 => {valid => [1, 0], default => 0},
85+
prefer_mp4 => {valid => [1, 0], default => 0},
86+
prefer_av1 => {valid => [1, 0], default => 0},
87+
prefer_invidious => {valid => [1, 0], default => 0},
8788

8889
# API/OAuth
8990
key => {valid => qr/^.{15}/, default => undef},
@@ -448,7 +449,9 @@ sub lwp_get {
448449
}
449450

450451
# Too many errors. Pick another invidious instance.
451-
# $self->pick_and_set_random_instance();
452+
if ($url !~ m{\bm\.youtube\.com/}) {
453+
$self->pick_and_set_random_instance();
454+
}
452455

453456
_warn_reponse_error($response, $url);
454457
return;
@@ -596,12 +599,33 @@ sub select_good_invidious_instances {
596599
sub pick_random_instance {
597600
my ($self) = @_;
598601

599-
# TODO: make sure the selected invidious instance actually works.
602+
my @candidates = $self->select_good_invidious_instances();
603+
my @extra_candidates = $self->select_good_invidious_instances(lax => 1);
604+
605+
require List::Util;
606+
require WWW::PipeViewer::Utils;
607+
608+
state $yv_utils = WWW::PipeViewer::Utils->new();
609+
610+
foreach my $instance (List::Util::shuffle(@candidates), List::Util::shuffle(@extra_candidates)) {
600611

601-
my @candidates = $self->select_good_invidious_instances();
612+
ref($instance) eq 'ARRAY' or next;
613+
614+
my $uri = $instance->[1]{uri} // next;
615+
$uri =~ s{/+\z}{}; # remove trailing '/'
616+
617+
local $self->{api_host} = $uri;
618+
local $self->{prefer_invidious} = 1;
619+
620+
my $results = $self->search_videos('test');
621+
622+
if ($yv_utils->has_entries($results)) {
623+
return $instance;
624+
}
625+
}
602626

603627
if (not @candidates) {
604-
@candidates = $self->select_good_invidious_instances(lax => 1);
628+
@candidates = @extra_candidates;
605629
}
606630

607631
$candidates[rand @candidates];

lib/WWW/PipeViewer/InitialData.pm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,8 @@ sub _extract_playlist_videos {
508508
sub _get_initial_data {
509509
my ($self, $url) = @_;
510510

511+
$self->get_prefer_invidious() and return;
512+
511513
my $content = $self->lwp_get($url) // return;
512514

513515
if ($content =~ m{var\s+ytInitialData\s*=\s*'(.*?)'}is) {

0 commit comments

Comments
 (0)