File: url_shorten.rb

package info (click to toggle)
weechat-scripts 20100422-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,184 kB
  • ctags: 1,634
  • sloc: python: 8,545; perl: 8,472; ruby: 147; tcl: 137; sh: 8; makefile: 2
file content (212 lines) | stat: -rw-r--r-- 6,926 bytes parent folder | download
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# Copyright (c) 2008, Daniel Bretoi <daniel@bretoi.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY Daniel Bretoi ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# * Simpler, more robust version of tinyurl (doesn't rely on html output)
# * Echo's urls from all channels that are over the plugin's maxlen value
#     in a shortened format.
# * allows for manual shortening of urls
# * set config variable 'shortener' to one of:
#
#	Value		Service used
#	-----		------------
#	qurl	http://qurl.com/
#	tinyurl	http://tinyurl.com/
#	isgd	http://is.gd/
#	trim	http://tr.im/
#	bitly	http://bit.ly/
#
#   Note: attempting to use the bitly shortener without setting the
#	  bitly_login and bitly_key config variables will fail.
#
#   Note: 'trim' and 'bitly' shorteners require the 'rubygems' and
#	  'json' ruby modules.
#
# 2009-12-12, FlashCode <flashcode@flashtux.org>
#     version 1.5: fix wrong license in register()
# 2009-11-29, penryu <penryu@gmail.com>
#     version 1.4: fixed URI encoding bug, added shorteners
#		   changed default shortener, as qurl is _slow_
# 2009-11-29, penryu <penryu@gmail.com>
#     version 1.3: add bit.ly shortener routine
#		   add ability to choose shortener from config
# 2009-05-02, FlashCode <flashcode@flashtux.org>:
#     version 1.2: sync with last API changes
# 2008-11-11, FlashCode <flashcode@flashtux.org>:
#     version 1.1: conversion to WeeChat 0.3.0+

require 'net/http'
require 'uri'

SCRIPT_NAME = 'url_shorten'
SCRIPT_AUTHOR = 'Daniel Bretoi <daniel@bretoi.com>'
SCRIPT_DESC = 'Shorten url'
SCRIPT_VERSION = '1.5'
SCRIPT_LICENSE = 'BSD'

DEFAULTS = {
  'maxlen'      => '50',
  'color'       => 'red',
  'shortener'   => 'tinyurl',
  'bitly_login' => '',
  'bitly_key'   => '',
}


def weechat_init
  Weechat.register SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, "", ""
  Weechat.hook_command SCRIPT_NAME, SCRIPT_DESC, "url", "url: url to shorten", "", SCRIPT_NAME, ""
  #Weechat.hook_signal "*,irc_in2_privmsg", "msg_shorten", ""
  Weechat.hook_print "", "", "://", 1, "msg_shorten", ""
  DEFAULTS.each_pair { |option, def_value|
    cur_value = Weechat.config_get_plugin(option)
    if cur_value.nil? || cur_value.empty?
      Weechat.config_set_plugin(option, def_value)
    end
  }

  if Weechat.config_get_plugin("shortener").eql?('bitly')
    cfg_bitly_login = Weechat.config_get_plugin("bitly_login")
    cfg_bitly_key   = Weechat.config_get_plugin("bitly_key")
    if cfg_bitly_login.empty? || cfg_bitly_key.empty?
      yellow = Weechat.color("yellow")
      Weechat.print("", "#{yellow}WARNING: The bit.ly shortener requires a valid API login and key.")
      Weechat.print("", "#{yellow}WARNING: Please configure the `bitly_login' and `bitly_key' options before using this script.")
    end
  end

  return Weechat::WEECHAT_RC_OK
end

def fetch(uri_str, limit = 10)
  raise ArgumentError, 'HTTP redirect too deep' if limit == 0
  
  response = Net::HTTP.get_response(URI.parse(uri_str))
  case response
  when Net::HTTPSuccess     then response.body
  when Net::HTTPRedirection then fetch(response['location'], limit - 1)
  else
    response.error!
  end
end

def qurl_shorten(url)
  fetch('http://www.qurl.com/automate.php?url=' + URI.encode(url)).gsub('www.','')
end

def tinyurl_shorten(url)
  fetch('http://tinyurl.com/api-create.php?url=' + URI.encode(url))
end

def isgd_shorten(url)
  fetch('http://is.gd/api.php?longurl=' + URI.encode(url))
end

def trim_shorten(url)
  require 'rubygems'
  require 'json'

  params = ['url=' + URI.encode(url)]
  params << 'newtrim=1'
  #params << 'sandbox=1'  # comment out for release
  json = fetch('http://api.tr.im/v1/trim_url.json?' + params.join('&'))
  data = JSON.parse(json)

  if data['status']['result'].eql?('OK')
    begin
      data['url']
    rescue NoMethodError => ex
      "Failure parsing tr.im result: #{ex}"
    end
  else
    data['status']['message']
  end
end

def bitly_shorten(url)
  require 'rubygems'
  require 'json'

  params = ['longUrl=' + URI.encode(url)]
  params << 'login=' + Weechat.config_get_plugin('bitly_login')
  params << 'apiKey=' + Weechat.config_get_plugin('bitly_key')
  api_url = 'http://api.bit.ly/shorten?version=2.0.1&' + params.join('&')

  begin
    url_data = JSON.parse(fetch(api_url))
  rescue Exception => ex
    return "Failure shortening url: " + ex.to_s
  end

  if url_data['statusCode'].eql?('OK')
    begin
      res = url_data['results']
      res[res.keys[0]]['shortUrl']
    rescue NoMethodError => ex
      "Failure parsing bitly result: #{ex}"
    end
  else
    url_data['errorMessage']
  end
end

def shortener(url)
  service = Weechat.config_get_plugin('shortener').tr('.','')
  begin
    send("#{service}_shorten", url)
  rescue NoMethodError => e
    "Shortening service #{service} not supported."
  end
end

def regexp_url
  @regexp_url ||= Regexp.new('https?://[^\s,>]*')
  @regexp_url
end

def url_shorten(data,buffer,msg)
  if (msg.empty?)
    return Weechat::WEECHAT_RC_OK
  end
  url = (msg.scan regexp_url).to_s
  short = shortener(url)
  color = Weechat.color(Weechat.config_get_plugin("color"))
  Weechat.print(Weechat.current_buffer, "[url]\t#{color}#{short}");
  return Weechat::WEECHAT_RC_OK
end

def msg_shorten(data,buffer,time,tags,displayed,highlight,prefix,message)
  if (message.empty?)
    return Weechat::WEECHAT_RC_OK
  end

  matchdata = message.match(regexp_url)
  return Weechat::WEECHAT_RC_OK unless matchdata

  url = matchdata[0].to_s
  maxlen = Weechat.config_get_plugin("maxlen").to_i
  return Weechat::WEECHAT_RC_OK if url.length < maxlen

  short = shortener(url)
  color = Weechat.color(Weechat.config_get_plugin("color"))
  Weechat.print(buffer, "[url]\t%s%s" % [color, short])
  return Weechat::WEECHAT_RC_OK
end