phoghawk / twitterbuddy

Command line utility to print recent tweets of you, your friends, or everyone on twitter.

This URL has Read+Write access

phoghawk (author)
Wed Mar 11 18:21:41 -0700 2009
commit  eaf6aeefd030cfecf3f799eac8d31b889df3b8fe
tree    3f2898fc268ce10e7540dc3d8870f2e0ab3c8c48
parent  30264f83febe9eb831f1a7b56ebf91a7e6ebffee
twitterbuddy / twitterBuddy
100755 102 lines (94 sloc) 3.482 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/ruby
# Copyright Benjamin Goering, 2009
# b@enjam.in
# Released under GNU GPL v3
require 'rubygems'
require 'twitter'
require 'htmlentities'
require 'optparse'
require 'ostruct'
 
if ARGV.size == 0 then
  print "Not enough args. Use `twitterBuddy -h` for help.\n"
  exit 1
end
 
class TwitterOpts
  def self.parse(args)
    options = OpenStruct.new
    options.linebreaks = 2
    options.numtweets = 15
    options.title = "Twitter Timeline"
    options.hidetitle = false
    options.who = :friends
    options.need_to_log_on = false;
    
    opts = OptionParser.new do |opts|
      
      opts.on_tail("-h", "--help", "Show this message") do
puts opts
puts "\tNote: the --search and --hashtag switches have precedence over the -w flag."
puts "\tAlso: you do not need to supply username and password flags to use the --search and --hashtag switches."
exit
      end
      opts.on("-u", "--username login",
       "Login to twitter with username [login]") {|login| options.login = login}
      opts.on("-p", "--password pass",
       "Login to twitter with password [pass]") do |pass|
       options.pass = pass
       options.need_to_log_on = true
      end
      opts.on("-n", "--num-tweets num", Integer,
       "Show [num] tweets") {|n| options.numtweets = n}
      opts.on("-l", "--line-breaks breaks", Integer,
       "Insert [breaks] linebreaks between each tweet"){|breaks| options.linebreaks = breaks}
      opts.on("-w", "--who who", ["everybody","me",:friends],
      "Select which tweets to show (public, me, friends)") do |who|
        who = case who
                  when "me" then :user
                  when "everybody" then :public
                end if who.class == String
        options.need_to_log_on = true;
        options.who = who
      end
      opts.on("-s", "--search query",
        "Show results of search for keyword [query]") do |query|
          options.searchquery = query
      end
      opts.on("-g", "--hashtag tag", String,
        "Shows only tweets with hashtag of [tag]") {|tag| options.hasht = tag}
      opts.on("-t", "--title title", String,
       "Replaces title text with custom [title] string") {|title| options.title = title}
      opts.on(:NONE, "-i", "--hide-title", "Hide title string") { options.hidetitle = true}
      
    end #opts
    
    opts.parse!(args)
    options
  end #parse()
  
end #class TwitterOpts
 
options = TwitterOpts.parse(ARGV)
decoder = HTMLEntities.new
print options.title + "\n"*options.linebreaks unless (options.hidetitle)
if (options.searchquery or options.hasht)
  search = Twitter::Search.new
  search.containing(options.searchquery) if options.searchquery
  search.hashed(options.hasht) if options.hasht
  begin
    tempr = search.fetch
    raise error if tempr["results"].size==0
  rescue
    puts "ERROR: No results found for that query"
    exit 1
  end
  results = tempr["results"]
  for result in results[0,options.numtweets]
    printf "%s: %s",result["from_user"],decoder.decode(result["text"]) + "\n"*options.linebreaks
  end
elsif options.need_to_log_on then
  begin
    twit = Twitter::Base.new(options.login,options.pass)
    tweets = twit.timeline(options.who)[0,options.numtweets]
    for tweet in tweets
     printf "%s: %s",tweet.user.screen_name,decoder.decode(tweet.text) + "\n"*options.linebreaks
    end
  rescue
    puts "ERROR: Could not connect with twitter. Did you specify a username and password?"
    exit 1
  end
else
end