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!"
  1. #!/usr/bin/perl  
  2. use 5.10.0;         #To be able to use "say" function  
  3. use strict;  
  4. use warnings;  
  5. use Getopt::Long;   #To parse the command line  
  6. use Net::Twitter;   #API to twitter.com and identi.ca  
  7.   
  8. my $VERSION  = "v0.0.1";  
  9. my $TwitUser = 'twitterlogin';  
  10. my $TwitPass = 'twitterpasswd';  
  11. my $IdenticaUser = 'identicalogin';  
  12. my $IdenticaPass = 'identicapasswd';  
  13. my $TwitterUse = 1;  # 1-> send to twitter, 0 -> do not send  
  14. my $IdenticaUse = 1; # 1-> send to identica, 0 -> do not send  
  15. my $Status;  
  16.   
  17. #Parse command line arguments  
  18. GetOptions ("status=s" => \$Status);  
  19. say "Sending: $Status";  
  20. #There's a hard limit on the size of twits  
  21. if (length $Status > 140) {  
  22.    say "Too long";  
  23.    exit;  
  24. }  
  25. if ($TwitterUse) {  
  26.    my $twitter = Net::Twitter->new(username => $TwitUser, password => $TwitPass);  
  27.    if ($twitter->update($Status)) {  
  28.        say "twitter: OK";  
  29.    }  
  30.    else {  
  31.        say "twitter: FAIL";  
  32.    }  
  33. }  
  34. if ($IdenticaUse) {  
  35.    my $identica = Net::Twitter->new(identica => 1, username => $IdenticaUser, password => $IdenticaPass);  
  36.    if ($identica->update($Status)) {  
  37.        say "Identica: OK";  
  38.    }  
  39.    else {  
  40.        say "Identica: FAIL";  
  41.    }  
  42. }  
  43.   
  44. __END__  
  45. History:  
  46. v0.0.1 (2009/02/08): First version of twit.pl. Use as:  
  47.                     >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

No comments :

Post a Comment