Last active
December 9, 2015 10:26
-
-
Save VincentSit/30ce863cd18043559ee5 to your computer and use it in GitHub Desktop.
download_instagram_images
This file contains hidden or 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/ruby | |
# download_instagram_images.rb | |
require "open-uri" | |
require "json" | |
@uid = "260732811" | |
@access_token = "277504095.59c578a.6aed864be0794247a1c0318221b03d7b" | |
@url = "https://api.instagram.com/v1/users/#{@uid}/media/recent?access_token=#{@access_token}" | |
@image_count = 0 | |
@images = [] | |
# 150x150 | |
@download_thumbnail = false | |
# 306x306 | |
@download_low_resolution = false | |
# 640x640 | |
@download_standard_resolution = true | |
def get_next_url | |
if @uid.to_s == '' || @access_token.to_s == '' | |
puts "URL Error!! #{@url}" | |
return | |
end | |
if @url.to_s == '' | |
return | |
end | |
content = open(@url).read | |
json_data = JSON.parse(content) | |
success = json_data["meta"]["code"] == 200 | |
if success | |
data = json_data["data"] | |
data.each do |dic| | |
@images << dic["images"] | |
end | |
pagination = json_data["pagination"] | |
@url = pagination["next_url"] | |
@image_count += data.count | |
puts "Current've got #{@image_count} images" | |
get_next_url | |
else | |
puts "Finish parse!!" | |
end | |
end | |
def download_images | |
if !@download_thumbnail && !@download_low_resolution && !@download_standard_resolution | |
puts "Nothing to download!" | |
return | |
end | |
if not File.exist?("./instagram") | |
Dir.mkdir("instagram") | |
end | |
@images.each do |dic| | |
puts "There are #{@image_count} images need to download..." | |
if @download_thumbnail | |
thumbnail_path = "instagram/thumbnail" | |
url = dic["thumbnail"]["url"] | |
download(url, thumbnail_path) | |
end | |
if @download_low_resolution | |
low_resolution_path = "instagram/lowResolution" | |
url = dic["low_resolution"]["url"] | |
download(url, low_resolution_path) | |
end | |
if @download_standard_resolution | |
standard_resolution_path = "instagram/standardResolution" | |
url = dic["standard_resolution"]["url"] | |
download(url, standard_resolution_path) | |
end | |
@image_count -= 1 | |
end | |
end | |
def download(image_url, to_path) | |
if image_url.to_s == '' || to_path.to_s == '' | |
puts "Parameter error!!, skip it." | |
return | |
end | |
if not File.exist?("./" + to_path) | |
Dir.mkdir(to_path) | |
end | |
filename = File.basename(String(URI.parse(image_url))) | |
begin | |
File.write(to_path + "/" + filename, open(image_url).read) | |
rescue Exception => e | |
puts e | |
end | |
end | |
def start | |
puts "Parsing...." | |
get_next_url | |
puts "Downloading..." | |
download_images | |
puts "Finish!!" | |
end | |
start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment