Sunday, February 8, 2009

How to post in both Twitter and Identica at once - Part II

twit.pl: version 0.0.1
In this post you will find the first version of twit.pl.
I have relied a lot on Dave's code with the following changes:
- I removed the IO::Interactive module (I did not see any difference whether I used it or not, so I removed the dependency)
- I used the Getopt module to parse the command line
- I added Identica support

Here is version 0.0.1 of twit.pl.
Use it this way:
perl twit.pl -s "This is my message and I'll stick to it!"

#!/usr/bin/perl
use 5.10.0; #To be able to use "say" function
use strict;
use warnings;
use Getopt::Long; #To parse the command line
use Net::Twitter; #API to twitter.com and identi.ca

my $VERSION = "v0.0.1";
my $TwitUser = 'twitterlogin';
my $TwitPass = 'twitterpasswd';
my $IdenticaUser = 'identicalogin';
my $IdenticaPass = 'identicapasswd';
my $TwitterUse = 1; # 1-> send to twitter, 0 -> do not send
my $IdenticaUse = 1; # 1-> send to identica, 0 -> do not send
my $Status;

#Parse command line arguments
GetOptions ("status=s" => \$Status);
say "Sending: $Status";
#There's a hard limit on the size of twits
if (length $Status > 140) {
say "Too long";
exit;
}
if ($TwitterUse) {
my $twitter = Net::Twitter->new(username => $TwitUser, password => $TwitPass);
if ($twitter->update($Status)) {
say "twitter: OK";
}
else {
say "twitter: FAIL";
}
}
if ($IdenticaUse) {
my $identica = Net::Twitter->new(identica => 1, username => $IdenticaUser, password => $IdenticaPass);
if ($identica->update($Status)) {
say "Identica: OK";
}
else {
say "Identica: FAIL";
}
}

__END__
History:
v0.0.1 (2009/02/08): First version of twit.pl. Use as:
>perl twit.pl -s "I have just finished coding the first version of twit.pl"
New Perl concepts:
- On line 2, use 5.10.0 means that your program explicitly requires a minimal Perl version number (5.10.0 in this case). This is so we can use the "say" function.
say "hello"; is equivalent to print "hello\n";
- Line 6 loads the Net::Twitter module.
- Line 21: length command returns the length of the following scalar.
- Line 23: the exit function terminates the application.
- Line 26 creates an instance of the Net::Twitter class. You can then manipulate the object with the -> operator.
- Line 35 does the same thing but for identi.ca. You just need to set the configuration item identica to true (non zero).
- Lines 27 and 36 send the message to the proper websites. If there is an error, undef is returned. The error return is tested inside the if() statement

How to improve on twit.pl
This script does the job but there are many ways to improve on it. Here's my to-do list:
  • Use template
  • Read Login and password from file (so I don't accidentally divulge my own login, har!)
  • Factorize code (use subroutine to suppress code duplication)
  • Set TwitterUse and IdenticaUse from command line option
  • More error traces: check for no argument, traces when sending msg fails
  • Simple GUI interface (1 text box + 1 check box for each Twitter and Identi.ca + 1 "Send" button)
French expression of the day:
"C'est le premier pas qui compte": The first step is always the hardest (litt: The first step is the one that counts).

Next posts:
  • Improving on twit.pl: fonctions and reading from file
  • How to install and use Google Analytics on your Blogger blog
  • Improving on twit.pl: using more of the Net::Twitter API
  • Improving on twit.pl: Graphical User interface
  • Perl help resources
  • POD

1 comment :

  1. Now you can also support posting to Wordpress, just change the values for apiurl, apirealm and apihost. http://en.blog.wordpress.com/2009/12/12/twitter-api/

    ReplyDelete