Skip to content

Instantly share code, notes, and snippets.

@Ninjex
Last active August 29, 2015 13:57
Show Gist options
  • Save Ninjex/9527296 to your computer and use it in GitHub Desktop.
Save Ninjex/9527296 to your computer and use it in GitHub Desktop.
A Weechat plugin for shortening long URL's. This script uses the URL shortening application at hts.io
#!/usr/bin/ruby
# Need to add a validation check on URL's -- just don't be ignorant with it :D
require 'mechanize' # Necessary to communicate with the server.
def weechat_init
Weechat.register('short_url', 'Ninjex', '1.0', 'GPL3', 'Shorten a URL /short_url <link>', '', '')
Weechat.hook_command('short_url', 'Shorten URL /short_url <link>', '', '', '', 'short_url', '')
return Weechat::WEECHAT_RC_OK
end
def short_url(data, buffer, args)
buffer = Weechat.current_buffer
client = Mechanize.new {|agent| agent.user_agent = 'Short URL -- Ninjex'} # Create a scraping client with a custom user agent.
client.request_headers = {'Referer' => 'https://www.hackthissite.org'} # Set the referer for hackthissite.
client.get('http://hts.io/') do |page| # Connect the client to the home page of hts.
shorten_form = page.form_with(:action => '') do |f| # Load the information of the login form.
f.url = args
end.click_button # Click the form button to login with the specified credentials. (Too lazy to handle error outputs)
form_result = shorten_form.body
form_result.each_line do |line|
@short_url = line.split('"')[1] if line.include?('Short URL:')
end
Weechat.command(buffer, "#{@short_url} (Generated via hts.io")
return Weechat::WEECHAT_RC_OK
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment