Automatically send updates to Twitter
Here is a PERL script I developed to automate Twitter update activity. The fundamental thing is you have a database and all updates are stored there. You are sick of sending all Twitter updates manually. As long as you meet these two criteria you can keep reading. Otherwise, stop here. The PERL source code is shared here. You can modify and adopt it for your purpose.
#!/usr/bin/perl
# include libraries
#——————————-
require 5.6.0;
use warnings;
use strict;
use DBI;
use Time::Local;
use Data::Dumper;
if ($#ARGV != 3) {
print “usage: twitter_friends username password\n”;
exit;
}
my $username = $ARGV[0];
my $password = $ARGV[1];
my $newdeal = $ARGV[2];
my $direct = $ARGV[3];
my $dbname = “your mysql database name”;
my $dbh;
my $sql;
my $sth;
my $todayStr;
getTodayStr();
getConnected();
if ($newdeal != 1)
{
$sql = “SELECT price, name, id_items FROM deals “.
“WHERE (expiration_date >= ‘$todayStr’) AND (imagefile <> ”) AND featured = 1;”;
}
else
{
$sql = “SELECT price, name, id_items FROM deals “.
“WHERE (expiration_date >= ‘$todayStr’) AND (imagefile <> ”) “.
“ORDER BY id_items DESC “.
“LIMIT 0,18;”;
}
updateStatus();
disConnected();
#############################################
#
# send status update to twitter
#
sub updateStatus {
$sth = $dbh->prepare($sql);
$sth->execute();
my ($price, $name_items, $id_items);
$sth->bind_columns(\$price, \$name_items, \$id_items);
while ($sth->fetch())
{
my $price_str = “”;
if ($price > 0.0)
{
$price_str = “[\$".$price."]“;
}
my $short_name = substr($name_items, 0, 70);
my $url = “http://usbargains.net/deals/$id_items.html”;
if ($direct == 1)
{
$url = “http://usbargains.net/items/usb_$id_items.html”;
}
my $status=”Deal-”.$price_str.” “.$short_name.” “.$url;
#print $status.”\n”;
$status =~ s/([^A-Za-z0-9])/sprintf(”%%%02X”, ord($1))/seg;
my $cmd = “curl -X POST -u $username:$password \”http://twitter.com/statuses/update.xml?status=”.$status.”\” >/dev/null”;
system($cmd);
}
$sth->finish();
}
#############################################
#
# connect to database and generate a handle
#
sub getConnected {
# return the database handle object to the caller
# Set the parameter values for the connection
#——————————-
my $host=”localhost”;
my $connectionInfo = “DBI:mysql:$dbname;$host”;
my $databaseUser = “your database username”;
my $databasePw = “your database password”;
# Connect to the database
# Note this connection can be used to
# execute more than one statement
# on any number of tables in the database
#——————————-
$dbh = DBI->connect($connectionInfo, $databaseUser,
$databasePw) || die “Connect failed: $DBI::errstr\n”;
}
######################################
#
# disconnect the database handle
#
sub disConnected {
$dbh->disconnect();
}
######################################
#
# generate today’s date string
#
sub getTodayStr {
# get local time
my ($sec,$min,$hour,$iDay,$iMonth,$iYear,$wday,$yday,$isdst) = localtime time;
$iYear = 1900 + $iYear;
$iMonth++;
$todayStr = “$iYear-$iMonth-$iDay”;
}
==============
To use this PERL script, you have to install CURL to your system. We assume that your have MySQL database server somewhere you can access. Post your comments to discuss this script. Enjoying.
Reference
- Use PERL script to automatically follow your followers on Twitter
- Use libcurl.net at C# project – a complete solution

[...] Automatically send updates to Twitter [...]
i just love Twittering compared to blogging. i was a blog addict and now i am a Twitter addict.