Last active
October 31, 2015 19:58
-
-
Save mxbees/e16d4d0b78dd5d7c3cd2 to your computer and use it in GitHub Desktop.
this ruby script takes txt file of image post URLs and downloads the images.
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
#!/bin/ruby | |
require 'open-uri' | |
require 'tumblr_client' | |
require 'json' | |
require 'uri' | |
#this takes the txt file with the photo post URLs where you're grabbing the pics from. | |
file = ARGV | |
#you'll need to register an app with tumblr to get your oauth stuff. | |
client = Tumblr::Client.new({ | |
:consumer_key => '', | |
:consumer_secret => '', | |
:oauth_token => '', | |
:oauth_token_secret => '' | |
}) | |
#this reads my text file (from the argument above) | |
image_id = IO.readlines("#{file[0]}") | |
#this loop iterates for every line of the file | |
image_id.collect! do |x| | |
#this is to parse the URL so we can get data | |
break_up = URI::parse(x) | |
#this grabs the path (the part after the domain name) | |
path = break_up.path | |
#these two are for removing tumblr's /post/ and grabs the post id | |
first_pass = path.gsub(/\/post\//, "") | |
post_id = first_pass.gsub(/\/.*$/, "") | |
#this is to grab the hostname of the blog. | |
host = break_up.host | |
#this is the api call. since photosets can only have 10 pics, that's the limit. | |
all_data = client.posts("#{host}", :id => post_id, :type => "photo", :limit => 10) | |
#gets the post data | |
post_data = all_data.fetch("posts") | |
#hashes that data | |
post_hash = Hash[post_data[0]] | |
#grabs the value for 'photos' | |
photos = post_hash.fetch("photos") | |
#Pics from a tumblr post come in many sizes, so this is actually its own hash and this loop iterates over that hash. | |
photos.each do |i| | |
#this gets the data for the highest resolution image | |
original = i.fetch("original_size") | |
#this grabs the image URL | |
original_url = original.fetch("url") | |
#this is to write the file (eg, download the image) to your current dir. | |
File.open("#{post_id}" + ".jpg" || ".png", 'wb') do |f| | |
f.write open("#{original_url}").read | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment