Skip to content

Instantly share code, notes, and snippets.

@nudded
Created May 10, 2010 16:25
Show Gist options
  • Save nudded/396234 to your computer and use it in GitHub Desktop.
Save nudded/396234 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require "net/http"
require "uri"
require "json"
require "base64"
class Imgur
API_KEY = "15a6efd29935f62ea87579a8325d945a"
def self.upload_file(file_name)
self.upload(File.read(file_name))
end
def self.upload(file_data)
url = URI::parse 'http://imgur.com/'
img = Base64.encode64 file_data
req = Net::HTTP::Post.new('/api/upload.json')
req.set_form_data :key => API_KEY , :image => img
response,body = Net::HTTP::start(url.host,url.port) do |http|
http.request(req)
end
puts JSON[body]['rsp']['image']['imgur_page']
end
end
if __FILE__ == $0
if ARGV.first == nil
begin
data = ''
while line = gets
data << line
end
Imgur.upload data unless data.empty?
rescue Exception => e
end
else
Imgur.upload_file ARGV.first
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment