Last active
December 27, 2015 02:08
-
-
Save vivien/7249793 to your computer and use it in GitHub Desktop.
Fetch last few tweets, unauthenticated.
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
# Fetch last few tweets, unauthenticated. | |
# | |
# I just want the last few tweets of a user, I really don't want to do this: | |
# http://stackoverflow.com/a/15314662 | |
require 'open-uri' | |
require 'cgi' | |
require 'openssl' | |
module Twitter | |
module_function | |
def last_tweets(user) | |
url = "https://twitter.com/" << user | |
html = open(url, :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE).read | |
html.scan(/<p class="js-tweet-text tweet-text">(.*)<\/p>/).map do |p| | |
p = p.first.gsub(/<\/?[^>]*>/, '') # remove HTML elements | |
p = CGI.unescape_html(p) # remove some HTML sequences like encoded ' | |
p.gsub(' ', '') # unescape doesn't remove them | |
end | |
rescue OpenURI::HTTPError | |
[] | |
end | |
end | |
if __FILE__ == $0 | |
user = ARGV.first || "twitterapi" | |
puts Twitter.last_tweets(user) | |
end | |
# vim: et ts=2 sw=2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment