Created
August 26, 2014 16:26
-
-
Save otobrglez/183b90f6beab20da3250 to your computer and use it in GitHub Desktop.
Fetching all my photos from Facebook
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
#!/usr/bin/env ruby | |
# Use it like this: | |
# token=access_token_here ./fetcher.rb | |
require 'bundler/setup' | |
require 'koala' | |
require 'chronic' | |
class Fetcher | |
def initialize(access_token) | |
@graph = Koala::Facebook::API.new(access_token) | |
end | |
def fetch | |
albums.each_with_index do |album, i| | |
download(album, photos_from(album["id"])) if album["type"] == "normal" | |
# break if i == 10 | |
end | |
end | |
def albums(object_id="me") | |
@albums = [] | |
albums = @graph.get_connections(object_id, "albums", fields: "id,name,type", limit: 200) | |
api_error = false | |
begin | |
albums.each do |album| | |
@albums.push album | |
end unless albums.nil? | |
begin | |
albums = albums.next_page | |
rescue Exception => e | |
api_error = true | |
end | |
end while api_error != true | |
@albums | |
end | |
def photos_from album_id | |
@photos=[] | |
photos = @graph.get_connections(album_id, "photos", fields: "id,name,source", limit: 200) | |
api_error = false | |
begin | |
photos.each do |album| | |
@photos.push album | |
end unless photos.nil? | |
begin | |
photos = photos.next_page | |
rescue Exception => e | |
api_error = true | |
end | |
end while api_error != true | |
@photos | |
end | |
def download album, photos | |
slug = album["name"].downcase.gsub(/[\s.\/_]/, ' ').squeeze(' ').strip.gsub(/[^\w-]/, '').tr(' ', '-') | |
album.merge!({slug: slug}) | |
$stdout.puts "Processing: #{album['name']} - #{album["created_time"]}" | |
Dir.mkdir("data/#{slug}", 0700) rescue $stdout.puts("Skipping: mkdir for #{slug}") | |
# Set time for folder | |
# created_at = Chronic.parse(album["created_time"]).strftime "%d/%m/%Y %H:%M:%S" | |
# stamp_cmd = "SetFile -d '#{created_at}' -m '#{created_at}' ./data/#{slug}" | |
# puts stamp_cmd | |
# system stamp_cmd | |
photos.each do |photo| | |
path = "./data/#{slug}/#{photo['id']}.jpg" | |
break if File.exist?(path) | |
get_cmd = "wget --output-document #{path} #{photo['source']}" | |
# Set time for image | |
created_at = Chronic.parse(photo["created_time"]).strftime "%d/%m/%Y %H:%M:%S" | |
stamp_cmd = "SetFile -d '#{created_at}' -m '#{created_at}' #{path}" | |
system get_cmd | |
system stamp_cmd | |
end | |
$stdout.puts "Done.\n" | |
end | |
end | |
f = Fetcher.new(ENV["token"]).fetch |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment