Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Download all the photos you've liked on instagram
require 'restclient'
require 'json'
require 'digest'
class IGDownloader
def initialize(output_path)
@base_output_path = output_path
end
def run(media_url)
image_urls = []
while media_url != nil
json = json_for(media_url)
media_url = json["pagination"]["next_url"]
image_urls.concat(find_urls(json["data"]))
# don't slam instagram's api
sleep 0.2
end
download_images(image_urls)
end
def download_images(urls)
urls.each do |url|
digest = digest(url)
image_output_path = image_output_path(digest)
image = RestClient.get(url) unless File.exists? image_output_path
save_image(image, image_output_path) if image
save_url(url, url_output_path(digest)) if image
end
end
def output_directory(digest)
path = "#{@base_output_path}/#{digest}"
dir = Dir.mkdir(path) unless Dir.exists? path
path
end
def digest(url)
Digest::MD5.hexdigest(url)
end
def image_output_path(digest)
path = output_directory(digest)
full_path = "#{path}/#{digest}.jpg"
end
def url_output_path(digest)
path = output_directory(digest)
full_path = "#{path}/url.txt"
end
def save_image(image, path)
save_item(image, path)
end
def save_url(url, path)
save_item("#{url}\n", path)
end
def save_item(item, path)
File.open(path, 'w') {|f| f.write(item) }
end
def json_for(url)
begin
response = RestClient.get url
json = JSON.parse(response)
rescue Exception
puts "Rescuing #{url}"
# yeah, infinite loop, be careful
json_for(url)
end
end
def find_urls(items)
items.map do |item|
url = item["images"]["standard_resolution"]["url"]
end
end
end
# See http://instagram.com/developer/authentication/
# for authentication details
token = "some valid access token"
media_url = "https://api.instagram.com/v1/users/self/media/liked/?access_token=#{token}"
output_path = "/some/path/that/already/exists"
IGDownloader.new(output_path).run(media_url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment