#! /usr/bin/perl
use strict;
use XML::Simple;
use LWP::UserAgent;
use DBI;
# CONFIGURE THE BOT
my $twitter_username = "";
my $twitter_password = "";
my $database_host = "";
my $database_username = "";
my $database_password = "";
my $database_name = "";
my $search_word = "%23gorillapenis";
# CONFIGURATION ENDS HERE
# prepare MySQL
my $DSN = "dbi:mysql:database=$database_name;host=$database_host;user=$database_username;password=$database_password";
# search Twitter
my @records = searchTwitter();
die "No new tweets to post.\n";
}
# post new tweets
my $i = 0;
foreach my $item (@records) {
my $statusid = $item->{statusid};
my $text = $item->{text};
my $username = $item->{username};
my $handle = $item->{handle};
$text =~ s/'/''/g;
my $tweet = "RT \@".$handle.": ".$text;
$tweet =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
my $ua = new LWP::UserAgent;
my $req = new HTTP::Request POST => 'http://twitter.com/statuses/update.xml';
$req->authorization_basic($twitter_username, $twitter_password);
$req->content("status=$tweet");
my $res = $ua->request($req);
if ($res->is_success) {
}
else {
print "ERROR POSTING TO TWITTER: " . $res->status_line, "\n";
}
}
# update database
if($write[0] != '0') {
my $dbh = DBI->connect($DSN);
my $checkid = "insert into tabela values ('', '".$write[0]."')";
my $sth_check = $dbh->prepare($checkid);
my $rv = $sth_check->execute;
}
sub searchTwitter() {
my $dbh = DBI->connect($DSN);
my $checkid = "select twitter from tabela order by id desc limit 1";
my $sth_check = $dbh->prepare($checkid);
my $rv = $sth_check->execute;
my $tweetid = $sth_check->fetchrow();
my @res = ();
my $ua = new LWP::UserAgent;
my $req = new HTTP::Request GET => "http://search.twitter.com/search.atom?q=$search_word&rpp=100&since_id=$tweetid";
my $content = $ua->request($req)->content() or die "Error searching Twitter";
my $xml = new XML::Simple (KeyAttr=>[]);
my $data = $xml->XMLin($content);
foreach my $item (@{$data->{entry}}) {
my $rec = {};
if ($item->{title}) {
$item->{author}{name} =~ /\s*(\S+)\s+\(/;
my $handle = $1;
my $id = (split ':',$item->{id
})[2
];
print "Search id: $id username: $handle\n";
$rec->{'statusid'} = $id;
$rec->{'text'} = $item->{title};
$rec->{'handle'} = $handle;
if($handle ne $twitter_username) { push (@res, $rec); }
}
}
}