Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Grab tagged photos from Facebook Graph API
# Ruby script to grab photos tagged of yourself on facebook and download them.
# Go to the Graph API explorer (http://developers.facebook.com/tools/explorer)
# and generate an access token with the permissions you need (user_photos, user_photo_video_tags)
# and use that access token here.
require 'json'
require 'net/http'
def fetch(output_dir, username, photo_limit, access_token)
url = "https://graph.facebook.com/#{username}/photos?limit=#{photo_limit}&access_token=#{access_token}"
uri = URI.parse(URI.encode(url))
http_session = Net::HTTP.new(uri.host, uri.port)
http_session.use_ssl = true
response = http_session.request(Net::HTTP::Get.new(uri.request_uri))
jsonresult = JSON.parse(response.body)
result_data = jsonresult["data"]
if result_data.nil?
puts "No data to parse. Maybe you've got an invalid access token or uri?"
return
end
for entry in result_data
system "wget --directory-prefix='#{output_dir}' #{entry['source']}"
end
end
fetch('my-photos', 'YOUR_FACEBOOK_IDENTIFIER', 2000, 'YOUR_ACCESS_TOKEN')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment