diff options
-rwxr-xr-x | bin/fair-viewer | 26 | ||||
-rwxr-xr-x | bin/gtk-fair-viewer | 9 | ||||
-rw-r--r-- | lib/WWW/FairViewer/Utils.pm | 14 | ||||
-rw-r--r-- | lib/WWW/FairViewer/Videos.pm | 50 |
4 files changed, 81 insertions, 18 deletions
diff --git a/bin/fair-viewer b/bin/fair-viewer index 7f3377c..d50164f 100755 --- a/bin/fair-viewer +++ b/bin/fair-viewer @@ -16,7 +16,7 @@ #------------------------------------------------------- # fair-viewer # Fork: 14 February 2020 -# Edit: 14 February 2020 +# Edit: 05 June 2020 # https://framagit.org/heckyel/fair-viewer #------------------------------------------------------- @@ -2078,15 +2078,21 @@ sub rate_videos { sub get_and_play_video_ids { (my @ids = grep { get_valid_video_id($_) } @_) || return; - my $info = $yv_obj->video_details(join(',', @ids)); - if ($yv_utils->has_entries($info)) { - if (not play_videos([$info->{results}])) { - return; + foreach my $id (@ids) { + my $info = $yv_obj->video_details($id); + + if (ref($info) eq 'HASH' and keys %$info) { + ## OK } - } - else { - warn_cant_do('get info for', @ids); + else { + $info->{title} = "unknwon"; + $info->{lengthSeconds} = 0; + $info->{videoId} = $id; + warn_cant_do('get info for', $id); + } + + play_videos([$info]) || return; } return 1; @@ -2107,9 +2113,9 @@ sub get_and_print_video_info { my $videoID = get_valid_video_id($id) // next; my $info = $yv_obj->video_details($videoID); - if ($yv_utils->has_entries($info)) { + if (ref($info) eq 'HASH' and keys %$info) { local $opt{show_video_info} = 1; - print_video_info($info->{results}); + print_video_info($info); } else { warn_cant_do('get info for', $videoID); diff --git a/bin/gtk-fair-viewer b/bin/gtk-fair-viewer index e8ea8c4..e68a8c4 100755 --- a/bin/gtk-fair-viewer +++ b/bin/gtk-fair-viewer @@ -16,7 +16,7 @@ #------------------------------------------------------- # GTK Fair Viewer # Fork: 14 February 2020 -# Edit: 14 February 2020 +# Edit: 05 June 2020 # https://framagit.org/heckyel/fair-viewer #------------------------------------------------------- @@ -2226,11 +2226,8 @@ sub check_keywords { if ($key =~ /$get_video_id_re/o) { my $info = $yv_obj->video_details($+{video_id}); - - if ($yv_utils->has_entries($info)) { - if (not play_video($info->{results})) { - return; - } + if (ref($info) eq 'HASH' and keys %$info) { + play_video($info) || return; } else { return; diff --git a/lib/WWW/FairViewer/Utils.pm b/lib/WWW/FairViewer/Utils.pm index e50bf3f..06ea9d9 100644 --- a/lib/WWW/FairViewer/Utils.pm +++ b/lib/WWW/FairViewer/Utils.pm @@ -230,7 +230,7 @@ sub has_entries { } } - my $type = $result->{results}{type}//''; + my $type = $result->{results}{type} // ''; if ($type eq 'playlist') { return $result->{results}{videoCount} > 0; @@ -241,6 +241,10 @@ sub has_entries { return scalar(@{$result->{results}}) > 0; } + if (ref($result->{results}) eq 'HASH' and not keys %{$result->{results}}) { + return 0; + } + return 1; # maybe? #ref($result) eq 'HASH' and ($result->{results}{pageInfo}{totalResults} > 0); } @@ -458,6 +462,7 @@ sub get_description { require URI::Escape; require HTML::Entities; + # Decode external links $desc =~ s{<a href="/redirect\?(.*?)".*?>.*?</a>}{ my $url = $1; if ($url =~ /(?:^|;)q=([^&]+)/) { @@ -468,6 +473,13 @@ sub get_description { } }segi; + # Decode internal links to videos / playlists + $desc =~ s{<a href="/(watch\?.*?)".*?>(https://www\.youtube\.com)/watch\?.*?</a>}{ + my $url = $2; + my $params = URI::Escape::uri_unescape($1); + "$url/$params"; + }segi; + $desc =~ s{<br/?>}{\n}gi; $desc =~ s{<a href=".*?".*?>(.*?)</a>}{$1}sgi; $desc =~ s/<.*?>//gs; diff --git a/lib/WWW/FairViewer/Videos.pm b/lib/WWW/FairViewer/Videos.pm index aed2c3c..b29d1f7 100644 --- a/lib/WWW/FairViewer/Videos.pm +++ b/lib/WWW/FairViewer/Videos.pm @@ -187,8 +187,56 @@ When C<$part> is C<undef>, it defaults to I<snippet>. sub video_details { my ($self, $id, $fields) = @_; + $fields //= $self->basic_video_info_fields; - $self->_get_results($self->_make_feed_url("videos/$id", fields => $fields)); + my $info = $self->_get_results($self->_make_feed_url("videos/$id", fields => $fields))->{results}; + + if (ref($info) eq 'HASH' and exists $info->{videoId} and exists $info->{title}) { + return $info; + } + + if ($self->get_debug) { + say STDERR ":: Extracting video info using the fallback method..."; + } + + # Fallback using the `get_video_info` URL + my %video_info = $self->_get_video_info($id); + my $video = $self->parse_json_string($video_info{player_response} // return); + + if (exists $video->{videoDetails}) { + $video = $video->{videoDetails}; + } + else { + return; + } + + my %details = ( + title => $video->{title}, + videoId => $video->{videoId}, + + videoThumbnails => [ + map { + scalar { + quality => 'medium', + url => $_->{url}, + width => $_->{width}, + height => $_->{height}, + } + } @{$video->{thumbnail}{thumbnails}} + ], + + description => $video->{shortDescription}, + lengthSeconds => $video->{lengthSeconds}, + + keywords => $video->{keywords}, + viewCount => $video->{viewCount}, + + author => $video->{author}, + authorId => $video->{channelId}, + rating => $video->{averageRating}, + ); + + return \%details; } =head2 Return details |