forked from a4agarwal/dropzone-user-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkSmasher.dropzone
107 lines (99 loc) · 3.77 KB
/
LinkSmasher.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/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