Skip to content

Instantly share code, notes, and snippets.

@bpoplauschi
Last active November 4, 2015 23:04
Show Gist options
  • Save bpoplauschi/21cc081a0cd744effbb4 to your computer and use it in GitHub Desktop.
Save bpoplauschi/21cc081a0cd744effbb4 to your computer and use it in GitHub Desktop.
download all Facebook photo albums
require 'rest-graph'
require 'json'
require "open-uri"
# get the access token from https://developers.facebook.com/tools/explorer
ACCESS_TOKEN="CAACEdEose0cBAKlZC1SZAvb3LY4aGkTJgqbE0GAir8cNhT4uVvf9vgw8fCww2BdSUbzWXKMWh65MRzSnyN3EhGSUZCmySqIHqUawt6H9ESchIfcHWk7I751FBXwGF4nfQFsHmuTvtdZBk38hqEfHowGeTyEUcZArLIZA4tdTHINldkWZBKwBP1g2u3tF3b0Q4FcxFNuYyutsgZDZD"
rg = RestGraph.new(:access_token => ACCESS_TOKEN)
albums_next_cursor = nil
albums_downloaded = 0
loop do
if albums_next_cursor.nil?
albums_response = rg.get('v2.5/273.ro/albums', :fields => 'name,count,created_time', :limit => 100)
else
albums_response = rg.get('v2.5/273.ro/albums', :fields => 'name,count,created_time', :limit => 100, :after => albums_next_cursor)
end
albums = albums_response['data']
albums_paging = albums_response['paging']
if !albums_paging.nil?
albums_next_cursor = albums_paging['cursors']['after']
else
albums_next_cursor = nil
end
puts 'List of albums downloaded'
# looks like this
# {
# "data": [
# {
# "count": 2850,
# "created_time": "2011-03-05T15:13:17+0000",
# "name": "Timeline Photos",
# "id": "191928127514673"
# }
# ]
# }
for album in albums
directory_name = album['name']
album_number_of_photos=album['count']
puts 'Making sure the folder ' + directory_name + ' exists'
Dir.mkdir(directory_name) unless File.exists?(directory_name)
album_next_cursor = nil
photos_downloaded = 0
photos_skipped = 0
loop do
puts 'Downloading the photo list for album ' + album['name']
if album_next_cursor.nil?
album_photos_response = rg.get('v2.5/' + album['id'] + '/photos', :fields => 'images,name', :limit => 100)
else
album_photos_response = rg.get('v2.5/' + album['id'] + '/photos', :fields => 'images,name', :limit => 100, :after => album_next_cursor)
end
album_photos = album_photos_response['data']
album_paging = album_photos_response['paging']
if !album_paging.nil?
album_next_cursor = album_paging['cursors']['after']
else
album_next_cursor = nil
end
# looks like this
# {
# "data": [
# {
# "images": [
# {
# "height": 720,
# "source": "https://scontent.xx.fbcdn.net/hphotos-xal1/v/t1.0-9/10151217_1059520597422084_3958860623884554303_n.jpg?oh=397223c10f53033a9f9b5212abe33a98&oe=56BB0627",
# "width": 960
# }
# ],
# "name": "#colectiv",
# "id": "1059520597422084"
# }
# ]
# }
for photo in album_photos
largest_photo_url = photo['images'][0]['source']
imagepath = directory_name + '/' + photo['id'] + '.jpg'
descrpath = directory_name + '/' + photo['id'] + '.txt'
if File.exists?(imagepath)
puts 'Skipping ' + imagepath
photos_skipped += 1
next
end
puts 'Downloading ' + largest_photo_url
open(largest_photo_url) {|f|
puts 'Saving image to ' + imagepath
File.open(imagepath, 'wb') do |file|
file.puts f.read
end
puts 'File saved'
puts 'Saving description to ' + descrpath
File.open(descrpath, 'w') do |file|
file.puts photo['name']
end
}
photos_downloaded += 1
puts "Skipped #{photos_skipped} Downloaded #{photos_downloaded}/#{album_number_of_photos} photos in album"
end
break if album_next_cursor.nil?
end
albums_downloaded += 1
puts "Downloaded #{albums_downloaded}/#{albums.length} albums"
end
break if albums_next_cursor.nil?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment