aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authortrizen <trizen@protonmail.com>2020-06-05 23:19:15 +0300
committerJesús <heckyel@hyperbola.info>2020-06-06 22:05:56 -0500
commit751dd4e0d6ac3fe6a45c51e742a2b73ba69a4eed (patch)
tree128fba638ce4d849437b139d651c63500a852a6f /lib
parent4621e4af0b93c5654e022158122f4fb64cf12799 (diff)
downloadfair-viewer-751dd4e0d6ac3fe6a45c51e742a2b73ba69a4eed.tar.lz
fair-viewer-751dd4e0d6ac3fe6a45c51e742a2b73ba69a4eed.tar.xz
fair-viewer-751dd4e0d6ac3fe6a45c51e742a2b73ba69a4eed.zip
- Added a fallback method for extracting video information. - Decode internal YouTube URLs in description. - Various fixes and improvements.
Signed-off-by: Jesús <heckyel@hyperbola.info>
Diffstat (limited to 'lib')
-rw-r--r--lib/WWW/FairViewer/Utils.pm14
-rw-r--r--lib/WWW/FairViewer/Videos.pm50
2 files changed, 62 insertions, 2 deletions
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