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

Commit

Permalink
- Added a fallback method for extracting video information.
Browse files Browse the repository at this point in the history
- Decode internal YouTube URLs in description.
- Various fixes and improvements.
  • Loading branch information
trizen committed Jun 5, 2020
1 parent 3ebdcb0 commit 5ce3cb4
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 18 deletions.
9 changes: 3 additions & 6 deletions bin/gtk-straw-viewer
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#-------------------------------------------------------
# GTK Straw Viewer
# Fork: 14 February 2020
# Edit: 29 May 2020
# Edit: 05 June 2020
# https://github.com/trizen/straw-viewer
#-------------------------------------------------------

Expand Down Expand Up @@ -2225,11 +2225,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;
Expand Down
26 changes: 16 additions & 10 deletions bin/straw-viewer
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#-------------------------------------------------------
# straw-viewer
# Fork: 14 February 2020
# Edit: 29 May 2020
# Edit: 05 June 2020
# https://github.com/trizen/straw-viewer
#-------------------------------------------------------

Expand Down Expand Up @@ -2075,15 +2075,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;
Expand All @@ -2104,9 +2110,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);
Expand Down
14 changes: 13 additions & 1 deletion lib/WWW/StrawViewer/Utils.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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=([^&]+)/) {
Expand All @@ -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;
Expand Down
50 changes: 49 additions & 1 deletion lib/WWW/StrawViewer/Videos.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5ce3cb4

Please sign in to comment.