Archive

Archive for May, 2009

Use PERL script to automatically follow your followers on Twitter

May 26th, 2009 admin 1 comment

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=<DAT>;

close(DAT);

 

my $sth;

my $nIDs = 0;

foreach (@lines) {

my $id = $_;

$id =~ m/<id>([\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. If you like, please post a comment. 
  • Share/Save/Bookmark
Categories: Hands-On Tags:

Post best deals to twitter automatically

May 25th, 2009 admin 1 comment

We at U’s Bargain Network collect and compile deals across the Internet every  day. Best deals will be selected based on 1) Must be current and 2) Lowest price by comparing with price history. Since we have Twitter account (@usbargains), we post these best deals to Twitter to help general consumers like you. To do that manually it is tedious and error-prone. We have developed a nice PERL script to help us do this. It can be modified and adopted to other purpose. We’d like to share this with you here. 

==============

#!/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.
  • Share/Save/Bookmark
Categories: Help on usbargains.net Tags:

Facebook account for U’s Bargain Network

May 24th, 2009 admin No comments

Maya 24, a Facebook account was setup for our business. Visit it at http://www.facebook.com/pages/Us-Bargain-Network/86524726879.  We will use this account to connect to general consumers align with our Twitter account (@usbargains).

  • Share/Save/Bookmark
Categories: Help on usbargains.net Tags:

Enhanced search function

May 22nd, 2009 admin No comments

The SEARCH function of www.usbargains.net was designed recently. By combining fast web server and AJAX technology, the speed of search function has been dramatically improved. It totally get rid of the overhead time to generate the whole page by using AJAX technique. In addition, full text search has been enabled. You can search multiple keywords and search results will be listed in order of relevance.

  • Share/Save/Bookmark
Categories: Help on usbargains.net Tags:

ING Direct Electronic Orange Checking

May 6th, 2009 admin 1 comment

This checking service is very good. If you combine this checking with an ING Direct saving account it will dramtically simplify your life to manage your money. You can have your money in the ING saving account because it has higher interest rate. Transfer money to your checking instantly whenever you need your money to pay bills. This is excellent solution for personal financial management. WE and several of our friends use this kind of combination for years. We all enjoy the simplicity and convenience to use ING Direct. Open your first ING high interest checking now.

  • Share/Save/Bookmark
Categories: Hands-On Tags:

Find deals of your favorite brands

May 5th, 2009 admin 3 comments

In the right hand side, there is a cloud tag box for brands. All well known brands are included in the cloud tags. Use your mouse move over each brand name, you can find out how many deals for that particular brand. Click a brand name, a new page will open and only include deals for that brand. This is shortcut for brand shopper.

  • Share/Save/Bookmark
Categories: Help on usbargains.net Tags:

Use price history to help you make decision

May 4th, 2009 admin No comments

There is price history graph, which is updated every day, on every item page. This graph reflects the recent price history of the product you are viewing. There are multiple situations. Price of a product can go up and down frequently. In that case, you can monitor the product closely and buy it at lowest price. Or it can go down quickly. If that is the case, you may wait a few days to see what will happen. Another situation is price go up recently and stay high. You know its price was low at one time point. You can wait a while until its proce drop the lowest or even lower.

  • Share/Save/Bookmark
Categories: Help on usbargains.net Tags:

Use cloud tags on the right hand side

May 4th, 2009 admin No comments

Cloud tags can be found on the right hand side of every page on the U’s Bargain Netwoork website. You can click tags relevant to what you are looking for.  In this way, you can quickly found excellent deals.

  • Share/Save/Bookmark
Categories: Help on usbargains.net Tags: