From: Ralf Ertzinger Date: Thu, 15 Aug 2013 14:44:27 +0000 (+0200) Subject: Base: add simple_get() and ua() functions X-Git-Url: https://git.camperquake.de/gitweb.cgi?p=quotesite.git;a=commitdiff_plain;h=31f6aa5cd983c20d38e04efd14ac1d07a0e15cde Base: add simple_get() and ua() functions --- diff --git a/quotesite/Base.pm b/quotesite/Base.pm index ccbb657..ebd65c3 100644 --- a/quotesite/Base.pm +++ b/quotesite/Base.pm @@ -4,6 +4,8 @@ package quotesite::Base; use strict; +use LWP::UserAgent; +use HTTP::Cookies; use Data::Dumper; sub new { @@ -170,4 +172,30 @@ sub setdebug { $self->{'_DEBUG'} = shift; } +sub ua { + my $self = shift; + my $ua; + + $ua = LWP::UserAgent->new( + 'agent' => 'Mozilla/5.0', + 'cookie_jar' => HTTP::Cookies->new, + 'timeout' => 15, + ); + + $self->{_CACHED_UA} = $ua; + + return $ua; +} + +sub simple_get { + my $self = shift; + my $url = shift; + my $ua = shift || $self->ua(); + my $r; + + $r = $ua->get($url); + return $r->decoded_content() if $r->is_success(); + return undef; +} + 1;