A PERL script to get Twitter user information

If you are a Twitter user and do business on Twitter, you probably think about how I can target to a group of Twitter users, such as living in certain region or having similar interests. Can we screen Twitter users and target to a given population of Twitter users? Answer is absolutely positive: yes, we can. One simple solution is to write a PERL script to download Twitter user information and store them into database first. Then we can conduct different sort of queries to achieve our goal. The following PERL script can download Twitter user information, extract user information and then store in MySQL database. Here is the source code.

#!/usr/bin/perl
require 5.6.0;
use strict;
use warnings;
use DBI;
use Data::Dumper;
use XML::Simple;

my $dbh;
my $sql;
my $sth;
my $dbname = "twitter_world";
my $tablename = "users";
my $xmlname = "tid.xml";

getConnected();

my $i = 100;
while (1==1)
{
	$i++;
	process($i);
}

disConnected();

# END OF MAIN PROGRAM

sub process {

	my $tid = $_[0];
	system("curl  http://twitter.com/users/show.xml?user_id=$tid -o tid.xml");

	# create an XML object
	my $xml = new XML::Simple;

	# read the RSS feed file
	my $data = $xml->XMLin($xmlname);

	#print Dumper(\$data); 

	if ($data->{error}) { return; }

	my $location        = checkVar($data->{location});
	my $time_zone       = checkVar($data->{time_zone});
	if (($location eq "") && ($time_zone eq "")) { return; }

	my $id              = checkVar($data->{id});
	my $name            = checkVar($data->{name});
	my $screen_name     = checkVar($data->{screen_name});
	my $description     = checkVar($data->{description});
	my $url             = checkVar($data->{url});
	my $followers_count = checkVar($data->{followers_count});
	#my $friends_count   = checkVar($data->{friends_count});
	#my $created_at      = checkVar($data->{created_at});
	#my $utc_offset      = checkVar($data->{utc_offset});

	my $sql = "insert into users (id, name, screen_name, location, description, url, time_zone) ".
	          "values ($id, $dbh->quote($name), $dbh->quote($screen_name), $dbh->quote($location), $dbh->quote($description), $dbh->quote($url), $dbh->quote($time_zone));";
	#print "$sql\n";
	executesql($sql);
}

sub checkVar
{
	my $var = $_[0];
	if (ref($var) eq 'HASH')
	{
		$var = "";
	}
	return $var;
}

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

#############################################
#
# 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="host address";
	my $connectionInfo = "DBI:mysql:$dbname;$host";
	my $databaseUser = "mysql database username";
	my $databasePw = "mysql 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();
}
  • Share/Bookmark

One Response to “A PERL script to get Twitter user information”

  1. A PERL script to get Twitter user information | 太阳帆数据 Sunfine … « New says:

    [...] More: A PERL script to get Twitter user information | 太阳帆数据 Sunfine … [...]

Leave a Response

You must be logged in to post a comment.