diff options
-rwxr-xr-x | bin/gtk-straw-viewer | 6 | ||||
-rwxr-xr-x | bin/straw-viewer | 89 | ||||
-rw-r--r-- | lib/WWW/StrawViewer.pm | 33 | ||||
-rw-r--r-- | lib/WWW/StrawViewer/Utils.pm | 23 | ||||
-rw-r--r-- | lib/WWW/StrawViewer/Videos.pm | 5 |
5 files changed, 70 insertions, 86 deletions
diff --git a/bin/gtk-straw-viewer b/bin/gtk-straw-viewer index 26183ac..7af5b95 100755 --- a/bin/gtk-straw-viewer +++ b/bin/gtk-straw-viewer @@ -55,8 +55,6 @@ my $version = $WWW::StrawViewer::VERSION; # Share directory my $share_dir = ($DEVEL and -d "../share") ? '../share' : dist_dir('WWW-StrawViewer'); -sub VIDEO_PART () { 'contentDetails,statistics,snippet' } - # Configuration dir/file my $home_dir; my $xdg_config_home = $ENV{XDG_CONFIG_HOME}; @@ -2233,10 +2231,10 @@ sub check_keywords { my ($key) = @_; if ($key =~ /$get_video_id_re/o) { - my $info = $yv_obj->video_details($+{video_id}, VIDEO_PART); + my $info = $yv_obj->video_details($+{video_id}); if ($yv_utils->has_entries($info)) { - if (not play_video($info->{results}{items}[0])) { + if (not play_video($info->{results})) { return; } } diff --git a/bin/straw-viewer b/bin/straw-viewer index e444a35..cb30552 100755 --- a/bin/straw-viewer +++ b/bin/straw-viewer @@ -81,11 +81,6 @@ my $execname = 'straw-viewer'; require Term::ReadLine; my $term = Term::ReadLine->new("$appname $version"); -# Developer key -my $key = 'aXalQYmzI8gPkMSLyMhpApfMAiU2b23Qz2nE3mq'; - -sub VIDEO_PART () { 'contentDetails,statistics,snippet' } - # Options (key=>value) goes here my %opt; my $term_width = 80; @@ -257,7 +252,7 @@ my %CONFIG = ( highlight_watched => 1, highlight_color => 'bold', remove_played_file => 0, - history => 0, + history => undef, # auto-defined history_limit => 100_000, history_file => $history_file, convert_cmd => 'ffmpeg -i *IN* *OUT*', @@ -536,6 +531,19 @@ sub load_config { $update_config = 1; } + # Enable history if Term::ReadLine::Gnu::XS is installed + if (not defined $CONFIG{history}) { + + if ($term->can('ReadHistory')) { + $CONFIG{history} = 1; + } + else { + $CONFIG{history} = 0; + } + + $update_config = 1; + } + foreach my $key (keys %CONFIG) { if (not exists $CONFIG->{$key}) { $update_config = 1; @@ -600,14 +608,8 @@ if ($opt{history}) { } } -{ - my $i = length $key; - $key =~ s/(.{$i})(.)/$2$1/g while --$i; -} - my $yv_obj = WWW::StrawViewer->new( escape_utf8 => 1, - key => $key, config_dir => $config_dir, cache_dir => $opt{cache_dir}, lwp_env_proxy => $opt{env_proxy}, @@ -615,63 +617,6 @@ my $yv_obj = WWW::StrawViewer->new( authentication_file => $authentication_file, ); -{ - $yv_obj->set_client_id('923751928481.apps.googleusercontent.com'); - $yv_obj->set_client_secret("\26/Ae]3\b\6\x186a:*#0\32\t\f\n\27\17GC`" ^ substr($key, -24)); - $yv_obj->set_redirect_uri('urn:ietf:wg:oauth:2.0:oob'); -} - -if (-f $api_file) { - - open(my $fh, '<', $api_file) or die "[!] Can't open file <<$api_file>> for reading: $!\n"; - my $content = do { local $/; <$fh> }; - my $api = $yv_obj->parse_json_string($content); - - if (ref($api) ne 'HASH') { - die "[!] Invalid format inside file 'api.json'.\n"; - } - - my $orig_key = $yv_obj->get_key; - my $orig_client_id = $yv_obj->get_client_id; - my $orig_client_secret = $yv_obj->get_client_secret; - - my $key = $api->{key}; - my $client_id = $api->{client_id}; - my $client_secret = $api->{client_secret}; - - if (defined($key)) { - $yv_obj->set_key($key) // do { - warn "[!] Invalid key: $key\n" if $key ne 'API_KEY'; - $yv_obj->set_key($orig_key); - }; - } - if (defined($client_id)) { - $yv_obj->set_client_id($client_id) // do { - warn "[!] Invalid client_id: $client_id\n" if $client_id ne 'CLIENT_ID'; - $yv_obj->set_client_id($orig_client_id); - }; - } - if (defined($client_secret)) { - $yv_obj->set_client_secret($client_secret) // do { - warn "[!] Invalid client_secret: $client_secret\n" if $client_secret ne 'CLIENT_SECRET'; - $yv_obj->set_client_secret($orig_client_secret); - }; - } -} -else { - open(my $fh, '>', $api_file) or warn "[!] Can't create file <<$api_file>>: $!\n"; - print $fh <<"EOT"; -{ - "key": "API_KEY", - "client_id": "CLIENT_ID", - "client_secret": "CLIENT_SECRET" -} -EOT - close $fh; -} - -$yv_obj->load_authentication_tokens(); - require WWW::StrawViewer::Utils; my $yv_utils = WWW::StrawViewer::Utils->new(youtube_url_format => $opt{youtube_video_url}, thousand_separator => $opt{thousand_separator},); @@ -2173,10 +2118,10 @@ 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), VIDEO_PART); + my $info = $yv_obj->video_details(join(',', @ids)); if ($yv_utils->has_entries($info)) { - if (not play_videos($info->{results}{items})) { + if (not play_videos([$info->{results}])) { return; } } @@ -2200,7 +2145,7 @@ sub get_and_print_video_info { foreach my $id (@_) { my $videoID = get_valid_video_id($id) // next; - my $info = $yv_obj->video_details($videoID, VIDEO_PART); + my $info = $yv_obj->video_details($videoID); if ($yv_utils->has_entries($info)) { local $opt{show_video_info} = 1; diff --git a/lib/WWW/StrawViewer.pm b/lib/WWW/StrawViewer.pm index 42206d1..de13099 100644 --- a/lib/WWW/StrawViewer.pm +++ b/lib/WWW/StrawViewer.pm @@ -130,6 +130,39 @@ sub _our_smartmatch { return 0; } +sub basic_video_info_fields { + join(',', + qw( + title + videoId + description + published + publishedText + viewCount + likeCount + dislikeCount + genre + author + authorId + lengthSeconds + rating + liveNow + ) + ) +} + +sub extra_video_info_fields { + my ($self) = @_; + join(',', + $self->basic_video_info_fields, + qw( + subCountText + captions + isFamilyFriendly + ) + ); +} + { no strict 'refs'; diff --git a/lib/WWW/StrawViewer/Utils.pm b/lib/WWW/StrawViewer/Utils.pm index 062bbe1..f70afc0 100644 --- a/lib/WWW/StrawViewer/Utils.pm +++ b/lib/WWW/StrawViewer/Utils.pm @@ -228,12 +228,18 @@ sub has_entries { return scalar @{$result->{results}{comments}} > 0; } - if ($result->{results}{type} eq 'playlist') { + my $type = $result->{results}{type}//''; + + if ($type eq 'playlist') { return $result->{results}{videoCount} > 0; } } - scalar(@{$result->{results}}) > 0; + if (ref($result->{results}) eq 'ARRAY') { + return scalar(@{$result->{results}}) > 0; + } + + return 1; # maybe? #ref($result) eq 'HASH' and ($result->{results}{pageInfo}{totalResults} > 0); } @@ -530,7 +536,8 @@ sub get_channel_id { sub get_category_id { my ($self, $info) = @_; #$info->{snippet}{resourceId}{categoryId} // $info->{snippet}{categoryId}; - "unknown"; + #"unknown"; + $info->{genre} // 'Unknown'; } sub get_category_name { @@ -554,7 +561,9 @@ sub get_category_name { 29 => 'Nonprofits & Activism', }; - $categories->{$self->get_category_id($info) // ''} // 'Unknown'; + #$categories->{$self->get_category_id($info) // ''} // 'Unknown'; + + $info->{genre} // 'Unknown'; } sub get_publication_date { @@ -677,14 +686,12 @@ sub get_views_approx { sub get_likes { my ($self, $info) = @_; - #$info->{statistics}{likeCount}; - 0; + $info->{likeCount} // 0; } sub get_dislikes { my ($self, $info) = @_; - #$info->{statistics}{dislikeCount}; - 0; + $info->{dislikeCount} // 0; } sub get_comments { diff --git a/lib/WWW/StrawViewer/Videos.pm b/lib/WWW/StrawViewer/Videos.pm index 9df9ff3..11bf60b 100644 --- a/lib/WWW/StrawViewer/Videos.pm +++ b/lib/WWW/StrawViewer/Videos.pm @@ -186,8 +186,9 @@ When C<$part> is C<undef>, it defaults to I<snippet>. =cut sub video_details { - my ($self, $id, $part) = @_; - return $self->_get_results($self->_make_videos_url(id => $id, part => $part // 'snippet')); + my ($self, $id, $fields) = @_; + $fields //= $self->basic_video_info_fields; + $self->_get_results($self->_make_feed_url("videos/$id", fields => $fields)); } =head2 Return details |