Skip to content

Instantly share code, notes, and snippets.

@vivien
Last active December 27, 2015 02:08
Show Gist options
  • Save vivien/7249793 to your computer and use it in GitHub Desktop.
Save vivien/7249793 to your computer and use it in GitHub Desktop.
Fetch last few tweets, unauthenticated.
# 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('&nbsp;', '') # 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