Last active
December 25, 2015 17:00
-
-
Save jeepkd/689316cd58e6be19a303 to your computer and use it in GitHub Desktop.
Quick ruby script to back up tagged facebook photos
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
source 'https://rubygems.org' | |
gem 'koala' | |
gem 'awesome_print' | |
gem 'mini_exiftool' |
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 'koala' | |
require 'awesome_print' | |
require 'open-uri' | |
require 'mini_exiftool' | |
OAUTH_ACCESS_TOKEN = 'your facebook access token here' | |
LIMIT = 350 | |
def photo_count(photos) | |
sum = 0 | |
while photos | |
sum += photos.count | |
photos = photos.next_page | |
end | |
sum | |
end | |
def put_info(attr, value) | |
"#{attr}: \"#{value}\"\n" | |
end | |
@graph = Koala::Facebook::API.new(OAUTH_ACCESS_TOKEN) | |
photos = @graph.get_connections("me", "photos", limit: LIMIT) | |
index = photo_count photos | |
while photos | |
photos.each do |photo| | |
filename = "fb_photo_#{index.to_s.rjust(4,'0')}.jpg" | |
index -= 1 | |
unless File.exist?(filename) | |
File.open(filename, 'wb') do |fo| | |
fo.write open(photo['images'].first['source']).read | |
end | |
description = Hash.new | |
file = MiniExiftool.new filename | |
file.title = photo['name'] | |
file.album = photo['album'] | |
description['album'] = photo['album'] | |
file.author = photo['from']['name'] | |
description['author'] = photo['from']['name'] | |
file.datetimeoriginal = photo['backdated_time'] || photo['created_time'] | |
if photo['place'] | |
description['place'] = photo['place']['name'] | |
file.gpslatitude = photo['place']['location']['latitude'] | |
file.gpslongitude = photo['place']['location']['longitude'] | |
end | |
description.reject!{|k, v| v.nil?} | |
file.description = description.to_json | |
file.save | |
end | |
ap filename | |
end | |
photos = photos.next_page | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment