Skip to content

Commit

Permalink
- Added support for loading cookies from a file.
Browse files Browse the repository at this point in the history
The file must be a "# Netscape HTTP Cookie File"; same format as "youtube-dl" requires.

The "cookies.txt" extension can be used for exporting the cookies from the browser.

This helps with the "429: Too Many Requests" issue.

See also: https://github.com/ytdl-org/youtube-dl#how-do-i-pass-cookies-to-youtube-dl

- Also added support for changing the user-agent.
  • Loading branch information
trizen committed Sep 8, 2020
1 parent de05ad6 commit 940b2c5
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 69 deletions.
35 changes: 21 additions & 14 deletions bin/gtk2-youtube-viewer
Expand Up @@ -15,7 +15,7 @@
#-------------------------------------------------------
# GTK YouTube Viewer
# Created on: 12 September 2010
# Latest edit on: 24 August 2020
# Latest edit on: 08 September 2020
# https://github.com/trizen/youtube-viewer
#-------------------------------------------------------

Expand All @@ -25,7 +25,7 @@ use 5.016;
use warnings;
no warnings 'once';

my $DEVEL; # true in devel mode
my $DEVEL; # true in devel mode
use if ($DEVEL = 0), lib => qw(../lib); # devel only

use WWW::YoutubeViewer v3.7.8;
Expand Down Expand Up @@ -216,13 +216,16 @@ my %CONFIG = (
# Others
env_proxy => 1,
http_proxy => undef,
timeout => undef,
user_agent => undef,
cookie_file => undef,
prefer_fork => (($^O eq 'linux') ? 0 : 1),
debug => 0,
fullscreen => 0,
audio_only => 0,

tooltips => 1,
tooltip_max_len => 512, # max length of description in tooltips
tooltip_max_len => 512, # max length of description in tooltips

thousand_separator => q{,},
downloads_dir => curdir(),
Expand Down Expand Up @@ -735,6 +738,9 @@ my $yv_obj = WWW::YoutubeViewer->new(
hl => $CONFIG{hl},
lwp_env_proxy => $CONFIG{env_proxy},
cache_dir => $CONFIG{cache_dir},
cookie_file => $CONFIG{cookie_file},
user_agent => $CONFIG{user_agent},
timeout => $CONFIG{timeout},
authentication_file => $authentication_file,
);

Expand Down Expand Up @@ -807,7 +813,8 @@ sub apply_configuration {
videoEmbeddable videoLicense
publishedAfter publishedBefore
regionCode videoCategoryId
debug http_proxy
debug http_proxy user_agent
timeout cookie_file
)
) {

Expand Down Expand Up @@ -1363,8 +1370,8 @@ $accel->connect(ord('g'), ['control-mask'], ['visible'], \&show_user_favorited_v
$accel->connect(ord('m'), ['control-mask'], ['visible'], \&show_videos_from_selected_author);
$accel->connect(ord('k'), ['control-mask'], ['visible'], \&show_playlists_from_selected_author);
$accel->connect(ord('w'), ['control-mask'], ['visible'], \&show_warnings_window);
$accel->connect(0xffff, ['lock-mask'], ['visible'], \&delete_selected_row);
$accel->connect(0xffc8, ['lock-mask'], ['visible'], \&maximize_unmaximize_mainw);
$accel->connect(0xffff, ['lock-mask'], ['visible'], \&delete_selected_row);
$accel->connect(0xffc8, ['lock-mask'], ['visible'], \&maximize_unmaximize_mainw);
$mainw->add_accel_group($accel);

# Support for navigating back and forth using the side buttons of the mouse
Expand Down Expand Up @@ -1393,7 +1400,7 @@ $accel->connect(0xff1b, ['lock-mask'], ['visible'], \&hide_feeds_window);
$feeds_window->add_accel_group($accel);

$accel = Gtk2::AccelGroup->new;
$accel->connect(0xff1b, ['lock-mask'], ['visible'], \&hide_preferences_window);
$accel->connect(0xff1b, ['lock-mask'], ['visible'], \&hide_preferences_window);
$accel->connect(ord('s'), ['control-mask'], ['visible'], \&save_configuration);
$prefernces_window->add_accel_group($accel);

Expand Down Expand Up @@ -1974,8 +1981,8 @@ sub set_youtube_tops {
}

sub remove_selected_user {
my $selection = $users_treeview->get_selection // return;
my $iter = $selection->get_selected // return;
my $selection = $users_treeview->get_selection // return;
my $iter = $selection->get_selected // return;
my $channel_id = $users_liststore->get($iter, 0);
delete $channels{$channel_id};
$users_liststore->remove($iter);
Expand Down Expand Up @@ -2421,7 +2428,7 @@ sub display_results {

my $url = $results->{url};
my $info = $results->{results} // {};
my $items = $info->{items} // [];
my $items = $info->{items} // [];

hide_feeds_window();

Expand Down Expand Up @@ -3067,10 +3074,10 @@ sub get_options_as_arguments {
'no-interactive' => q{},
'resolution' => $CONFIG{resolution},
'download-dir' => quotemeta(rel2abs($CONFIG{downloads_dir})),
'fullscreen' => $CONFIG{fullscreen} ? q{} : undef,
'fullscreen' => $CONFIG{fullscreen} ? q{} : undef,
'no-dash' => $CONFIG{dash_support} ? undef : q{},
'no-video' => $CONFIG{audio_only} ? q{} : undef,
'resolution=audio' => $CONFIG{audio_only} ? q{} : undef,
'no-video' => $CONFIG{audio_only} ? q{} : undef,
'resolution=audio' => $CONFIG{audio_only} ? q{} : undef,
);

while (my ($argv, $value) = each %options) {
Expand Down Expand Up @@ -3273,7 +3280,7 @@ sub display_comments {

my $url = $results->{url};
my $res = $results->{results} // {};
my $comments = $res->{items} // [];
my $comments = $res->{items} // [];

foreach my $comment (@{$comments}) {
my $snippet = (($comment->{snippet} // next)->{topLevelComment} // next)->{snippet};
Expand Down
36 changes: 22 additions & 14 deletions bin/gtk3-youtube-viewer
Expand Up @@ -15,7 +15,7 @@
#-------------------------------------------------------
# GTK YouTube Viewer
# Created on: 12 September 2010
# Latest edit on: 24 August 2020
# Latest edit on: 08 September 2020
# https://github.com/trizen/youtube-viewer
#-------------------------------------------------------

Expand All @@ -25,7 +25,7 @@ use 5.016;
use warnings;
no warnings 'once';

my $DEVEL; # true in devel mode
my $DEVEL; # true in devel mode
use if ($DEVEL = 0), lib => qw(../lib); # devel only

use WWW::YoutubeViewer v3.7.8;
Expand Down Expand Up @@ -217,13 +217,16 @@ my %CONFIG = (
# Others
env_proxy => 1,
http_proxy => undef,
timeout => undef,
user_agent => undef,
cookie_file => undef,
prefer_fork => (($^O eq 'linux') ? 0 : 1),
debug => 0,
fullscreen => 0,
audio_only => 0,

tooltips => 1,
tooltip_max_len => 512, # max length of description in tooltips
tooltip_max_len => 512, # max length of description in tooltips

thousand_separator => q{,},
downloads_dir => curdir(),
Expand Down Expand Up @@ -806,6 +809,10 @@ my $yv_obj = WWW::YoutubeViewer->new(
hl => $CONFIG{hl},
lwp_env_proxy => $CONFIG{env_proxy},
cache_dir => $CONFIG{cache_dir},
cookie_file => $CONFIG{cookie_file},
user_agent => $CONFIG{user_agent},
http_proxy => $CONFIG{http_proxy},
timeout => $CONFIG{timeout},
authentication_file => $authentication_file,
);

Expand Down Expand Up @@ -878,7 +885,8 @@ sub apply_configuration {
videoEmbeddable videoLicense
publishedAfter publishedBefore
regionCode videoCategoryId
debug http_proxy
debug http_proxy user_agent
timeout cookie_file
)
) {

Expand Down Expand Up @@ -1434,8 +1442,8 @@ $accel->connect(ord('g'), ['control-mask'], ['visible'], \&show_user_favorited_v
$accel->connect(ord('m'), ['control-mask'], ['visible'], \&show_videos_from_selected_author);
$accel->connect(ord('k'), ['control-mask'], ['visible'], \&show_playlists_from_selected_author);
$accel->connect(ord('w'), ['control-mask'], ['visible'], \&show_warnings_window);
$accel->connect(0xffff, ['lock-mask'], ['visible'], \&delete_selected_row);
$accel->connect(0xffc8, ['lock-mask'], ['visible'], \&maximize_unmaximize_mainw);
$accel->connect(0xffff, ['lock-mask'], ['visible'], \&delete_selected_row);
$accel->connect(0xffc8, ['lock-mask'], ['visible'], \&maximize_unmaximize_mainw);
$mainw->add_accel_group($accel);

# Support for navigating back and forth using the side buttons of the mouse
Expand Down Expand Up @@ -1464,7 +1472,7 @@ $accel->connect(0xff1b, ['lock-mask'], ['visible'], \&hide_feeds_window);
$feeds_window->add_accel_group($accel);

$accel = Gtk3::AccelGroup->new;
$accel->connect(0xff1b, ['lock-mask'], ['visible'], \&hide_preferences_window);
$accel->connect(0xff1b, ['lock-mask'], ['visible'], \&hide_preferences_window);
$accel->connect(ord('s'), ['control-mask'], ['visible'], \&save_configuration);
$prefernces_window->add_accel_group($accel);

Expand Down Expand Up @@ -2077,8 +2085,8 @@ sub set_youtube_tops {
}

sub remove_selected_user {
my $selection = $users_treeview->get_selection // return;
my $iter = $selection->get_selected // return;
my $selection = $users_treeview->get_selection // return;
my $iter = $selection->get_selected // return;
my $channel_id = $users_liststore->get($iter, 0);
delete $channels{$channel_id};
$users_liststore->remove($iter);
Expand Down Expand Up @@ -2572,7 +2580,7 @@ sub display_results {

my $url = $results->{url};
my $info = $results->{results} // {};
my $items = $info->{items} // [];
my $items = $info->{items} // [];

hide_feeds_window();

Expand Down Expand Up @@ -3223,10 +3231,10 @@ sub get_options_as_arguments {
'no-interactive' => q{},
'resolution' => $CONFIG{resolution},
'download-dir' => quotemeta(rel2abs($CONFIG{downloads_dir})),
'fullscreen' => $CONFIG{fullscreen} ? q{} : undef,
'fullscreen' => $CONFIG{fullscreen} ? q{} : undef,
'no-dash' => $CONFIG{dash_support} ? undef : q{},
'no-video' => $CONFIG{audio_only} ? q{} : undef,
'resolution=audio' => $CONFIG{audio_only} ? q{} : undef,
'no-video' => $CONFIG{audio_only} ? q{} : undef,
'resolution=audio' => $CONFIG{audio_only} ? q{} : undef,
);

while (my ($argv, $value) = each %options) {
Expand Down Expand Up @@ -3431,7 +3439,7 @@ sub display_comments {

my $url = $results->{url};
my $res = $results->{results} // {};
my $comments = $res->{items} // [];
my $comments = $res->{items} // [];

foreach my $comment (@{$comments}) {
my $snippet = (($comment->{snippet} // next)->{topLevelComment} // next)->{snippet};
Expand Down
34 changes: 23 additions & 11 deletions bin/youtube-viewer
Expand Up @@ -15,7 +15,7 @@
#-------------------------------------------------------
# youtube-viewer
# Created on: 02 June 2010
# Latest edit on: 24 August 2020
# Latest edit on: 08 September 2020
# https://github.com/trizen/youtube-viewer
#-------------------------------------------------------

Expand Down Expand Up @@ -71,7 +71,7 @@ use 5.016;
use warnings;
no warnings 'once';

my $DEVEL; # true in devel mode
my $DEVEL; # true in devel mode
use if ($DEVEL = 0), lib => qw(../lib); # devel mode

use WWW::YoutubeViewer v3.7.8;
Expand Down Expand Up @@ -190,7 +190,7 @@ my %CONFIG = (
video_player_selected => (
$constant{win32}
? 'vlc'
: undef # auto-defined
: undef # auto-defined
),

# YouTube options
Expand Down Expand Up @@ -230,6 +230,9 @@ my %CONFIG = (
# Others
autoplay_mode => 0,
http_proxy => undef,
cookie_file => undef,
user_agent => undef,
timeout => undef,
env_proxy => 1,
confirm => 0,
debug => 0,
Expand Down Expand Up @@ -618,6 +621,10 @@ my $yv_obj = WWW::YoutubeViewer->new(
config_dir => $config_dir,
cache_dir => $opt{cache_dir},
lwp_env_proxy => $opt{env_proxy},
cookie_file => $opt{cookie_file},
http_proxy => $opt{http_proxy},
user_agent => $opt{user_agent},
timeout => $opt{timeout},
authentication_file => $authentication_file,
);

Expand Down Expand Up @@ -833,6 +840,8 @@ usage: $execname [options] ([url] | [keywords])
--use-colors! : enable or disable the ANSI colors for text
* Other
--cookies=s : file to read cookies from and dump cookie
--user-agent=s : specify a custom user agent
--proxy=s : set HTTP(S)/SOCKS proxy: 'proto://domain.tld:port/'
If authentication required,
use 'proto://user:pass\@domain.tld:port/'
Expand Down Expand Up @@ -1133,7 +1142,8 @@ sub apply_configuration {
publishedAfter publishedBefore
safeSearch regionCode debug hl
http_proxy page comments_order
subscriptions_order
subscriptions_order user_agent
cookie_file timeout
)
) {

Expand Down Expand Up @@ -1554,6 +1564,8 @@ sub parse_arguments {
'related-videos|rv=s' => \$opt{related_videos},
'popular-videos|popular|pv=s' => \$opt{popular_videos},

'cookie-file|cookies=s' => \$opt{cookie_file},
'user-agent|agent=s' => \$opt{user_agent},
'http_proxy|http-proxy|proxy=s' => \$opt{http_proxy},

'catlang|cl|hl=s' => \$opt{hl},
Expand Down Expand Up @@ -2548,7 +2560,7 @@ sub print_channels {

my $url = $results->{url};
my $info = $results->{results} // {};
my $channels = $info->{items} // [];
my $channels = $info->{items} // [];

foreach my $i (0 .. $#{$channels}) {
my $channel = $channels->[$i];
Expand Down Expand Up @@ -2662,7 +2674,7 @@ sub print_comments {

my $url = $results->{url};
my $info = $results->{results} // {};
my $comments = $info->{items} // [];
my $comments = $info->{items} // [];

my $i = 0;
foreach my $comment (@{$comments}) {
Expand Down Expand Up @@ -2850,7 +2862,7 @@ sub print_playlists {

my $url = $results->{url};
my $info = $results->{results} // {};
my $playlists = $info->{items} // [];
my $playlists = $info->{items} // [];

state $info_format = <<"FORMAT";
Expand Down Expand Up @@ -3046,7 +3058,7 @@ sub parse_page_number {

$page_number = int($total / $per_page);
$page_number += 1 if ($total % $per_page != 0);
$page_number = 1 if ($page_number <= 0);
$page_number = 1 if ($page_number <= 0);
}
}
elsif ($page_number =~ /first|beg/) {
Expand Down Expand Up @@ -3122,7 +3134,7 @@ sub get_streaming_url {
resolution => ($opt{novideo} ? 'audio' : $opt{resolution}),
hfr => $opt{hfr},
dash => $dash,
dash_mp4_audio => ($opt{novideo} ? 1 : $opt{dash_mp4_audio}),
dash_mp4_audio => ($opt{novideo} ? 1 : $opt{dash_mp4_audio}),
dash_segmented => ($opt{download_video} ? 0 : $opt{dash_segmented}),
);

Expand Down Expand Up @@ -3392,7 +3404,7 @@ sub get_player_command {

$MPLAYER{fullscreen} = $opt{fullscreen} ? $opt{video_players}{$opt{video_player_selected}}{fs} // '' : q{};
$MPLAYER{novideo} = $opt{novideo} ? $opt{video_players}{$opt{video_player_selected}}{novideo} // '' : q{};
$MPLAYER{arguments} = $opt{video_players}{$opt{video_player_selected}}{arg} // q{};
$MPLAYER{arguments} = $opt{video_players}{$opt{video_player_selected}}{arg} // q{};

my $cmd = join(
q{ },
Expand Down Expand Up @@ -3647,7 +3659,7 @@ sub print_videos {

my $url = $results->{url};
my $info = $results->{results} // {};
my $videos = $info->{items} // [];
my $videos = $info->{items} // [];

foreach my $entry (@$videos) {
if ($yv_utils->is_activity($entry)) {
Expand Down

0 comments on commit 940b2c5

Please sign in to comment.