diff options
-rwxr-xr-x | bin/fair-viewer | 1 | ||||
-rwxr-xr-x | bin/gtk-fair-viewer | 1 | ||||
-rw-r--r-- | lib/WWW/FairViewer/GetCaption.pm | 46 |
3 files changed, 11 insertions, 37 deletions
diff --git a/bin/fair-viewer b/bin/fair-viewer index b1a9c4b..8468f28 100755 --- a/bin/fair-viewer +++ b/bin/fair-viewer @@ -3051,6 +3051,7 @@ sub get_streaming_url { captions_dir => $opt{cache_dir}, captions => $captions, languages => $CONFIG{srt_languages}, + yv_obj => $yv_obj, ); $srt_file = $yv_cap->save_caption($video_id); } diff --git a/bin/gtk-fair-viewer b/bin/gtk-fair-viewer index fadbf18..8cd773a 100755 --- a/bin/gtk-fair-viewer +++ b/bin/gtk-fair-viewer @@ -3017,6 +3017,7 @@ sub get_streaming_url { captions_dir => $CONFIG{cache_dir}, captions => $captions, languages => $CONFIG{srt_languages}, + yv_obj => $yv_obj, ); $srt_file = $yv_cap->save_caption($video_id); } diff --git a/lib/WWW/FairViewer/GetCaption.pm b/lib/WWW/FairViewer/GetCaption.pm index 83b699b..710a2af 100644 --- a/lib/WWW/FairViewer/GetCaption.pm +++ b/lib/WWW/FairViewer/GetCaption.pm @@ -13,8 +13,7 @@ WWW::FairViewer::GetCaption - Save the YouTube closed captions as .srt files for use WWW::FairViewer::GetCaption; my $yv_cap = WWW::FairViewer::GetCaption->new(%opts); - - print $yv_cap->get_caption($videoID); + my $file = $yv_cap->save_caption($videoID); =head1 SUBROUTINES/METHODS @@ -44,16 +43,23 @@ sub new { my ($class, %opts) = @_; my $self = bless {}, $class; + $self->{captions_dir} = undef; $self->{captions} = []; $self->{auto_captions} = 0; $self->{languages} = [qw(en es)]; + $self->{yv_obj} = undef; foreach my $key (keys %{$self}) { $self->{$key} = delete $opts{$key} if exists $opts{$key}; } + $self->{yv_obj} //= do { + require WWW::FairViewer; + WWW::FairViewer->new(cache_dir => $self->{captions_dir},); + }; + foreach my $invalid_key (keys %opts) { warn "Invalid key: '${invalid_key}'"; } @@ -184,41 +190,7 @@ Get the XML content for a given caption data. sub get_xml_data { my ($self, $url) = @_; - - state $lwp = do { - - require LWP::UserAgent; - - my $agent = LWP::UserAgent->new( - timeout => 30, - env_proxy => 1, - agent => - 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36', - ); - - require LWP::ConnCache; - state $cache = LWP::ConnCache->new; - $cache->total_capacity(undef); # no limit - - state $accepted_encodings = do { - require HTTP::Message; - HTTP::Message::decodable(); - }; - - $agent->ssl_opts(Timeout => 30); - $agent->default_header('Accept-Encoding' => $accepted_encodings); - $agent->conn_cache($cache); - - $agent; - }; - - my $req = $lwp->get($url); - - if ($req->is_success) { - return $req->decoded_content; - } - - return; + $self->{yv_obj}->lwp_get($url, simple => 1); } =head2 save_caption($video_ID) |