forked from a4agarwal/dropzone-user-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbit.ly.dropzone
executable file
·54 lines (47 loc) · 1.48 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
53
54
# Dropzone Destination Info
# Name: bit.ly
# Description: A dropped URL will be converted to a Bit.ly URL. Password is your API key which can be found at http://bit.ly/a/your_api_key
# 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 'cgi'
require 'rexml/document'
def dragged
$dz.determinate(false)
$dz.begin("Getting Bit.ly URL")
if $items[0] =~ /http/
username = ENV['USERNAME']
apikey = ENV['PASSWORD']
url = CGI::escape($items[0])
version = "2.0.1"
bitly_url = "http://api.bit.ly/shorten?version=#{version}&format=xml&longUrl=#{url}&login=#{username}&apiKey=#{apikey}"
# parse result and return shortened url
buffer = open(bitly_url, "UserAgent" => "Ruby-ExpandLink").read
doc = REXML::Document.new(buffer)
doc.elements.each("bitly/errorMessage") do |a|
if a.text == "INVALID_LOGIN"
$dz.finish("Invalid user or API key")
$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
else
$dz.finish("Invalid URL")
$dz.url(false)
end
end
def clicked
system("open http://bit.ly")
end