| 1 | package WWW::Leech::Walker; | ||
| 2 | use strict; | ||
| 3 | use LWP; | ||
| 4 | use URI::URL; | ||
| 5 | use WWW::Leech::Parser; | ||
| 6 | |||
| 7 | BEGIN { | ||
| 8 | use Exporter (); | ||
| 9 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); | ||
| 10 | $VERSION = '0.04'; | ||
| 11 | @ISA = qw(Exporter); | ||
| 12 | @EXPORT = qw(); | ||
| 13 | @EXPORT_OK = qw(); | ||
| 14 | %EXPORT_TAGS = (); | ||
| 15 | } | ||
| 16 | |||
| 17 | sub new{ | ||
| 18 | my $class = shift; | ||
| 19 | my $this = shift; | ||
| 20 | |||
| 21 | $this->{'url'} = url $this->{'url'}; | ||
| 22 | $this->{'state'} ||= {}; | ||
| 23 | |||
| 24 | $this->{'_stop_flag'} = undef; | ||
| 25 | |||
| 26 | $this = bless $this, __PACKAGE__; | ||
| 27 | |||
| 28 | $this->{'parser'} = new WWW::Leech::Parser($this->{'parser'}); | ||
| 29 | |||
| 30 | if(!$this->{'raw_preprocessor'}){ | ||
| 31 | $this->{'raw_preprocessor'} = sub { | ||
| 32 | return shift(); | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | return $this; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub leech{ | ||
| 40 | my $this = shift; | ||
| 41 | $this->{'_stop_flag'} = undef; | ||
| 42 | |||
| 43 | my $current_url = $this->{'url'} or die("No url provided"); | ||
| 44 | |||
| 45 | while(1){ | ||
| 46 | |||
| 47 | $this->log("Getting links list: $current_url "); | ||
| 48 | |||
| 49 | # get and parse link list | ||
| 50 | my $data = $this->_get($current_url, $this->{'post_data'}); | ||
| 51 | |||
| 52 | my $parse_result = $this->{'parser'}->parseList($data); | ||
| 53 | |||
| 54 | my $links = [map { url($_, $this->{'url'})->abs } @{$parse_result->{'links'}}]; | ||
| 55 | my $links_text = $parse_result->{'links_text'}; | ||
| 56 | |||
| 57 | $this->log("Links list length: ".scalar(@$links)); | ||
| 58 | |||
| 59 | if(scalar(@$links) == 0){ | ||
| 60 | $this->log("No items links found, check your XPath: ".$this->{'parser'}->{'item:link'}); | ||
| 61 | |||
| 62 | $this->stop(); | ||
| 63 | return; | ||
| 64 | } | ||
| 65 | |||
| 66 | # filtering if required | ||
| 67 | if( $this->{'filter'} ){ | ||
| 68 | $this->log("Filtering links"); | ||
| 69 | $links = $this->{'filter'}->($links, $this, $links_text); | ||
| 70 | $this->log("Filtered links list length: ".scalar(@$links)); | ||
| 71 | |||
| 72 | return if($this->{'_stop_flag'}); | ||
| 73 | } | ||
| 74 | |||
| 75 | # get and parse individual items | ||
| 76 | my $i = 0; | ||
| 77 | my $llength = scalar(@$links); | ||
| 78 | foreach(@$links){ | ||
| 79 | |||
| 80 | $i++; | ||
| 81 | |||
| 82 | # some log | ||
| 83 | my $ll = length($llength); | ||
| 84 | |||
| 85 | $this->log("\tGetting item (".sprintf("%0${ll}d",$i)."/$llength): $_ "); | ||
| 86 | |||
| 87 | my $item = $this->{'parser'}->parse($this->_get($_)); | ||
| 88 | $item->{'_url'} = $_->as_string; | ||
| 89 | |||
| 90 | $this->{'processor'}->($item,$this); | ||
| 91 | |||
| 92 | return if($this->{'_stop_flag'}); | ||
| 93 | } | ||
| 94 | |||
| 95 | if($parse_result->{'next_page'}){ | ||
| 96 | |||
| 97 | my $pnurl; | ||
| 98 | |||
| 99 | if($this->{'next_page_link_post_process'}){ | ||
| 100 | $pnurl = $this->{'next_page_link_post_process'}->($parse_result->{'next_page'}, $this, $current_url); | ||
| 101 | } | ||
| 102 | |||
| 103 | if($pnurl){ | ||
| 104 | $current_url = url($pnurl)->abs; | ||
| 105 | } else { | ||
| 106 | $current_url = url($parse_result->{'next_page'}, $this->{'url'})->abs; | ||
| 107 | } | ||
| 108 | |||
| 109 | return if($this->{'_stop_flag'}); | ||
| 110 | |||
| 111 | } else { | ||
| 112 | $this->stop("No next page link found"); | ||
| 113 | return; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | |||
| 118 | } | ||
| 119 | |||
| 120 | sub getCurrentDOM{ | ||
| 121 | my $this = shift; | ||
| 122 | return $this->{'parser'}->{'current_dom'}; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub log{ | ||
| 126 | my $this = shift; | ||
| 127 | my $message = shift; | ||
| 128 | |||
| 129 | if($this->{'logger'}){ | ||
| 130 | $this->{'logger'}->($message,$this); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | sub stop{ | ||
| 135 | my $this = shift; | ||
| 136 | my $reason = shift; | ||
| 137 | |||
| 138 | $this->{'_stop_flag'} = 1; | ||
| 139 | $this->log("\tWalker stopped."); | ||
| 140 | if($reason){ | ||
| 141 | $this->log("\tReason: $reason."); | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | sub _get{ | ||
| 146 | my $this = shift; | ||
| 147 | my $url = shift; | ||
| 148 | my $post_data = shift; | ||
| 149 | |||
| 150 | my $req; | ||
| 151 | |||
| 152 | if( $post_data ){ | ||
| 153 | $req = new HTTP::Request('POST',$url); | ||
| 154 | $req->content_type('application/x-www-form-urlencoded'); | ||
| 155 | $req->content( $post_data ); | ||
| 156 | |||
| 157 | } else { | ||
| 158 | $req = new HTTP::Request('GET',$url); | ||
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | my $res = $this->{'ua'}->request($req)->decoded_content(); | ||
| 163 | $res = $this->{'raw_preprocessor'}->($res); | ||
| 164 | |||
| 165 | return $res; | ||
| 166 | |||
| 167 | } | ||
| 168 | |||
| 169 | |||
| 170 | 1; | ||
| 171 | |||
| 172 | |||
| 173 | __END__ |