Created
August 23, 2014 23:46
-
-
Save chasewoodford/ed06285b546cd82f89f5 to your computer and use it in GitHub Desktop.
Ruby Twitter User Feed Scrapper
This file contains hidden or 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
# Code from Codecademy - Twitter API course (http://www.codecademy.com/en/tracks/twitter) | |
require 'rubygems' | |
require 'oauth' | |
require 'json' | |
# Now you will fetch /1.1/statuses/user_timeline.json, | |
# returns a list of public Tweets from the specified | |
# account. | |
baseurl = "https://api.twitter.com" | |
path = "/1.1/statuses/user_timeline.json" | |
query = URI.encode_www_form( | |
"screen_name" => "chase1263070", | |
"count" => 20, | |
) | |
address = URI("#{baseurl}#{path}?#{query}") | |
request = Net::HTTP::Get.new address.request_uri | |
# Print data about a list of Tweets | |
def print_timeline(tweets) | |
tweets.each do |tweet| | |
puts tweet['text'] | |
end | |
end | |
# Set up HTTP. | |
http = Net::HTTP.new address.host, address.port | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_PEER | |
# If you entered your credentials in the first | |
# exercise, no need to enter them again here. The | |
# ||= operator will only assign these values if | |
# they are not already set. | |
consumer_key ||= OAuth::Consumer.new "[CONSUMER_KEY]", "[CONSUMER_SECRET]" | |
access_token ||= OAuth::Token.new "[ACCESS_TOKEN]", "[ACCESS_TOKEN_SECRET]" | |
# Issue the request. | |
request.oauth! http, consumer_key, access_token | |
http.start | |
response = http.request request | |
# Parse and print the Tweet if the response code was 200 | |
tweets = nil | |
if response.code == '200' then | |
tweets = JSON.parse(response.body) | |
print_timeline(tweets) | |
end | |
nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment