Use PERL script to automatically follow your followers on Twitter

If you have a Twitter account, you will know there are friends follow you and you are not following them. To be nice to your fans, you’d better follow them back. If you have a large number fans added everyday, it will be hard to catch up. Now it is time to automate the process. The following PERL function will do the trick for you.

#!/usr/bin/perl
require 5.6.0;
use strict;
use warnings;
use DBI;

# ==== get command line arguments
if ($#ARGV != 1) {
    print “usage: twitter_friends2 username password\n”;
    exit;
}

my $username = $ARGV[0];
my $password = $ARGV[1];

my $dbh;
my $sql;

getConnected();

my $friends = “friends”;
my $followers = “followers”;
my $target_followers = “target_followers”;

# ==== download friends, followers and target’s followers list
system(”curl -u $username:$password http://twitter.com/friends/ids/$username.xml >$friends.xml”);
system(”curl -u $username:$password http://twitter.com/followers/ids/$username.xml >$followers.xml”);

# ==== clean data tables
$sql =”DELETE FROM friends”;
executesql($sql);
$sql =”DELETE FROM followers”;
executesql($sql);
$sql =”DELETE FROM nonfollowers”;
executesql($sql);

# ==== load data to tables
importIDs($friends);
importIDs($followers);

# ==== find non following
$sql = “INSERT INTO nonfollowers (id_nonfollowers) “.
 ”SELECT id_followers FROM followers WHERE id_followers NOT IN “.
 ”(SELECT id_friends FROM friends)”;
executesql($sql);

# follow the friends you are not following now
add();

disConnected();

# END OF MAIN PROGRAM

#############################################
#
# add friends who do not follow you in the past
#
sub add {
my $sql = “SELECT * FROM nonfollowers”;
my $sth = $dbh->prepare($sql);
$sth->execute();
my $id;
$sth->bind_columns(\$id);
while ($sth->fetch()) {
   system(”curl -X POST -u $username:$password http://twitter.com/friendships/create/$id.xml >/dev/null”);
   my $sql1 = “DELETE FROM nonfollowers where id_nonfollowers=$id”;
   my $sth1 = $dbh->prepare($sql1);
   $sth1->execute();
   $sth1->finish();
  }
$sth->finish();
}

#############################################
#
# remove friends who do not follow you
#
sub remove2 {
my $sql = “SELECT * FROM friends”;
my $sth = $dbh->prepare($sql);
$sth->execute();
my $id;
$sth->bind_columns(\$id);
while ($sth->fetch()) {
   system(”curl -X POST -u $username:$password http://twitter.com/friendships/destroy/$id.xml >/dev/null”);
   my $sql1 = “DELETE FROM friends where id_friends=$id”;
   my $sth1 = $dbh->prepare($sql1);
   $sth1->execute();
   $sth1->finish();
   }
$sth->finish();
}

#############################################
#
# execute a SQL statement
#
sub executesql {
my $sth = $dbh->prepare($_[0]);
$sth->execute();
$sth->finish();
}

#############################################
#
# import data to table
#
sub importIDs {
my $tablename = $_[0];
open(DAT, “$tablename.xml”) || die(”Could not open file!”);
my @lines=;
close(DAT);

my $sth;
my $nIDs = 0;
foreach (@lines) {
my $id = $_;
$id =~ m/([\d]*)<\/id>/;
if (defined($1)) {
   my $sql = “INSERT INTO $tablename (id_$tablename) values ($1);”;
   $sth = $dbh->prepare($sql);
   $sth->execute();
   $nIDs ++;
   }
}
$sth->finish();
unlink “$tablename.xml”;
return $nIDs;
}

#############################################
#
# 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:db_twitter;$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();
}

To use this PERL script, you have to create a database in MySQL server by using the following SQL commands.

/*
DATABASE db_twitter
*/
/*
followers
*/
CREATE TABLE `followers` (
  `id_followers` int(15) unsigned NOT NULL,
  PRIMARY KEY  (`id_followers`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
/*
friends
*/
CREATE TABLE `friends` (
  `id_friends` int(15) unsigned NOT NULL,
  PRIMARY KEY  (`id_friends`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
/*
nonfollowers
*/
CREATE TABLE `nonfollowers` (
  `id_nonfollowers` int(15) unsigned NOT NULL,
  PRIMARY KEY  (`id_nonfollowers`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Once you ccreate the database (db_twiiter), you can run the PERL script to automate follow your friends.

Reference

  • Share/Bookmark

2 Responses to “Use PERL script to automatically follow your followers on Twitter”

  1. [...] an account (usbargains) for our business on Twitter. Original GIFT application was a set of PERL scripts running on one of our servers. All proven successful Twitter strategies and tips were incorporated [...]

  2. [...] Use PERL script to automatically follow your followers on Twitter [...]

Leave a Response

You must be logged in to post a comment.