| 1 | package LWP::UserAgent::OfflineCache; | ||
| 2 | use Moose; | ||
| 3 | extends 'LWP::UserAgent', 'Moose::Object'; | ||
| 4 | use IO::All; | ||
| 5 | use HTTP::Message; | ||
| 6 | use Digest::MD5 qw/md5_hex/; | ||
| 7 | use File::Path qw/mkpath/; | ||
| 8 | use Fatal qw/mkpath/; | ||
| 9 | use Log::Log4perl; | ||
| 10 | |||
| 11 | =head1 ABSTRACT | ||
| 12 | |||
| 13 | LWP::UserAgent::OfflineCache - a caching user agent | ||
| 14 | |||
| 15 | =head1 VERSION | ||
| 16 | |||
| 17 | Version 0.02 | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | our $VERSION = '0.02'; | ||
| 22 | |||
| 23 | use constant DEFAULT_CACHE_DIR_NAME => 'cache'; | ||
| 24 | |||
| 25 | |||
| 26 | =head1 SYNOPSIS | ||
| 27 | |||
| 28 | This module will cache all GET requests to disk. It otherwise | ||
| 29 | behaves the same as LWP::UserAgent. | ||
| 30 | |||
| 31 | Caching performs regardsless of any returned HTTP headers. | ||
| 32 | |||
| 33 | Simple example: | ||
| 34 | |||
| 35 | #!/usr/bin/perl -w | ||
| 36 | use strict; | ||
| 37 | use LWP::UserAgent::OfflineCache; | ||
| 38 | my $ua = LWP::UserAgent::OfflineCache->new; | ||
| 39 | print $ua->get("http://www.yahoo.com/index.html"); | ||
| 40 | |||
| 41 | |||
| 42 | More advanced example | ||
| 43 | |||
| 44 | #!/usr/bin/perl -w | ||
| 45 | use strict; | ||
| 46 | use LWP::UserAgent::OfflineCache; | ||
| 47 | use Log::Log4perl; | ||
| 48 | |||
| 49 | my $logConf = q( | ||
| 50 | log4perl.category.lwp.useragent.offlinecache = INFO, Screen | ||
| 51 | log4perl.appender.Screen = Log::Log4perl::Appender::Screen | ||
| 52 | log4perl.appender.Screen.stderr = 0 | ||
| 53 | log4perl.appender.Screen.layout = Log::Log4perl::Layout::SimpleLayout | ||
| 54 | ); | ||
| 55 | Log::Log4perl::init( \$logConf ); | ||
| 56 | |||
| 57 | |||
| 58 | my $ua = LWP::UserAgent::OfflineCache->new(cache_dir=> 'cache', delay=>3, use_gzip=>1); | ||
| 59 | # The same as my $ua = LWP::UserAgent::OfflineCache->new; | ||
| 60 | |||
| 61 | print $ua->get("http://www.yahoo.com/index.html"); | ||
| 62 | |||
| 63 | =head1 DESCRIPTION | ||
| 64 | |||
| 65 | The C<LWP::UserAgent::OfflineCache> is a class-wrapper for C<LWP::UserAgent>. | ||
| 66 | |||
| 67 | ::OfflineCache module allows to use offline cache for debug and logging purpose. | ||
| 68 | |||
| 69 | Also by default ::OfflineCache tries to use 'Accept-Encoding' HTTP header | ||
| 70 | for using less bandwidth. | ||
| 71 | |||
| 72 | Cached content stored as a files with names md5($url) in the specified directory. | ||
| 73 | |||
| 74 | =head1 CONSTRUCTOR METHOD | ||
| 75 | |||
| 76 | =over 4 | ||
| 77 | |||
| 78 | =item $ua = LWP::UserAgent::OfflineCache->new(%options) | ||
| 79 | |||
| 80 | In additional to standard LWP::UserAgent's options the following | ||
| 81 | key/pairs values are available: | ||
| 82 | |||
| 83 | KEY DEFAULT | ||
| 84 | ---------- ------------------ | ||
| 85 | cache_dir cache (means './cache') | ||
| 86 | delay 3 (seconds) | ||
| 87 | use_gzip 1 | ||
| 88 | |||
| 89 | =head1 ATTRIBUTES | ||
| 90 | |||
| 91 | =over | ||
| 92 | |||
| 93 | =item $ua->cache_dir | ||
| 94 | |||
| 95 | Get directory where cached content stored as files with names as md5 hash | ||
| 96 | from requested url. | ||
| 97 | |||
| 98 | =item $ua->delay | ||
| 99 | |||
| 100 | Get/set delay in seconds after each request to the server. | ||
| 101 | Default value is 3. | ||
| 102 | |||
| 103 | =item $ua->use_gzip | ||
| 104 | |||
| 105 | Get/set boolean flag for automatic usage of 'gzip' (HTTP::Message::decodable()) in | ||
| 106 | 'Accept-Encoding' header. | ||
| 107 | Switched on by default. | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | # as suggested in Moose FAQ | ||
| 112 | # explicit constructor for non-Moose superclasses | ||
| 113 | sub new { | ||
| 114 | my $class = shift; | ||
| 115 | my $self; | ||
| 116 | { | ||
| 117 | local $^W; # switch off Unrecognized LWP::UserAgent options... | ||
| 118 | # specific only for implementation of LWP::UserAgent | ||
| 119 | my $obj = $class->SUPER::new(@_); | ||
| 120 | $self= $class->meta->new_object(__INSTANCE__ => $obj, @_); | ||
| 121 | } | ||
| 122 | $self->BUILD(); | ||
| 123 | $self; | ||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | has 'cache_dir' => ( | ||
| 128 | is => 'ro', | ||
| 129 | isa => 'Str', | ||
| 130 | default => DEFAULT_CACHE_DIR_NAME | ||
| 131 | ); | ||
| 132 | |||
| 133 | has 'delay' => ( | ||
| 134 | is => 'rw', | ||
| 135 | isa => 'Int', | ||
| 136 | default => 3 | ||
| 137 | ); | ||
| 138 | |||
| 139 | has 'use_gzip' => ( | ||
| 140 | is => 'rw', | ||
| 141 | isa => 'Bool', | ||
| 142 | default => '1' | ||
| 143 | ); | ||
| 144 | |||
| 145 | has 'logger' => ( | ||
| 146 | is => 'ro', | ||
| 147 | isa => 'Object' | ||
| 148 | ); | ||
| 149 | |||
| 150 | |||
| 151 | sub BUILD { | ||
| 152 | my $self = shift; | ||
| 153 | $self->{logger} = Log::Log4perl->get_logger('lwp.useragent.offlinecache'); | ||
| 154 | my $dir = $self->cache_dir; | ||
| 155 | |||
| 156 | if (!-d $dir) { | ||
| 157 | mkpath($dir); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | around 'get' => sub { | ||
| 162 | my $orig = shift; | ||
| 163 | my $self = shift; | ||
| 164 | my $url = shift; | ||
| 165 | |||
| 166 | my $logger = $self->logger; | ||
| 167 | my $file = $self->_url_to_file($url); | ||
| 168 | if (-e $file) { | ||
| 169 | $logger->info("Returning $url from $file"); | ||
| 170 | return io($file)->all(); | ||
| 171 | } | ||
| 172 | |||
| 173 | my $html; | ||
| 174 | if ($self->use_gzip) { | ||
| 175 | my $can_accept = HTTP::Message::decodable; | ||
| 176 | my $resp = $orig->($self, $url, 'Accept-Encoding' => $can_accept); | ||
| 177 | my $msg = 'Raw '. length ($resp->content). ' bytes,'; | ||
| 178 | $html = $resp->decoded_content('charset'=> 'none'); | ||
| 179 | $logger->info($msg ,' uncompressed ', length $html, ' bytes'); | ||
| 180 | } | ||
| 181 | else { | ||
| 182 | my $resp = $orig->($self, $url); | ||
| 183 | $html = $resp->content; | ||
| 184 | } | ||
| 185 | |||
| 186 | io($file)->print($html); | ||
| 187 | $logger->info("Saved $url to $file"); | ||
| 188 | sleep $self->delay; | ||
| 189 | return $html; | ||
| 190 | }; | ||
| 191 | |||
| 192 | |||
| 193 | sub _url_to_file { | ||
| 194 | my $self = shift; | ||
| 195 | my $cd = $self->cache_dir; | ||
| 196 | return join '/', $cd, md5_hex(shift); | ||
| 197 | } | ||
| 198 | |||
| 199 | |||
| 200 | =head1 AUTHORS | ||
| 201 | |||
| 202 | =over | ||
| 203 | |||
| 204 | =item Luke Closs, C<< <cpan at 5thplane.com> >> | ||
| 205 | |||
| 206 | =item Dmytro Gorbunov, C<< <gdm at savesources.com> >> | ||
| 207 | |||
| 208 | =back | ||
| 209 | |||
| 210 | =head1 SUPPORT | ||
| 211 | |||
| 212 | You can find documentation for this module with the perldoc command. | ||
| 213 | |||
| 214 | perldoc LWP::UserAgent::OfflineCache | ||
| 215 | |||
| 216 | =head1 ACKNOWLEDGEMENTS | ||
| 217 | |||
| 218 | Thanks to Luke Closs for an idea and perfect implementation of | ||
| 219 | Net::Parliament::UserAgent module. | ||
| 220 | |||
| 221 | =head1 SEE ALSO | ||
| 222 | |||
| 223 | =over | ||
| 224 | |||
| 225 | =item LWP::UserAgent::Snapshot - same purpose as this module | ||
| 226 | |||
| 227 | =item LWP::Online - answer the question "Am I online?" | ||
| 228 | |||
| 229 | =item LWP::UserAgent::WithCache - acts like a browser with cache | ||
| 230 | |||
| 231 | =item LWP::Simple::WithCache - simplify an access to previous one | ||
| 232 | |||
| 233 | =item LWP::UserAgent::Cache::Memcached - same, but with memcache | ||
| 234 | |||
| 235 | =back | ||
| 236 | |||
| 237 | =head1 COPYRIGHT & LICENSE | ||
| 238 | |||
| 239 | =over | ||
| 240 | |||
| 241 | =item Copyright 2009 Luke Closs, all rights reserved. | ||
| 242 | |||
| 243 | =item Copyright 2010 Dmytro Gorbunov, all rights reserved. | ||
| 244 | |||
| 245 | =back | ||
| 246 | |||
| 247 | This program is free software; you can redistribute it and/or modify it | ||
| 248 | under the same terms as Perl itself. | ||
| 249 | |||
| 250 | =cut | ||
| 251 | |||
| 252 | 1; | ||
| 253 |