diff options
author | trizen <trizen@protonmail.com> | 2020-09-09 15:33:20 +0300 |
---|---|---|
committer | Jesús <heckyel@hyperbola.info> | 2020-09-14 11:06:49 -0500 |
commit | 06b67e37a7a591ec844bd454033042e6bb198734 (patch) | |
tree | 2b2135981c25e2c167add4199c09842b1bf3cf69 /lib | |
parent | 0ba7b35af0acd66807996abcf81d62517f4ad796 (diff) | |
download | fair-viewer-06b67e37a7a591ec844bd454033042e6bb198734.tar.lz fair-viewer-06b67e37a7a591ec844bd454033042e6bb198734.tar.xz fair-viewer-06b67e37a7a591ec844bd454033042e6bb198734.zip |
Reuse the "WWW::FairViewer" in "WWW::FairViewer::GetCaption".
Signed-off-by: Jesús <heckyel@hyperbola.info>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/WWW/FairViewer/GetCaption.pm | 46 |
1 files changed, 9 insertions, 37 deletions
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) |