Skip to content

Commit

Permalink
- Fixed some potential corner-cases.
Browse files Browse the repository at this point in the history
- When `youtube-dl` is not available, try multiple instances of invidious. (trying twice per instance)
  • Loading branch information
trizen committed Jun 15, 2020
1 parent 058fca3 commit ecd2990
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
14 changes: 12 additions & 2 deletions bin/youtube-viewer
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ sub load_config {
# Enable history if Term::ReadLine::Gnu::XS is installed
if (not defined $CONFIG{history}) {

if ($term->can('ReadHistory')) {
if (eval { $term->can('ReadHistory') }) {
$CONFIG{history} = 1;
}
else {
Expand Down Expand Up @@ -574,12 +574,22 @@ if ($opt{history}) {

# Create the history file.
if (not -e $opt{history_file}) {

require File::Basename;
my $dir = File::Basename::dirname($opt{history_file});

if (not -d $dir) {
require File::Path;
File::Path::make_path($dir)
or warn "[!] Can't create path <<$dir>>: $!";
}

open my $fh, '>', $opt{history_file}
or warn "[!] Can't create the history file `$opt{history_file}': $!";
}

# Add history to Term::ReadLine
$term->ReadHistory($opt{history_file});
eval { $term->ReadHistory($opt{history_file}) };

# All history entries
my @history = $term->history_list;
Expand Down
13 changes: 11 additions & 2 deletions lib/WWW/YoutubeViewer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -492,12 +492,21 @@ sub _make_feed_url {
sub _extract_from_invidious {
my ($self, $videoID) = @_;

my $url = sprintf("https://invidio.us/api/v1/videos/%s?fields=formatStreams,adaptiveFormats", $videoID);
my @instances = qw(
invidio.us
invidious.snopyta.org
invidious.13ad.de
);

my $tries = 3;
my $instance = shift(@instances);
my $url_format = "https://%s/api/v1/videos/%s?fields=formatStreams,adaptiveFormats";
my $url = sprintf($url_format, $instance, $videoID);

my $tries = 2 * scalar(@instances);
my $resp = $self->{lwp}->get($url);

while (not $resp->is_success() and $resp->status_line() =~ /read timeout/i and --$tries >= 0) {
$url = sprintf($url_format, shift(@instances), $videoID) if (@instances and ($tries % 2 == 0));
$resp = $self->{lwp}->get($url);
}

Expand Down

0 comments on commit ecd2990

Please sign in to comment.