Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
107 lines (99 sloc) 3.77 KB
#!/usr/bin/ruby
# Dropzone Destination Info
# Name: LinkSmasher
# Description: Drop text/url, or click to scan clipboard. If multiple links are found, they'll be collected into one url with linkbun.ch, a single url will be shortened with bit.ly.
# Handles: NSStringPboardType
# Creator: Brett Terpstra, w/code by Sergej Müller
# URL: http://brettterpstra.com
# IconURL: http://abyss.designheresy.com/LinkSmash.png
# OptionsNIB: Login
# LoginTitle: bit.ly Login Details
require 'open-uri'
require 'net/http'
require 'cgi'
require 'rexml/document'
$KCODE = 'u'
def get_bitly_link(text)
if text =~ /(https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w\/\-_\.]*(\?\S+)?)?)?)/
$dz.begin("Getting Bit.ly link")
url = $1
Net::HTTP.start('api.bit.ly') do |http|
req = Net::HTTP::Get.new('/shorten?version=2.0.1&format=xml&history=1&longUrl=' + CGI::escape(url))
req.basic_auth ENV['USERNAME'], ENV['PASSWORD']
res = http.request(req)
doc = REXML::Document.new(res.body)
doc.elements.each("bitly/statusCode") do |a|
if a.text == "ERROR"
%x{/usr/local/bin/growlnotify -a "Dropzone.app" -m "Please check your username and password in the settings" -t "Username/password error"} if File.exists?("/usr/local/bin/growlnotify")
$dz.finish("Invalid user or password")
$dz.url(false)
else
doc.elements.each("bitly/results/nodeKeyVal/shortUrl") do |b|
if b.text.empty?
%x{/usr/local/bin/growlnotify -a "Dropzone.app" -m "We ran into a problem, sorry!" -t "Empty URL returned"} if File.exists?("/usr/local/bin/growlnotify")
$dz.finish("Empty URL returned")
$dz.url(false)
else
%x{/usr/local/bin/growlnotify -a "Dropzone.app" -m "Copied #{b.text} to clipboard" -t "Bit.ly url in clipboard"} if File.exists?("/usr/local/bin/growlnotify")
$dz.finish("Copied short URL")
$dz.url(b.text)
end
end
end
end
end
else
$dz.finish("Invalid URL")
$dz.url(false)
end
end
def bunch_links(text)
$dz.begin("Bunching links")
links = text.scan /https?:\/\/(?:[-\w\.]+)+(?::\d+)?(?:\/(?:[\w\/\-_\.]*(?:\?\S+)?)?)?/m
unless links.empty?
linkbunch = CGI.escape(links.join("\n"))
url = "http://linkbun.ch/linkbunch.php?links=#{linkbunch}&bunch=Bunch&mode=api"
res = Net::HTTP.get_response(URI.parse(url))
if res.code.to_i == 200
%x{/usr/local/bin/growlnotify -a "Dropzone.app" -m "#{links.length} links bunched" -t "Linkbunch in Clipboard"} if File.exists?("/usr/local/bin/growlnotify")
$dz.finish("#{links.length} links bunched")
$dz.url(res.body)
else
%x{/usr/local/bin/growlnotify -a "Dropzone.app" -m "Now you've done it'" -t "Linkbunch returned an error"} if File.exists?("/usr/local/bin/growlnotify")
$dz.finish("Linkbunch returned an error")
$dz.url(false)
end
else
%x{/usr/local/bin/growlnotify -a "Dropzone.app" -m "Drop or copy something with links in it." -t "No links detected"} if File.exists?("/usr/local/bin/growlnotify")
$dz.finish("No links detected")
$dz.url(false)
end
end
def dragged
$dz.determinate(false)
$dz.begin("Checking links")
count = $items[0].scan(/(https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w\/\-_\.]*(\?\S+)?)?)?)/m).length
if count > 1
bunch_links($items[0])
elsif count == 1
get_bitly_link($items[0])
else
$dz.finish("No links detected")
$dz.url(false)
end
end
def clicked
$dz.determinate(false)
$dz.begin("Scanning clipboard for links")
`pbpaste|pbcopy`
text = %x{__CF_USER_TEXT_ENCODING=$UID:0x8000100:0x8000100 pbpaste}.strip
count = text.scan(/(https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w\/\-_\.]*(\?\S+)?)?)?)/m).length
if count == 1
get_bitly_link(text)
elsif count > 1
bunch_links(text)
else
$dz.finish("No links detected")
$dz.url(false)
end
end