| 76 | | |
| 77 | | sub poe_attach { |
| 78 | | my ($kernel, $heap, $args) = @_[KERNEL, HEAP, ARG0]; |
| 79 | | |
| 80 | | # $kernel->delay( attach => 1 ); |
| 81 | | } |
| 82 | | |
| 83 | | sub poe_update { |
| 84 | | my ($self, $kernel, $heap, $message) = @_[OBJECT, KERNEL, HEAP, ARG0]; |
| 85 | | $heap->{nowa}->update_nanishiteru($message); |
| 86 | | } |
| | 132 | sub _scrape { |
| | 133 | my ($self, $param) = @_; |
| | 134 | |
| | 135 | my $url = $param->{uri}; |
| | 136 | my $stuff = $url; |
| | 137 | $stuff = $url->as_string if (blessed($url) && $url->isa('URI')); |
| | 138 | |
| | 139 | my $html = $param->{content}; |
| | 140 | $html = Encode::decode('utf-8', $param->{content}) unless utf8::is_utf8($param->{content}); |
| | 141 | |
| | 142 | my $base = ($html =~ /<base\s+href="([^"]+?)"/)[0] || $stuff; |
| | 143 | |
| | 144 | $param->{scraper}->scrape($html, URI->new($base)); |
| | 145 | } |
| | 146 | |
| | 147 | sub poe__api { |
| | 148 | my ($self, $kernel, $heap, $method, $content) = @_[OBJECT, KERNEL, HEAP, ARG0, ARG1]; |
| | 149 | |
| | 150 | my $uri = URI->new_abs($method, $heap->{args}->{NOWA_API_HOME}); |
| | 151 | my $req; |
| | 152 | if (defined($content)) { |
| | 153 | $req = HTTP::Request::Common::POST( |
| | 154 | $uri, |
| | 155 | $content, |
| | 156 | ); |
| | 157 | } else { |
| | 158 | $req = HTTP::Request::Common::GET( |
| | 159 | $uri, |
| | 160 | ); |
| | 161 | } |
| | 162 | $req->authorization_basic($heap->{args}->{nowa_id}, $heap->{args}->{api_pass}); |
| | 163 | |
| | 164 | $kernel->post($self->ua_alias => request => '_api_response', $req); |
| | 165 | } |
| | 166 | |
| | 167 | sub poe__httpget { |
| | 168 | my ($self, $kernel, $heap, $uri) = @_[OBJECT, KERNEL, HEAP, ARG0]; |
| | 169 | |
| | 170 | my $req = HTTP::Request::Common::GET( |
| | 171 | $uri, |
| | 172 | ); |
| | 173 | $kernel->post($self->ua_alias => request => '_httpget_response', $req); |
| | 174 | } |
| | 175 | |
| | 176 | sub poe__api_response { |
| | 177 | my($kernel, $heap, $session, $request_packet, $response_packet) = @_[KERNEL, HEAP, SESSION, ARG0, ARG1]; |
| | 178 | |
| | 179 | my $request = $request_packet->[0]; |
| | 180 | my $response = $response_packet->[0]; |
| | 181 | |
| | 182 | unless ($response->is_success) { |
| | 183 | $kernel->yield(notify => 'response_error', $response); |
| | 184 | return; |
| | 185 | } |
| | 186 | |
| | 187 | my $uri = URI->new($request->uri)->path; |
| | 188 | my $content = $response->content; |
| | 189 | $content = Encode::decode('utf-8', $content) unless utf8::is_utf8($content); |
| | 190 | local $JSON::Syck::ImplicitUnicode = 1; |
| | 191 | my $res = JSON::Syck::Load($content); |
| | 192 | |
| | 193 | if (ref($res) eq 'HASH' and $res->{result} eq 'fail') { |
| | 194 | return; |
| | 195 | } |
| | 196 | |
| | 197 | my %apis = %{ $heap->{args}->{apis} }; |
| | 198 | while (my ($apiname, $api) = each(%apis)) { |
| | 199 | if ($uri eq $api->{json}) { |
| | 200 | $kernel->yield(notify => $api->{notify}, $res); |
| | 201 | return; |
| | 202 | } |
| | 203 | } |
| | 204 | warn "unknown " . $uri; |
| | 205 | } |
| | 206 | |
| | 207 | sub poe__httpget_response { |
| | 208 | my($kernel, $heap, $session, $request_packet, $response_packet) = @_[KERNEL, HEAP, SESSION, ARG0, ARG1]; |
| | 209 | |
| | 210 | my $request = $request_packet->[0]; |
| | 211 | my $response = $response_packet->[0]; |
| | 212 | |
| | 213 | unless ($response->is_success) { |
| | 214 | $kernel->yield(notify => 'response_error', $response); |
| | 215 | return; |
| | 216 | } |
| | 217 | |
| | 218 | my %scrapers = %{ $heap->{args}->{scrapers} }; |
| | 219 | while (my ($scraper, $uri) = each(%scrapers)) { |
| | 220 | if ($request->uri eq $uri) { |
| | 221 | $kernel->yield("_$scraper" => { uri => $uri, content => $response->content }); |
| | 222 | return; |
| | 223 | } |
| | 224 | } |
| | 225 | warn "unknown " . $request->uri; |
| | 226 | } |
| | 227 | |
| | 228 | |
| | 229 | sub poe_channels { |
| | 230 | my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP]; |
| | 231 | |
| | 232 | $self->yield(_httpget => $heap->{args}->{NOWA_HOME}); |
| | 233 | } |
| | 234 | |
| | 235 | sub poe__scrape_channels { |
| | 236 | my ($self, $kernel, $heap, $param) = @_[OBJECT, KERNEL, HEAP, ARG0]; |
| | 237 | |
| | 238 | $param->{scraper} = scraper { |
| | 239 | process 'ul.home-chlist > li', 'channels[]' => scraper { |
| | 240 | process 'a', |
| | 241 | name => 'TEXT', |
| | 242 | link => '@href'; |
| | 243 | }; |
| | 244 | }; |
| | 245 | |
| | 246 | my $res = $self->_scrape($param); |
| | 247 | my $data; |
| | 248 | for my $chan (@{ $res->{channels} }) { |
| | 249 | my $id = '#' . ($chan->{link} =~ m!^http://nowa.jp/ch/(.*?)/!)[0]; |
| | 250 | my $name = $chan->{name}; |
| | 251 | $name =~ s/\(\d+\)$//; |
| | 252 | $data->{$id} = $name; |
| | 253 | } |
| | 254 | |
| | 255 | $kernel->yield(notify => 'channels_success', $data); |
| | 256 | } |
| | 257 | |
| 103 | | my ($self, $kernel, $heap, $target, $message) = @_[OBJECT, KERNEL, HEAP]; |
| 104 | | |
| 105 | | my $data = $heap->{nowa}->channel_recent; |
| 106 | | $kernel->yield(notify => 'channel_recent_success', $data); |
| | 267 | my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP]; |
| | 268 | |
| | 269 | $self->yield(_httpget => $heap->{args}->{scrapers}->{scrape_channel_recent}); |
| | 270 | } |
| | 271 | |
| | 272 | sub poe__scrape_channel_recent { |
| | 273 | my ($self, $kernel, $heap, $param) = @_[OBJECT, KERNEL, HEAP, ARG0]; |
| | 274 | |
| | 275 | $param->{scraper} = scraper { |
| | 276 | process 'ul#article-list > li', 'msgs[]' => scraper { |
| | 277 | process_first 'a.blue-cms', |
| | 278 | user => 'TEXT', |
| | 279 | userlink => '@href'; |
| | 280 | process 'span.body', |
| | 281 | body => 'TEXT'; |
| | 282 | process 'span.body > a', |
| | 283 | channel => 'TEXT', |
| | 284 | channellink => '@href'; |
| | 285 | process 'span.time > a', |
| | 286 | permalink => '@href'; |
| | 287 | }; |
| | 288 | }; |
| | 289 | |
| | 290 | my $res = $self->_scrape($param); |
| | 291 | my @data; |
| | 292 | for my $msg (@{ $res->{msgs} }) { |
| | 293 | next unless $msg->{permalink}; |
| | 294 | |
| | 295 | my $user = ($msg->{userlink} =~ m!^http://([^\.]+)\.nowa\.jp/!)[0]; |
| | 296 | my $body = $msg->{body}; |
| | 297 | $body =~ s/\s+#\w+$//; |
| | 298 | |
| | 299 | push(@data, +{ |
| | 300 | body => $body, |
| | 301 | user => $user, |
| | 302 | permalink => $msg->{permalink}->as_string, |
| | 303 | channel => $msg->{channel}, |
| | 304 | }); |
| | 305 | } |
| | 306 | |
| | 307 | $kernel->yield(notify => 'channel_recent_success', \@data); |