Skip to content

Instantly share code, notes, and snippets.

@babie
Created October 29, 2013 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save babie/7215585 to your computer and use it in GitHub Desktop.
Save babie/7215585 to your computer and use it in GitHub Desktop.
require "date"
Earthquake.init do
_ = config[:save_image] ||= {}
_[:dir] ||= File.expand_path("./image", config[:dir])
FileUtils.mkdir_p(_[:dir]) unless Dir.exist? _[:dir]
command :save_image do |m|
tweet = twitter.status(m[1])
media = tweet["entities"]["media"]
if media.blank?
puts "Image not found".c(43)
next []
end
if confirm("save images of '#{tweet["user"]["screen_name"]}: #{tweet["text"]}'")
async do
media.each do |md|
next if md["type"] != "photo" || md["display_url"] !~ /\Apic.twitter.com/
dir = config[:save_image][:dir]
dt = DateTime.now.strftime("%Y%m%d-%H%M%S")
bn = File.basename(md["media_url"])
fn = %Q|#{dir}/#{dt}-#{bn}|
open(md["media_url"], "rb") do |i|
File.open(fn, "wb") do |f|
f.write i.read
end
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment