| 1 | #WWW/monitor.pm. Written in 2007 by Yaron Kahanoitch. This | ||
| 2 | # source code has been placed in the public domain by the author. | ||
| 3 | # Please be kind and preserve the documentation. | ||
| 4 | |||
| 5 | package WWW::Monitor; | ||
| 6 | |||
| 7 | use 5.005; | ||
| 8 | use warnings; | ||
| 9 | use strict; | ||
| 10 | use Carp; | ||
| 11 | use WWW::Monitor::Task; | ||
| 12 | use WWW::Mechanize; | ||
| 13 | use HTML::FormatText; | ||
| 14 | use File::HomeDir; | ||
| 15 | |||
| 16 | our(@ISA, @EXPORT, @EXPORT_OK, $VERSION); | ||
| 17 | |||
| 18 | $VERSION = 0.24; | ||
| 19 | |||
| 20 | use base qw(Exporter WWW::Mechanize); | ||
| 21 | |||
| 22 | |||
| 23 | @EXPORT = qw (); | ||
| 24 | @EXPORT_OK = qw (); | ||
| 25 | |||
| 26 | our $DEFAULT_CACHE_SUBDIR=".www-monitor"; | ||
| 27 | |||
| 28 | =head1 NAME | ||
| 29 | |||
| 30 | WWW::Monitor - Monitor websites for updates and changes | ||
| 31 | |||
| 32 | =head1 VERSION | ||
| 33 | |||
| 34 | Version 0.01 | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | =head1 SYNOPSIS | ||
| 39 | |||
| 40 | use MIME::Lite; | ||
| 41 | use WWW::Monitor; | ||
| 42 | sub notify { | ||
| 43 | my ($url,$text) =@_; | ||
| 44 | foreach my $recipient ('user1@host','user2@host2') { | ||
| 45 | my $mail_obj = | ||
| 46 | MIME::Lite->new(To=>$recipient, | ||
| 47 | From=>'from@myHost', | ||
| 48 | Subject=>"Web alert web page changed", | ||
| 49 | Type=>'Text', | ||
| 50 | Data=>'For details visit '.$url."\n".$text | ||
| 51 | ); | ||
| 52 | $mail_obj->send; | ||
| 53 | } | ||
| 54 | return 1; | ||
| 55 | } | ||
| 56 | my $mon = WWW::Monitor->new('MAIL_CALLBACK'=>\¬ify); | ||
| 57 | $mon->watch('http:://www.kahanovitch.com/'); | ||
| 58 | $mon->run; | ||
| 59 | |||
| 60 | |||
| 61 | Or: | ||
| 62 | |||
| 63 | use WWW::Monitor; | ||
| 64 | my $mon=WWW::Monitor->new('MAIL_CALLBACK'=>\¬ify,'CACHE'=>$cache); | ||
| 65 | my $task = $mon->watch("$url"); | ||
| 66 | $mon->run or die "Query ended with error"; | ||
| 67 | |||
| 68 | sun notify { | ||
| 69 | my ($url,$task) =@_; | ||
| 70 | print "$url has changed\n"; | ||
| 71 | while (my ($sub_url,$ref_http_response) = each %{$task->added_parts()}) { | ||
| 72 | print "New part added: $sub_url \n"; | ||
| 73 | } | ||
| 74 | |||
| 75 | while (my ($sub_url,$ref_http_response) = each %{$task->missing_parts()}) { | ||
| 76 | print "Part deleted: $sub_url \n"; | ||
| 77 | } | ||
| 78 | |||
| 79 | foreach my $sub_url ( $task->changed_parts()) { | ||
| 80 | print "$sub_url has changed:\n"; | ||
| 81 | my ($old,$new) = $task->get_old_new_pair($sub_url); | ||
| 82 | my $old_content = $old->content; | ||
| 83 | my $new_content = $new->content; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | =head1 Description | ||
| 88 | |||
| 89 | L<WWW::Monitor> ia a Web monitoring mechanism built to detect and | ||
| 90 | notify changes in web pages. The module is designed to compare | ||
| 91 | existing, online versions and pre-cached matched version. A web page | ||
| 92 | may include more than one file. A page may include some frames and | ||
| 93 | visible referenced data, which all together form a sigle visible page. | ||
| 94 | For now, WWW::Monitor compares only textual information. Images, and | ||
| 95 | non-HTML data are not being compared. To store information, | ||
| 96 | WWW::Monitor caches data with the "Cache" mechanism. By default, | ||
| 97 | Cache::File is being used, but the user may choose to use any Cache | ||
| 98 | object that implements the Cache module interface. L<WWW::Monitor> is | ||
| 99 | a subclass of L<WWW::Mechanize>, so any of L<WWW::Mechanize> or its | ||
| 100 | super classes can be used. | ||
| 101 | |||
| 102 | =head1 EXPORT | ||
| 103 | |||
| 104 | =head1 FUNCTIONS | ||
| 105 | |||
| 106 | =head2 new ( [ OPTIONS ] ) | ||
| 107 | |||
| 108 | A constructor. OPTIONS are passed in a hash like fashion, using key | ||
| 109 | and value pairs. Possible options are: URL - A target URL to monitor. | ||
| 110 | CACHE_ROOT - A root directory under which all caching is being | ||
| 111 | managed. Default = <home directory>/.www-monitor CACHE - cache | ||
| 112 | object. The object must have get() and set() methods like the Cache | ||
| 113 | interface, as well as set_validity and validity. | ||
| 114 | |||
| 115 | =cut | ||
| 116 | |||
| 117 | sub new { | ||
| 118 | my $this = shift; | ||
| 119 | my $class = ref($this) || $this; | ||
| 120 | my %args; | ||
| 121 | unless (@_ % 2) { | ||
| 122 | %args = @_; | ||
| 123 | } else { | ||
| 124 | carp( "Parameters for WWW::Monitor should be given as pair of 'OPTION'=>'VAL'"); | ||
| 125 | } | ||
| 126 | my $cache_root = delete $args{CACHE_ROOT}; | ||
| 127 | unless ($cache_root) { | ||
| 128 | my $def_dir = File::HomeDir->my_home."/".$DEFAULT_CACHE_SUBDIR; | ||
| 129 | if (!-d $def_dir && !mkdir($def_dir)) { | ||
| 130 | carp("directory $def_dir does not exists and cannot be created.$!"); | ||
| 131 | return 0; | ||
| 132 | } | ||
| 133 | $cache_root = $def_dir; | ||
| 134 | } | ||
| 135 | my $cache = delete $args{CACHE}; | ||
| 136 | if ($cache) { | ||
| 137 | unless ((ref($cache) ne "HASH") && | ||
| 138 | $cache->can("get") && | ||
| 139 | $cache->can("set") && | ||
| 140 | $cache->can("set_validity") && | ||
| 141 | $cache->can("get_validity") && | ||
| 142 | $cache->can("exists")) { | ||
| 143 | carp "The given CACHE object must implements Cache interface and must be initialized"; | ||
| 144 | $cache = ""; | ||
| 145 | } | ||
| 146 | } else { | ||
| 147 | require Cache::File; | ||
| 148 | $cache = Cache::File->new( cache_root => $cache_root); | ||
| 149 | } | ||
| 150 | my $mailcallback = delete $args{MAIL_CALLBACK}; | ||
| 151 | my $self=$class->SUPER::new(%args); | ||
| 152 | $self->{tasks} = []; | ||
| 153 | $self->{cache_root} = $cache_root; | ||
| 154 | $cache = ($cache)?$cache:Cache::File->new( cache_root => $self->{cache_root}); | ||
| 155 | $self->{cache} = $cache; | ||
| 156 | $self->{errors_hash} = {}; | ||
| 157 | $self->{mailcallback} = $mailcallback if ($mailcallback); | ||
| 158 | return $self; | ||
| 159 | } | ||
| 160 | |||
| 161 | =head2 watch ( URL(S) ) | ||
| 162 | |||
| 163 | Add URL to be watched. | ||
| 164 | watch returns a reference to a L<WWW::Monitor::Task> object. | ||
| 165 | for example $obj->watch('http://www.cnn.com' ) | ||
| 166 | |||
| 167 | =cut | ||
| 168 | |||
| 169 | sub watch { | ||
| 170 | my $self = shift; | ||
| 171 | my $target = shift; | ||
| 172 | my $task = WWW::Monitor::Task->new('URL',$target); | ||
| 173 | push @{$self->{tasks}},$task; | ||
| 174 | return $task; | ||
| 175 | } | ||
| 176 | |||
| 177 | |||
| 178 | =head2 notify_callback ( sub ) | ||
| 179 | |||
| 180 | A code reference to be executed whenever a change is detected | ||
| 181 | (commonly used for sending mail). The following parameters will be | ||
| 182 | passed to the code reference: | ||
| 183 | $url -> a string that holds the url for which a change was detected. | ||
| 184 | $text -> A Message to be sent. | ||
| 185 | $task -> WWW::Monitor::Task object reference. | ||
| 186 | The given code reference should return true for success. | ||
| 187 | |||
| 188 | =cut | ||
| 189 | |||
| 190 | sub notify_callback { | ||
| 191 | my $self = shift; | ||
| 192 | $self->{mailcallback} = shift; | ||
| 193 | return 1; | ||
| 194 | } | ||
| 195 | |||
| 196 | =head2 run | ||
| 197 | |||
| 198 | Watch all given web pages and report changes if detected. If a url is | ||
| 199 | first visited (i.e. the url is not in the cache db) than the url will | ||
| 200 | be cached and no report will be created. | ||
| 201 | |||
| 202 | |||
| 203 | =cut | ||
| 204 | |||
| 205 | sub run { | ||
| 206 | my $self = shift; | ||
| 207 | my $carrier = $self; | ||
| 208 | my $cache = $self->{cache}; | ||
| 209 | my $ret = 1; | ||
| 210 | $self->{errors_hash} = {}; | ||
| 211 | foreach my $task (@{$self->{tasks}}) { | ||
| 212 | $task->run($self,$carrier,$cache) or $ret = 0; | ||
| 213 | } | ||
| 214 | return $ret; | ||
| 215 | } | ||
| 216 | |||
| 217 | =head2 errors_table | ||
| 218 | |||
| 219 | Return a hash reference of errors updated to last execution (i.e. when | ||
| 220 | the run method was last executed). The returned keys are the urls where | ||
| 221 | the values are error descriptions. | ||
| 222 | |||
| 223 | =cut | ||
| 224 | |||
| 225 | sub errors_table { | ||
| 226 | my $self = shift; | ||
| 227 | my $ret_hash = {}; | ||
| 228 | foreach my $task (@{$self->{tasks}}) { | ||
| 229 | $ret_hash->{$task->{url}} = $task->{error} unless $task->success(); | ||
| 230 | } | ||
| 231 | while (my($url,$error) = each %{$self->{errors_hash}}) { | ||
| 232 | $ret_hash->{$url} = $error; | ||
| 233 | } | ||
| 234 | return $ret_hash; | ||
| 235 | } | ||
| 236 | |||
| 237 | =head2 errors | ||
| 238 | |||
| 239 | return a string that contains all errors. In array context return a list of errors. | ||
| 240 | |||
| 241 | =cut | ||
| 242 | |||
| 243 | sub errors { | ||
| 244 | my $self=shift; | ||
| 245 | my $all_errors_hash = $self->errors_table; | ||
| 246 | my @list_of_errors; | ||
| 247 | while (my($url,$error) = each %$all_errors_hash) { | ||
| 248 | push @list_of_errors,$url.":".$error; | ||
| 249 | } | ||
| 250 | return @list_of_errors if (wantarray); | ||
| 251 | return join("\n",@list_of_errors); | ||
| 252 | } | ||
| 253 | |||
| 254 | =head2 notify | ||
| 255 | |||
| 256 | (Private Method) | ||
| 257 | Activate notification callback | ||
| 258 | |||
| 259 | =cut | ||
| 260 | |||
| 261 | sub notify { | ||
| 262 | my $self = shift; | ||
| 263 | my ($url,$task) = @_; | ||
| 264 | if (exists $self->{mailcallback} and $self->{mailcallback}) { | ||
| 265 | return &{$self->{mailcallback}}($url,$task); | ||
| 266 | } | ||
| 267 | return 1; | ||
| 268 | } | ||
| 269 | |||
| 270 | =head2 targets | ||
| 271 | |||
| 272 | Return a list of strings out of watched targets. | ||
| 273 | |||
| 274 | =cut | ||
| 275 | |||
| 276 | sub targets { | ||
| 277 | my $self = shift; | ||
| 278 | my @res = (); | ||
| 279 | foreach my $task (@{$self->{tasks}}) { | ||
| 280 | push @res,$task->{url}; | ||
| 281 | } | ||
| 282 | return @res; | ||
| 283 | } | ||
| 284 | |||
| 285 | |||
| 286 | |||
| 287 | =head1 AUTHOR | ||
| 288 | |||
| 289 | Yaron Kahanovitch, C<< <yaron-helpme at kahanovitch.com> >> | ||
| 290 | |||
| 291 | =head1 BUGS | ||
| 292 | |||
| 293 | Please report any bugs or feature requests to | ||
| 294 | C<bug-www-monitor at rt.cpan.org>, or through the web interface at | ||
| 295 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-Monitor>. | ||
| 296 | I will be notified, and then you'll automatically be notified of progress on | ||
| 297 | your bug as I make changes. | ||
| 298 | |||
| 299 | =head1 SUPPORT | ||
| 300 | |||
| 301 | You can find documentation for this module with the perldoc command. perldoc WWW::Monitor | ||
| 302 | |||
| 303 | =over | ||
| 304 | |||
| 305 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 306 | |||
| 307 | L<http://annocpan.org/dist/WWW-Monitor> | ||
| 308 | |||
| 309 | =item * CPAN Ratings | ||
| 310 | |||
| 311 | L<http://cpanratings.perl.org/d/WWW-Monitor> | ||
| 312 | |||
| 313 | =item * RT: CPAN's request tracker | ||
| 314 | |||
| 315 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-Monitor> | ||
| 316 | |||
| 317 | =item * Search CPAN | ||
| 318 | |||
| 319 | L<http://search.cpan.org/dist/WWW-Monitor> | ||
| 320 | |||
| 321 | =back | ||
| 322 | |||
| 323 | =head1 ACKNOWLEDGMENTS | ||
| 324 | |||
| 325 | =head1 COPYRIGHT & LICENSE | ||
| 326 | |||
| 327 | Copyright 2007 Yaron Kahanovitch, all rights reserved. | ||
| 328 | This program is free software; you can redistribute it and/or modify it | ||
| 329 | under the same terms as Perl itself. | ||
| 330 | |||
| 331 | =cut | ||
| 332 | |||
| 333 | 1; # End of WWW::Monitor |