Skip to content
This repository
Browse code

Added a couple features

*Added --search flag and --hashtag flag to show twitter search results
*Added some error checking
*Fixed some of the -h output
  • Loading branch information...
commit 30264f83febe9eb831f1a7b56ebf91a7e6ebffee 1 parent 41ac834
Benjamin Michael Goering authored March 11, 2009

Showing 1 changed file with 47 additions and 12 deletions. Show diff stats Hide diff stats

  1. 59  twitterBuddy
59  twitterBuddy
@@ -17,36 +17,49 @@ class TwitterOpts
17 17
   def self.parse(args)
18 18
     options = OpenStruct.new
19 19
     options.linebreaks = 2
20  
-    options.who = :friends
21 20
     options.numtweets = 15
22 21
     options.title = "Twitter Timeline"
23 22
     options.hidetitle = false
  23
+    options.who = :friends
  24
+    options.need_to_log_on = false;
24 25
     
25 26
     opts = OptionParser.new do |opts|
26 27
       
27 28
       opts.on_tail("-h", "--help", "Show this message") do
28  
-	puts opts
29  
-	exit
  29
+	      puts opts
  30
+	      puts "\tNote: the --search and --hashtag switches have precedence over the -w flag."
  31
+	      puts "\tAlso: you do not need to supply username and password flags to use the --search and --hashtag switches."
  32
+	      exit
30 33
       end
31 34
       opts.on("-u", "--username login",
32 35
       	"Login to twitter with username [login]")	{|login| options.login = login}
33 36
       opts.on("-p", "--password pass",
34  
-      	"Login to twitter with password [pass]")	{|pass| options.pass = pass}
  37
+      	"Login to twitter with password [pass]") do |pass|
  38
+      	  options.pass = pass
  39
+      	  options.need_to_log_on = true
  40
+      end
35 41
       opts.on("-n", "--num-tweets num", Integer,
36 42
       	"Show [num] tweets")				{|n| options.numtweets = n}
37 43
       opts.on("-l", "--line-breaks breaks", Integer,
38 44
       	"Insert [breaks] linebreaks between each tweet"){|breaks| options.linebreaks = breaks}
39 45
       opts.on("-w", "--who who", ["everybody","me",:friends],
40  
-      "Select which tweets to show (public, mine, friends)") do |who|
  46
+      "Select which tweets to show (public, me, friends)") do |who|
41 47
         who =   case who
42 48
                   when "me" then :user
43 49
                   when "everybody" then :public
44 50
                 end if who.class == String
  51
+        options.need_to_log_on = true;
45 52
         options.who = who
46 53
       end
47  
-      opts.on("-t", "--title [title]", String,
  54
+      opts.on("-s", "--search query",
  55
+        "Show results of search for keyword [query]") do |query|
  56
+          options.searchquery = query
  57
+      end
  58
+      opts.on("-g", "--hashtag tag", String,
  59
+        "Shows only tweets with hashtag of [tag]") {|tag| options.hasht = tag}
  60
+      opts.on("-t", "--title title", String,
48 61
       	"Replaces title text with custom [title] string") {|title| options.title = title}
49  
-      opts.on("-i", "--hide-title", "Hide title string") {|i| options.hidetitle = i}
  62
+      opts.on(:NONE, "-i", "--hide-title", "Hide title string") { options.hidetitle = true}
50 63
       
51 64
     end #opts
52 65
     
@@ -57,11 +70,33 @@ class TwitterOpts
57 70
 end #class TwitterOpts
58 71
 
59 72
 options = TwitterOpts.parse(ARGV)
60  
-twit = Twitter::Base.new(options.login,options.pass)
61 73
 decoder = HTMLEntities.new
62  
-newest_tweets = twit.timeline(options.who).select{|tweet| DateTime.parse(tweet.created_at) >= (DateTime.now-1)}[0,options.numtweets]
63  
-
64 74
 print options.title + "\n"*options.linebreaks unless (options.hidetitle)
65  
-for tweet in newest_tweets
66  
-	printf "%s: %s",tweet.user.screen_name,decoder.decode(tweet.text) + "\n"*options.linebreaks
  75
+if (options.searchquery or options.hasht)
  76
+  search = Twitter::Search.new
  77
+  search.containing(options.searchquery) if options.searchquery
  78
+  search.hashed(options.hasht) if options.hasht
  79
+  begin
  80
+    tempr = search.fetch
  81
+    raise error if tempr["results"].size==0
  82
+  rescue
  83
+    puts "ERROR: No results found for that query"
  84
+    exit 1
  85
+  end
  86
+  results = tempr["results"]
  87
+  for result in results[0,options.numtweets]
  88
+    printf "%s: %s",result["from_user"],decoder.decode(result["text"]) + "\n"*options.linebreaks
  89
+  end
  90
+elsif options.need_to_log_on then
  91
+  begin
  92
+    twit = Twitter::Base.new(options.login,options.pass)
  93
+    tweets = twit.timeline(options.who)[0,options.numtweets]
  94
+    for tweet in tweets
  95
+    	printf "%s: %s",tweet.user.screen_name,decoder.decode(tweet.text) + "\n"*options.linebreaks
  96
+    end
  97
+  rescue
  98
+    puts "ERROR: Could not connect with twitter. Did you specify a username and password?"
  99
+    exit 1
  100
+  end
  101
+else 
67 102
 end

0 notes on commit 30264f8

Please sign in to comment.
Something went wrong with that request. Please try again.