#!/usr/bin/perl use MIME::Base64; use Encode qw(encode); use DBI; use DBI qw(:sql_types); require HTTP::Request; require HTTP::Response; use HTTP::Async; #HTTP::Async timout is broken by default. Check the cpan page for how + to fix. It's in the bugs page. my $async = HTTP::Async->new(timeout=>5,slots=>100); use List::MoreUtils; use strict; open PIDFILE, ">/usr/path/pidfile" or die $!; print PIDFILE $$; close PIDFILE; #definition of variables my $db="databasename"; my $host="localhost"; my $user="username"; my $password="password"; my $verbose_logging = -1; while (1 == 1) { sleep rand 3; my @sites; my @db_row; my @response_array; #connect to MySQL database my $dbh = DBI->connect ("DBI:mysql:database=$db:host=$host", $user, $password) or die "Can't connect to database: $DBI::err +str\n"; #prepare the query my $sth = $dbh->prepare( "SELECT `page_url`, `url2`, `idx`, `date_time +`, `userid` FROM `queue` LIMIT 100"); #execute the query $sth->execute( ); ## Retrieve the results of a row of data and print while ( my @row = $sth->fetchrow_array( ) ) { #set counter push(@sites, $row[0]); push @db_row, ([$row[0], $row[1], $row[2], $row[3], $row[4]]); } foreach my $site(@sites) { $async->add( HTTP::Request->new( GET => $site ) ); } # Do some processing with $response #while ( my ( $response, $id ) = $async->wait_for_next_response) # { while ( $async->not_empty ) { if ( my ($response, $id) = $async->wait_for_next_response ) { # deal with $response print $async->info; my $content = $response->decoded_content; #Needs to be reencoded #async assigns ID's for all the requests, but they begin with 1, wh +ereas, rows begin with 0 with #sql requests. We'll need to decrement them to make sure everythin +g matches up when we #pick the elements out of the nested array my $result_row = ($id - 1); my $urlcount = 0; #Counts the number occurances of the input in the html content $urlcount++ while ($content =~ m/$db_row[$result_row][1]/gi); #encodes the content so that we can easily store to the DB for lat +er referencing. $content = encode_base64(encode("UTF-8", $content)); my $insert = "INSERT INTO cheker (`active`,`page_url`,`url2`,`date +_time`,`userid`,`html_source`) VALUES ($urlcount,'$db_row[$result_row +][0]','$db_row[$result_row][1]','$db_row[$result_row][3]','$db_row[$r +esult_row][4]','$content');"; my $QueueExecute = $dbh->prepare($insert); $QueueExecute->execute( ); warn "Problem in retrieving results", $sth->errstr( ), "\n" if $QueueExecute->err( ); if ($verbose_logging >= 0) { print "Inserted record into checker\n"; } my $delete = "DELETE FROM queue WHERE `idx` = '$db_row[$result_row +][2]';"; my $QueueExecute = $dbh->prepare($delete); $QueueExecute->execute( ); if ($verbose_logging >= 0) { print "Deleted row from queue\n"; } warn "Problem in retrieving results", $sth->errstr( ), "\n" if $QueueExecute->err( ); warn "Problem in retrieving results", $sth->errstr( ), "\n" if $sth->err( ); } else { next; } } #$result_row++; # my $result_row = 0; # my $response_row = 1; }
In reply to Re^2: HTTP:Async weirdness
by schnibitz
in thread HTTP:Async weirdness
by schnibitz
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |