package II::Get; use LWP::UserAgent; use HTTP::Request; use II::DB; use II::Enc; use Data::Dumper; use utf8; sub new { my $class = shift; my $ua = LWP::UserAgent->new(); $ua->agent("Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0)"); my $db = II::DB->new(); my $self = { _config => shift, _ua => $ua, _db => $db, }; bless $self, $class; return $self; } sub get_echo { my ($self) = @_; my $config = $self->{_config}; my $echoareas = $config->{echoareas}; my $host = $config->{host}; my $ua = $self->{_ua}; my $db = $self->{_db}; my $echo_url = 'u/e/'; my $msg_url = 'u/m/'; my $msgs; my @messages; my $base64; my @messages_hash; foreach my $echo (@$echoareas) { # Get echo message hashes my $req_echo = HTTP::Request->new( GET => "$host$echo_url$echo" ); my $res_echo = $ua->request($req_echo); print "$host$echo_url$echo\n"; my @new; $db->begin(); if ( $res_echo->is_success ) { print "Request $echo is success\n"; my @mes = split /\n/, $res_echo->content(); while (<@mes>) { if ( $_ =~ /.{20}/ ) { if ( $db->check_hash( $_, $echo ) == 0 ) { my $echo_hash = { echo => $echo, hash => $_, }; my %e_write = ( echo => $echo, hash => $_, ); # Write new echo message $db->write_echo(%e_write); $msgs .= $_ . "\n"; push( @new, $echo_hash ); } } } } else { print $res_echo->status_line, "\n"; } $db->commit(); # Get messages and populate hash my $count = 0; while ( $count < @new ) { my $new_messages_url = "$host$msg_url" . $new[$count]->{hash}; print "URI: $new_messages_url\n"; my $req_msg = HTTP::Request->new( GET => $new_messages_url ); my $res_msg = $ua->request($req_msg); if ( $res_msg->is_success() ) { my ( $hash, $m ) = split(':', $res_msg->content()); push( @messages_hash, { hash => $hash, base64 => $m } ); } else { print $res_msg->status_line, "\n"; } $count++; } print "Sleep for 5 seconds\n"; sleep(5); } my $new_messages = "