#! /usr/bin/perl 

use strict;
use XML::Simple;
use LWP::UserAgent;
use DBI;

# constants
my $DSN = "dbi:mysql:database=YourDbName;host=YourDbHost;user=YourDbUser;password=YourDbPassword";

my $READFROM_USER = "YourTwitterBotName";
my $READFROM_PASS = "YourTwitterBotPassword";

my $WRITETO_USER = "YourTwitterBotName";
my $WRITETO_PASS = "YourTwitterBotPassword";


# fetch from twitter
my @records = fetchTweets();
if (scalar(@records) <= 0) { 
	# fetch from summize
	print "UNABLE TO FETCH FROM TWITTER, TRYING SUMMIZE!!!\n";
	@records = fetchFromSummize();
}


my $i = 0;
foreach my $item (@records) {
	my $statusid = $item->{statusid};
	my $text =     $item->{text};
	my $userid =   $item->{userid};
	my $username = $item->{username};
	my $handle   = $item->{handle};
	
	$text =~ s/\@YourTwitterBotName:* //ig;
	$text =~ s/'/''/g;
	
	my $dbh = DBI->connect($DSN);
	
	my $checkid = "select count(*) from lyrics where status_id = '$statusid'";
	my $sth_check = $dbh->prepare($checkid);
	my $rv = $sth_check->execute;
	
	my $lyric = $sth_check->fetchrow_array();
	
	if ($lyric < 1) {
		my $insert = "insert into lyrics(status_id,text,user_id,user_name,handle,posted)
			values('$statusid','$text','$userid','$username','$handle','0')";
		
		my $sth_insert = $dbh->prepare($insert);
		$rv = $sth_insert->execute;
		
		$sth_insert->finish();
	} 
	
	$sth_check->finish();
	$dbh->disconnect();
}


my $dbh = DBI->connect($DSN);

my $postq = "select status_id,handle,text from lyrics where posted = 0 order by lyric_id";
my $sth_postq = $dbh->prepare($postq);
my $rv = $sth_postq->execute;

my %unposted = ();
my $postcounter = "1";
while (my ($statusid,$handle,$text) = $sth_postq->fetchrow_array()) {
	
	$unposted{ $postcounter }{'id'} = $statusid;
	$unposted{ $postcounter }{'handle'} = $handle;
	$unposted{ $postcounter }{'text'} = $text;
	
	$postcounter++;
}

$sth_postq->finish();
$dbh->disconnect();

$postcounter = "1";

foreach my $item (keys (%unposted)) {
	if ($postcounter <15) {
		
		my $statusid = $unposted{ $item }{'id'};
		my $handle = $unposted{ $item }{'handle'};
		my $text = $unposted{ $item }{'text'};
		
		my $tweet = "Via \@".$handle.": ".$text;
		$tweet =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
		
		$dbh = DBI->connect($DSN);
	
		my $update = "update lyrics set posted = '1' where status_id = '$statusid'";
		my $sth_update = $dbh->prepare($update);
		$rv = $sth_update->execute;
		
		$sth_update->finish();
		$dbh->disconnect();
		
		my $ua = new LWP::UserAgent;
		my $req = new HTTP::Request POST => 'http://twitter.com/statuses/update.xml';
		$req->authorization_basic($WRITETO_USER, $WRITETO_PASS);
		$req->content("status=$tweet");

		my $res = $ua->request($req);
		
		if ($res->is_success) {
		  print $res->content;
	    }
		else {
			print "ERROR POSTING TO TWITTER: " . $res->status_line, "\n";
		}
		
		$postcounter++;
		sleep 59;
	} else {
		exit;
	}
}
exit;

	



sub fetchTweets() { 
	my @res = ();
	
	my $ua = new LWP::UserAgent;
	my $req = new HTTP::Request GET => 'http://twitter.com/statuses/replies.xml';
	$req->authorization_basic($READFROM_USER, $READFROM_PASS);
	my $content = $ua->request($req)->content();

	my $xml = new XML::Simple (KeyAttr=>[]);
	
	print "TWITTER content: $content";
	my $data = $xml->XMLin($content);
	
	foreach my $item (@{$data->{status}}) {
		my $rec = {};
		if ($item->{text}) { 
			$rec->{'statusid'} = $item->{id};
			$rec->{'text'} =     $item->{text};
			$rec->{'userid'} =   $item->{user}{id};
			$rec->{'username'} = $item->{user}{name};
			$rec->{'handle'}   = $item->{user}{screen_name};
			
			print "TWITTER id: " . $rec->{'statusid'} . "\n";

			push (@res, $rec);
		}
	}
	
	return @res;
}

sub fetchFromSummize() { 
	my @res = ();
	
	my $ua = new LWP::UserAgent;
	my $req = new HTTP::Request GET => "http://summize.com/search.atom?q=to%3A$READFROM_USER";
	my $content = $ua->request($req)->content();

	my $xml = new XML::Simple (KeyAttr=>[]);
	
	print "summize content: $content";
	my $data = $xml->XMLin($content);
	
	foreach my $item (@{$data->{entry}}) {
		my $rec = {};
		if ($item->{title}) { 
			my ($handle,$username) = ($item->{author}{name} =~ /\s*(\S+)\s+\(([^()]*)\)/);
			my $id = (split ':',$item->{id})[2];
			
			print "Summize id: $id username: $username handle: $handle\n";
			$rec->{'statusid'} = $id;
			$rec->{'text'} =     $item->{title};
			$rec->{'userid'} =   '-1'; # don't have this from summize...
			$rec->{'username'} = $username;
			$rec->{'handle'}   = $handle;
			push (@res, $rec);
		}
	}
	
	return @res;
}

<