Advertisement
Guest User

Untitled

a guest
Jul 5th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. #----------------------------------------------------------------------#
  2. #twitter.pl : Command line Twitter client. Win32/Linux/Mac #
  3. # Perl script to use the Twitter social networking at the command line #
  4. # to update your status, check other statuses, check your statuses, and#
  5. # much more. #
  6. #----------------------------------------------------------------------#
  7.  
  8. # -- USE STATEMENTS :
  9. # This script requires the following packages:
  10. # Net::Twitter
  11. # WWW::Shorten::isgd
  12. use Net::Twitter;
  13. use WWW::Shorten::isgd;
  14. use strict;
  15.  
  16. # -- CREATE TWITTER INSTANCE
  17. my $twit = Net::Twitter->new();
  18.  
  19. # -- GLOBAL VARIABLES
  20. my $console_on = 0;
  21.  
  22. # -- SUBROUTINES
  23. sub StartTwitConsole {
  24. # -- SUB INFO
  25. # TODO: Convert the long if-else block into a switch block. Looks cleaner.
  26. # Add more commands.
  27. # -- END SUB INFO
  28. # Check if console is supposed to be on. Not an efficient way, working on it.
  29. if($console_on == 0){
  30. $console_on = 1;
  31. } elsif ($console_on == 2) {
  32. $console_on = 0;
  33. }
  34. # Run main console loop
  35. while ($console_on == 1) {
  36. print "twitter -> "; my $cmd = <STDIN>;
  37. chomp($cmd);
  38. my @cmd_split = split(/ /, $cmd);
  39. my @args = ();
  40. for(my $i=1;$i<$#cmd_split + 1;$i++) {
  41. push(@args, $cmd_split[$i]);
  42. }
  43. if($cmd_split[0] eq 'login') {
  44. &loginTwitter(@args);
  45. }
  46. elsif($cmd_split[0] eq 'exit') {
  47. &exitConsole;
  48. }
  49. elsif($cmd_split[0] eq 'showlast') {
  50. &showLast(@args);
  51. }
  52. elsif($cmd_split[0] eq 'update') {
  53. my %urls = &checkAndShortenURL(@args);
  54. my $url_count = $#{$urls{'full'}};
  55. my $temp_args = join(' ', @args);
  56. print "URL Shortner Output:\n";
  57. print "Original: $temp_args\n";
  58. my $cnt = -1;
  59. foreach my $check (@{$urls{full}}) {
  60. $cnt++;
  61. $temp_args =~ s/$check/$urls{'short'}[$cnt]/;
  62. print "$check => $urls{'short'}[$cnt]\n";
  63. }
  64. print "New: $temp_args\n\n";
  65. @args = split(/ /, $temp_args);
  66. &updateTwitter(@args);
  67. }
  68. elsif($cmd_split[0] eq 'showfriends') {
  69. &showFriends(@args);
  70. }
  71. elsif($cmd_split[0] eq 'shrturls') {
  72. my %urls = &checkAndShortenURL(@args);
  73. my $url_count = $#{$urls{'full'}};
  74. print "URL Shortner Output:\n";
  75. my $cnt = -1;
  76. foreach my $check (@{$urls{full}}) {
  77. $cnt++;
  78. print "$check => $urls{'short'}[$cnt]\n";
  79. }
  80. }
  81. }
  82. }
  83.  
  84. # - COMMAND SUBS
  85. sub loginTwitter {
  86. my $user = shift(@_);
  87. my $pass = shift(@_);
  88. $twit->credentials($user, $pass);
  89. print "Logged in.\n\n";
  90. }
  91.  
  92. sub exitConsole {
  93. $console_on = 2;
  94. }
  95.  
  96. sub showLast {
  97. my $count = shift(@_);
  98. if($count == 0) {
  99. $count = 20;
  100. }
  101. my $tweets = $twit->user_timeline({count => $count});
  102. foreach my $tweet (@{$tweets}) {
  103. my $val = $tweet->{text};
  104. my $time = $tweet->{created_at};
  105. print "$time\n\t$val\n";
  106. }
  107. print "\n";
  108. }
  109.  
  110. sub updateTwitter {
  111. my $tweet = "";
  112. foreach(@_) {
  113. $tweet = $tweet." ".$_;
  114. }
  115. $twit->update({status => $tweet});
  116. print "Updated to: $tweet\n\n";
  117. }
  118.  
  119. sub showFriends {
  120. my $count = shift(@_);
  121. my $tweets = $twit->friends_timeline({count => $count});
  122. foreach my $tweet (@{$tweets}) {
  123. my $val = $tweet->{text};
  124. my $time = $tweet->{created_at};
  125. print "$time\n\t$val\n";
  126. }
  127. print "\n";
  128. }
  129.  
  130. sub checkAndShortenURL {
  131. my @tweetToCheck = ();
  132. foreach(@_) {
  133. push(@tweetToCheck, $_);
  134. }
  135. my @urls = grep(/(.*http:\/\/.+.*)/, @tweetToCheck);
  136. #TODO: replace old URLs in tweet string.
  137. my @short_urls = ();
  138. foreach my $url (@urls) {
  139. push(@short_urls, WWW::Shorten::isgd::makeashorterlink($url));
  140. }
  141. my %all_urls = (full => \@urls,
  142. short => \@short_urls);
  143. return %all_urls;
  144.  
  145. }
  146.  
  147. sub checkDirectMessages {
  148. my $dm_list = $twit->direct_messages();
  149. print "Received:\n";
  150. foreach(@$dm_list) {
  151. print $_."\n";
  152. }
  153. print "Sent:\n";
  154. my $sent_dm_list = $twit->sent_direct_messages();
  155. foreach(@$sent_dm_list) {
  156. print $_."\n";
  157. }
  158. }
  159.  
  160. # -- CONSOLE CALL/MAIN LOOP
  161. &StartTwitConsole();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement