Last active
April 3, 2016 04:28
-
-
Save the-frey/5021183 to your computer and use it in GitHub Desktop.
An extension of the Twitter API lesson from Codecademy to create a command line app that accepts user input. The idea is you can just have it open in a tab in Bash while you're doing other things. I actually use this all the time now...!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'oauth' | |
require 'json' | |
#You will need to add your keys below and your username where indicated inside the method. | |
CONSUMER_KEY1 = "" #consumer key | |
CONSUMER_KEY2 = "" #consumer secret | |
ACCESS_TOKEN1 = "" #access token | |
ACCESS_TOKEN2 = "" #access secret | |
consumer_key = OAuth::Consumer.new(CONSUMER_KEY1,CONSUMER_KEY2) | |
access_token = OAuth::Token.new(ACCESS_TOKEN1,ACCESS_TOKEN2) | |
choice = nil | |
def tweet_method(consumer_key, access_token, choice) | |
#You will need these for OAuth. The second of each should be your secret value. | |
#The naming simply refects the order in the two OAuth class constructors. | |
case choice | |
when "tweet" | |
puts "What do you want to tweet?" | |
print "> " | |
tweet_text = STDIN.gets.chomp | |
#tweet_text.to_s | |
if tweet_text.length > 140 | |
tweet_invalid = true | |
abort("The tweet is too long (#{tweet_text.length}).") | |
end | |
baseurl = "https://api.twitter.com" | |
path = "/1.1/statuses/update.json" | |
address = URI("#{baseurl}#{path}") | |
request = Net::HTTP::Post.new address.request_uri | |
request.set_form_data( | |
"status" => "#{tweet_text}" | |
) | |
#Set up HTTP | |
http = Net::HTTP.new address.host, address.port | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_PEER | |
#Debugger | |
#puts http.set_debug_output($stderr) | |
#Issue the request | |
request.oauth! http, consumer_key, access_token | |
http.start | |
response = http.request request unless tweet_invalid == true | |
#Parse and print the tweet if the response code was 200 | |
tweet = nil | |
if response.code == '200' | |
tweet = JSON.parse(response.body) | |
puts "Successfully posted Tweet: #{tweet["text"]}." | |
else | |
puts "Could not send the Tweet! " + "Code: #{response.code} \nBody: #{response.body}" | |
end | |
return choice | |
when "check" | |
# All requests will be sent to this server. Add your screen name below. | |
baseurl = "https://api.twitter.com" | |
query = URI.encode_www_form( | |
"screen_name" => "put_your_screen_name_here", | |
"count" => "10", | |
"contributor_details" => true | |
) | |
# The verify credentials endpoint returns a 200 status if | |
# the request is signed correctly. | |
address = URI("#{baseurl}/1.1/statuses/mentions_timeline.json?#{query}") | |
def print_mentions(tweets) | |
#puts JSON.pretty_generate(tweets) | |
tweets.each do |t| | |
output = "" | |
output << "#{t["user"]["name"]} " | |
output << "(#{t["user"]["screen_name"]}) " | |
output << "#{t["created_at"]}: \n" | |
output << "#{t["text"]}\n\n" | |
output << "--- \n\n" | |
puts output | |
end | |
end | |
# Set up Net::HTTP to use SSL, which is required by Twitter. | |
http = Net::HTTP.new address.host, address.port | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_PEER | |
#debugger | |
#puts http.set_debug_output($stderr) | |
# Build the request and authorize it with OAuth. | |
request = Net::HTTP::Get.new address.request_uri | |
request.oauth! http, consumer_key, access_token | |
# Issue the request and return the response. | |
http.start | |
response = http.request request | |
tweets = nil | |
if response.code == '200' | |
tweets = JSON.parse(response.body) | |
print_mentions(tweets) | |
else | |
puts "Failed, response #{response.code}" | |
end | |
return choice | |
when "exit" | |
sec = Time.now.sec | |
i = sec + 3 | |
until sec == i | |
print "." | |
until Time.now.sec == sec + 1 | |
#do nothing | |
end | |
sec += 1 | |
end | |
abort("Quitting...") | |
when "help" | |
puts "Type 'Tweet' or 'tweet' to compose a tweet." | |
puts "Type 'Check' or 'check' to check your mentions." | |
puts "Type 'exit' to quit." | |
return choice | |
else | |
puts "I don't understand that! Use 'check' to check tweets or 'tweet' to compose a tweet." | |
return choice | |
end | |
end | |
puts <<MESSAGE | |
| // _ _ _ _ _ _ | |
| /| // |_ | | | | | | | |_ | |
|/ |// |_ |_ |_ |_| | | |_ | |
Command line Twitter was hacked together with love | |
in Manchester, UK. Tweet me: @hipsters_unite | |
MESSAGE | |
until choice == "exit" | |
puts "What do you want to do? Use 'check', 'tweet' or 'exit'." | |
print "> " | |
choice = STDIN.gets.chomp | |
choice.downcase! | |
tweet_method(consumer_key, access_token, choice) | |
puts "\n\n---------" | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment