Created
August 23, 2012 20:30
-
-
Save denniskuczynski/3441302 to your computer and use it in GitHub Desktop.
Silly Script to Download Some Random Facebook Profile Images
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 "rest_client" | |
MISSING_IMAGE_RESPONSE = RestClient.get "https://graph.facebook.com/012345678/picture?type=large" | |
QUESTION_MARK_IMAGE_RESPONSE = RestClient.get "https://graph.facebook.com/2252150634/picture?type=large" | |
DIGITS = (0..9).to_a | |
def get_random_facebook_profile_image(image_path, max_tries=200) | |
for tries in 0..max_tries | |
# Generate a random 9 digit identifier | |
random_id = '' | |
9.times { random_id << DIGITS.sample.to_s } | |
# Keep trying to find an image that isn't the default MISSING_IMAGE_RESPONSE or QUESTION_MARK_IMAGE_RESPONSE | |
response = nil | |
url = "https://graph.facebook.com/#{random_id}/picture?type=large" | |
begin | |
puts "Attempting to GET Random Facebook Image: #{url}, Try #{tries}" | |
response = RestClient.get url | |
if response.body != MISSING_IMAGE_RESPONSE.body and | |
response.body != QUESTION_MARK_IMAGE_RESPONSE.body | |
open(image_path, "wb") do |file| | |
file.write(response.body) | |
end | |
return true | |
end | |
rescue Exception => e | |
puts "Exception trying to GET Random Facebook Image: #{url}: #{e}" | |
end | |
end | |
return false | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment