forked from a4agarwal/dropzone-user-scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bit.ly.dropzone
52 lines (47 loc) · 1.29 KB
/
bit.ly.dropzone
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Dropzone Destination Info
# Name: bit.ly
# Description: A dropped URL will be converted to a Bit.ly URL.
# Handles: NSStringPboardType
# Creator: Sergej Müller
# URL: http://ebiene.de
# IconURL: http://aptonic.com/destinations/icons/bit.ly.png
# OptionsNIB: Login
# LoginTitle: Bit.ly Login Details
require 'open-uri'
require 'net/http'
require 'cgi'
require 'rexml/document'
def dragged
$dz.determinate(false)
$dz.begin("Getting Bit.ly URL")
if $items[0] =~ /http/
Net::HTTP.start('api.j.mp') do |http|
req = Net::HTTP::Get.new('/shorten?version=2.0.1&format=xml&history=1&longUrl=' + CGI::escape($items[0]))
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"
$dz.finish("Invalid user or password")
$dz.url(false)
else
doc.elements.each("bitly/results/nodeKeyVal/shortUrl") do |b|
if b.text.empty?
$dz.finish("Empty URL is returned")
$dz.url(false)
else
$dz.finish("URL is now on clipboard")
$dz.url(b.text)
end
end
end
end
end
else
$dz.finish("Invalid URL")
$dz.url(false)
end
end
def clicked
system("open http://j.mp")
end