Module: Tweetline
- Defined in:
- lib/tweetline.rb,
lib/tweetline/cli.rb
Defined Under Namespace
Classes: CLI
Constant Summary collapse
- CONSUMER_KEY =
"cbaTxnPKFhGAngIYDqsNyg"
- CONSUMER_SECRET =
"FTrMluAB5pMPEZZRoGmOf2AH3md2KSqW6PNwD7TAzc"
Class Method Summary collapse
- .authorize_user ⇒ Object
- .cat_tweet(tweet_id, created_at, name, screen_name, text) ⇒ Object
- .configure_pipes ⇒ Object
- .configure_twitter ⇒ Object
- .each_tweet(options = {:count => 10}) ⇒ Object
- .is_piped_to_tweetline? ⇒ Boolean
- .put_tweet(tweet_id, created_at, name, screen_name, text) ⇒ Object
- .say_tweet(tweet_id, created_at, name, screen_name, text) ⇒ Object
- .time_stamp(time) ⇒ Object
Class Method Details
.authorize_user ⇒ Object
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 |
# File 'lib/tweetline.rb', line 13 def api_url = "https://api.twitter.com" consumer = OAuth::Consumer.new(Tweetline::CONSUMER_KEY, Tweetline::CONSUMER_SECRET, :site => api_url) request_token = consumer.get_request_token request = consumer.create_signed_request(:get, consumer., request_token, {:oauth_callback => 'oob'}) params = request['Authorization'].sub(/^OAuth\s+/, '').split(/,\s+/).map { |p| k, v = p.split('=') v =~ /"(.*?)"/ "#{k}=#{CGI::escape($1)}" }.join('&') auth_url = "#{api_url}#{request.path}?#{params}" #bitly = Bitly.new("tweetline", "R_c27cfa45caf4f11cea7fb224056fb7f5") bitly = Shortly::Clients::Bitly puts puts "Follow these steps to access your twitter account." puts puts "1) Navigate to the following url..." puts puts bitly.shorten(auth_url, {:apiKey => 'R_c27cfa45caf4f11cea7fb224056fb7f5', :login => 'tweetline'}).url puts puts "2) Allow Tweet-line access to your twitter account..." puts puts "3) Enter your pin number..." puts pin = gets puts access_token = request_token.get_access_token(:oauth_verifier => pin.chomp) File.open(rc_file_name, "w") do |f| f.write({:oauth_token => access_token.token, :oauth_token_secret => access_token.secret}.to_yaml) end end |
.cat_tweet(tweet_id, created_at, name, screen_name, text) ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/tweetline.rb', line 49 def cat_tweet(tweet_id, created_at, name, screen_name, text) Tweetline.put_tweet(tweet_id, created_at, name, screen_name, text) tweet = Twitter.status(tweet_id) puts "Conversation:", "" if tweet.in_reply_to_status_id while tweet.in_reply_to_status_id tweet = Twitter.status(tweet.in_reply_to_status_id) Tweetline.put_tweet(tweet.id, tweet.created_at, tweet.user.name, tweet.user.screen_name, tweet.text) end end |
.configure_pipes ⇒ Object
77 78 79 |
# File 'lib/tweetline.rb', line 77 def configure_pipes STDOUT.sync = true end |
.configure_twitter ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/tweetline.rb', line 60 def configure_twitter if File.exist?(rc_file_name) Twitter.configure do |config| File.open(rc_file_name) do |yaml| = YAML.load(yaml) config.consumer_key = [:consumer_key] || Tweetline::CONSUMER_KEY config.consumer_secret = [:consumer_secret] || Tweetline::CONSUMER_SECRET config.oauth_token = [:oauth_token] config.oauth_token_secret = [:oauth_token_secret] end end else Tweetline. configure_twitter end end |
.each_tweet(options = {:count => 10}) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/tweetline.rb', line 81 def each_tweet(={:count => 10}) if STDIN.fcntl(Fcntl::F_GETFL, 0) == 0 STDIN.each do |line| yield JSON.load(line) end else if [:screen_name] screen_name = .delete(:screen_name) timeline = Twitter.user_timeline(screen_name, ) else timeline = Twitter.home_timeline() end timeline.each do |tweet| yield({"id" => tweet.id, "created_at" => tweet.created_at, "name" => tweet.user.name, "screen_name" => tweet.user.screen_name, "text" => tweet.text}) end end end |
.is_piped_to_tweetline? ⇒ Boolean
99 100 101 |
# File 'lib/tweetline.rb', line 99 def is_piped_to_tweetline? @is_piped_to_tweetline |= `ps -ax -o pid,args | grep -E "^[ ]*#{Process.pid+1}"` =~ /\/tl[^\/]*$/ end |
.put_tweet(tweet_id, created_at, name, screen_name, text) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/tweetline.rb', line 103 def put_tweet(tweet_id, created_at, name, screen_name, text) if STDOUT.fcntl(Fcntl::F_GETFL, 0) == 1 and Tweetline.is_piped_to_tweetline? puts({"id" => tweet_id, "created_at" => created_at, "name" => name, "screen_name" => screen_name, "text" => text}.to_json) else puts "#{name} (@#{screen_name}) [#{time_stamp(created_at)}]", " #{text} (#{tweet_id})", "" end end |
.say_tweet(tweet_id, created_at, name, screen_name, text) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/tweetline.rb', line 111 def say_tweet(tweet_id, created_at, name, screen_name, text) tweet_text = text.strip.gsub(/http:.*?( |$)/, '').gsub(/^RT/, '').gsub(/@/, '').split(/ /).map{|s| s.gsub(/[^A-Za-z0-9.!?,']/, '')}.join(' ') connector = text.strip =~ /^RT/ ? " retweets " : "says" # puts name, " #{connector} #{tweet_text}", "" Tweetline.put_tweet(tweet_id, created_at, name, screen_name, text) `say #{name} #{connector} "#{tweet_text}"` end |
.time_stamp(time) ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/tweetline.rb', line 119 def time_stamp(time) time = Time.parse(time) format = '%I:%M %p' format = '%m/%d/%y ' + format unless time.yday == Time.now.yday return time.strftime(format) end |