Skip to content

Instantly share code, notes, and snippets.

@sharifulin
Created June 25, 2009 14:07
Show Gist options
  • Save sharifulin/135885 to your computer and use it in GitHub Desktop.
Save sharifulin/135885 to your computer and use it in GitHub Desktop.
Check avatar and get from twitter
#!/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