Skip to content

Instantly share code, notes, and snippets.

@sferik
Last active September 4, 2021 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sferik/214fb916e0297c438be7aba6303aebca to your computer and use it in GitHub Desktop.
Save sferik/214fb916e0297c438be7aba6303aebca to your computer and use it in GitHub Desktop.
require "http/client"
require "json"
require "oauth"
HOST = "api.twitter.com"
# Generate API keys at https://developer.twitter.com/en/portal/projects-and-apps
API_KEY = "Replace this with your Twitter API Key"
API_SECRET = "Replace this with your Twitter API Key Secret"
ACCESS_TOKEN = "Replace this with your Twitter Access Token"
ACCESS_TOKEN_SECRET = "Replace this with your Twitter Access Token Secret"
PROFILE_IMAGE_SUFFIX_REGEX = /_normal(\.gif|\.jpe?g|\.png)$/i
consumer = OAuth::Consumer.new(HOST, API_KEY, API_SECRET)
access_token = OAuth::AccessToken.new(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
http_client = HTTP::Client.new(HOST, tls: true)
consumer.authenticate(http_client, access_token)
response = http_client.get("/1.1/users/show.json?screen_name=#{ARGV[0]}")
raise "HTTP Error: #{response.status_code} #{response.status}" if response.status_code != 200
json = JSON.parse(response.body)
puts json["profile_image_url_https"].to_s.sub(PROFILE_IMAGE_SUFFIX_REGEX, "\\1")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment