Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #----------------------------------------------------------------------#
- #twitter.pl : Command line Twitter client. Win32/Linux/Mac #
- # Perl script to use the Twitter social networking at the command line #
- # to update your status, check other statuses, check your statuses, and#
- # much more. #
- #----------------------------------------------------------------------#
- # -- USE STATEMENTS :
- # This script requires the following packages:
- # Net::Twitter
- # WWW::Shorten::isgd
- use Net::Twitter;
- use WWW::Shorten::isgd;
- use strict;
- # -- CREATE TWITTER INSTANCE
- my $twit = Net::Twitter->new();
- # -- GLOBAL VARIABLES
- my $console_on = 0;
- # -- SUBROUTINES
- sub StartTwitConsole {
- # -- SUB INFO
- # TODO: Convert the long if-else block into a switch block. Looks cleaner.
- # Add more commands.
- # -- END SUB INFO
- # Check if console is supposed to be on. Not an efficient way, working on it.
- if($console_on == 0){
- $console_on = 1;
- } elsif ($console_on == 2) {
- $console_on = 0;
- }
- # Run main console loop
- while ($console_on == 1) {
- print "twitter -> "; my $cmd = <STDIN>;
- chomp($cmd);
- my @cmd_split = split(/ /, $cmd);
- my @args = ();
- for(my $i=1;$i<$#cmd_split + 1;$i++) {
- push(@args, $cmd_split[$i]);
- }
- if($cmd_split[0] eq 'login') {
- &loginTwitter(@args);
- }
- elsif($cmd_split[0] eq 'exit') {
- &exitConsole;
- }
- elsif($cmd_split[0] eq 'showlast') {
- &showLast(@args);
- }
- elsif($cmd_split[0] eq 'update') {
- my %urls = &checkAndShortenURL(@args);
- my $url_count = $#{$urls{'full'}};
- my $temp_args = join(' ', @args);
- print "URL Shortner Output:\n";
- print "Original: $temp_args\n";
- my $cnt = -1;
- foreach my $check (@{$urls{full}}) {
- $cnt++;
- $temp_args =~ s/$check/$urls{'short'}[$cnt]/;
- print "$check => $urls{'short'}[$cnt]\n";
- }
- print "New: $temp_args\n\n";
- @args = split(/ /, $temp_args);
- &updateTwitter(@args);
- }
- elsif($cmd_split[0] eq 'showfriends') {
- &showFriends(@args);
- }
- elsif($cmd_split[0] eq 'shrturls') {
- my %urls = &checkAndShortenURL(@args);
- my $url_count = $#{$urls{'full'}};
- print "URL Shortner Output:\n";
- my $cnt = -1;
- foreach my $check (@{$urls{full}}) {
- $cnt++;
- print "$check => $urls{'short'}[$cnt]\n";
- }
- }
- }
- }
- # - COMMAND SUBS
- sub loginTwitter {
- my $user = shift(@_);
- my $pass = shift(@_);
- $twit->credentials($user, $pass);
- print "Logged in.\n\n";
- }
- sub exitConsole {
- $console_on = 2;
- }
- sub showLast {
- my $count = shift(@_);
- if($count == 0) {
- $count = 20;
- }
- my $tweets = $twit->user_timeline({count => $count});
- foreach my $tweet (@{$tweets}) {
- my $val = $tweet->{text};
- my $time = $tweet->{created_at};
- print "$time\n\t$val\n";
- }
- print "\n";
- }
- sub updateTwitter {
- my $tweet = "";
- foreach(@_) {
- $tweet = $tweet." ".$_;
- }
- $twit->update({status => $tweet});
- print "Updated to: $tweet\n\n";
- }
- sub showFriends {
- my $count = shift(@_);
- my $tweets = $twit->friends_timeline({count => $count});
- foreach my $tweet (@{$tweets}) {
- my $val = $tweet->{text};
- my $time = $tweet->{created_at};
- print "$time\n\t$val\n";
- }
- print "\n";
- }
- sub checkAndShortenURL {
- my @tweetToCheck = ();
- foreach(@_) {
- push(@tweetToCheck, $_);
- }
- my @urls = grep(/(.*http:\/\/.+.*)/, @tweetToCheck);
- #TODO: replace old URLs in tweet string.
- my @short_urls = ();
- foreach my $url (@urls) {
- push(@short_urls, WWW::Shorten::isgd::makeashorterlink($url));
- }
- my %all_urls = (full => \@urls,
- short => \@short_urls);
- return %all_urls;
- }
- sub checkDirectMessages {
- my $dm_list = $twit->direct_messages();
- print "Received:\n";
- foreach(@$dm_list) {
- print $_."\n";
- }
- print "Sent:\n";
- my $sent_dm_list = $twit->sent_direct_messages();
- foreach(@$sent_dm_list) {
- print $_."\n";
- }
- }
- # -- CONSOLE CALL/MAIN LOOP
- &StartTwitConsole();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement