Created
June 25, 2009 14:07
-
-
Save sharifulin/135885 to your computer and use it in GitHub Desktop.
Check avatar and get from twitter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use utf8; | |
use lib '../../lib'; | |
use common; | |
use POE qw(Component::Client::HTTP); | |
use HTTP::Request::Common qw(GET); | |
use Data::Dumper; | |
POE::Component::Client::HTTP->spawn( Alias => 'HTTP_CLIENT' ); | |
POE::Session->create( | |
inline_states => { | |
_start => sub { | |
$_[KERNEL]->call($_[SESSION] => 'request', $_->{'image'}, $_) for @{$DB->select('select * from faces')}; | |
}, | |
request => sub { | |
warn $_[ARG0]; | |
$_[KERNEL]->post( | |
'HTTP_CLIENT' => 'request' => 'response', | |
GET($_[ARG0]), | |
$_[ARG1] | |
); | |
}, | |
response => sub { | |
my $r = $_[ARG1]->[0]; | |
my $tag = $_[ARG0]->[1]; | |
if ($tag->{'_'} eq 'parse') { | |
my($image) = map { m{<img.*?src="([^"]+)"} } $r->content =~ m{<h2 class="thumb clearfix">(.*?)</h2>}s; | |
warn "$tag->{'user'} update $image\n"; | |
$DB->query('update faces set image=?, updated=? where id=?', $image, @$tag{'updated', 'id'}); | |
} | |
else { | |
unless ($r->is_success) { | |
warn "$tag->{'user'} ".$r->status_line."\n"; | |
$tag->{'_'} = 'parse'; | |
$_[KERNEL]->call($_[SESSION] => 'request', "http://twitter.com/$tag->{'user'}", $tag); | |
} | |
else { | |
warn "$tag->{'user'} OK\n"; | |
} | |
} | |
}, | |
} | |
); | |
POE::Kernel->run; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment