Skip to content

Commit b2d68d5

Browse files
committed
- Minor performance improvent in downloading closed-caption.
Set connection cache and the `Accept-Encoding` header.
1 parent 6a40849 commit b2d68d5

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

lib/WWW/YoutubeViewer/GetCaption.pm

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,32 @@ Get the XML content for a given caption data.
197197
sub get_xml_data {
198198
my ($self, $url) = @_;
199199

200-
require LWP::UserAgent;
201-
state $lwp = LWP::UserAgent->new(
202-
timeout => 30,
203-
env_proxy => 1,
204-
agent => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36',
205-
);
200+
state $lwp = do {
201+
202+
require LWP::UserAgent;
203+
204+
my $agent = LWP::UserAgent->new(
205+
timeout => 30,
206+
env_proxy => 1,
207+
agent =>
208+
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36',
209+
);
210+
211+
require LWP::ConnCache;
212+
my $cache = LWP::ConnCache->new;
213+
$cache->total_capacity(undef); # no limit
214+
215+
my $accepted_encodings = do {
216+
require HTTP::Message;
217+
HTTP::Message::decodable();
218+
};
219+
220+
$agent->ssl_opts(Timeout => 30);
221+
$agent->default_header('Accept-Encoding' => $accepted_encodings);
222+
$agent->conn_cache($cache);
223+
224+
$agent;
225+
};
206226

207227
my $req = $lwp->get($url);
208228

0 commit comments

Comments
 (0)