Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 99 lines (87 sloc) 2.43 KB
#!/usr/bin/env ruby
#
# dropbox
#
# A hacky script by Nate Benes
#
# Based on cloudapp by Zach Holman / @holman
# (Original here: https://gist.github.com/1017937)
#
# Uploads a file from the command line to the UNL Dropbox, drops it into your clipboard
#
# Example:
#
# dropbox touchdown.png
require 'rubygems'
['rest-client', 'nokogiri', 'open-uri'].each do |gem|
begin
require gem
rescue LoadError
puts "You need to install #{gem}: gem install #{gem}"
exit!(1)
end
end
if ARGV[0].nil?
puts "You need to specify a file to upload."
exit!(1)
end
class Dropbox
class << self
def new_session
page = Nokogiri::HTML.parse(open('http://dropbox.unl.edu/cgi-bin/ajaxupload.pl'))
return page.css('form').first['action'].split('=').last
end
def upload(file, session)
response = RestClient.post("http://dropbox.unl.edu/cgi-bin/ajaxupload.pl?serial=#{session}", :uploadname => File.new(file))
page = Nokogiri::HTML.parse(response)
return page.css('textarea')[0].text.split("\n")[2].gsub('<', '').gsub('>', '').strip
end
end
end
class Copier
class << self
# Public: tests if currently running on darwin.
#
# Returns true if running on darwin (MacOS X), else false
def darwin?
!!(RUBY_PLATFORM =~ /darwin/)
end
# Public: tests if currently running on windows.
#
# Apparently Windows RUBY_PLATFORM can be 'win32' or 'mingw32'
#
# Returns true if running on windows (win32/mingw32), else false
def windows?
!!(RUBY_PLATFORM =~ /win|mingw/)
end
# Public: returns the command used to copy a url to the
# clipboard for the current platform.
#
# Returns a String with the bin
def copy_command
if darwin?
'pbcopy'
elsif windows?
'clip'
else
'xclip -selection clipboard'
end
end
# Public: copies a given url to the clipboard.
# This _should_ work in any POSIX environment and also windows,
# since they both have echo commands. I think. Maybe? I dunno.
#
# Returns nothing.
def copy(url)
system("echo #{url} | #{copy_command}")
end
end
end
# Lets upload some stuff
puts "Connecting to the UNL Dropbox..."
session = Dropbox::new_session
url = Dropbox::upload(ARGV[0], session)
# Copy it to your clipboard.
# Copier::copy(url)
# Say it for good measure.
puts "Uploaded to #{url}"