Skip to content

Instantly share code, notes, and snippets.

@Igglybuff
Created March 14, 2015 20:35
Show Gist options
  • Save Igglybuff/fe30df25d2ad12850093 to your computer and use it in GitHub Desktop.
Save Igglybuff/fe30df25d2ad12850093 to your computer and use it in GitHub Desktop.
url-shortener
#!/usr/bin/perl -w
use strict;
use WWW::Shorten 'TinyURL';
my %DATA = (
name => 'urlpinch',
author => 'Igglybuff',
version => '0.0.1-alpha',
license => 'GPL3',
desc => 'Grabs last URL from WeeChat buffer, shortens it, then prints it to the buffer. That is all.',
);
weechat::register($DATA{"name"}, $DATA{"author"}, $DATA{"version"}, $DATA{"license"}, $DATA{"desc"}, "", "") || return;
weechat::hook_command($DATA{"name"}, $DATA{"desc"}, "Usage: /urlpinch", "", "", "urlpinch", "");
if (weechat::config_integer(weechat::config_get("logger.file.flush_delay")) != 0) {
weechat::print("", "$DATA{'name'} says: Set logger.file.flush_delay to 0 for maximum URL grabbiness. If you're not comfortable doing that, fair enough.");
}
sub urlpinch {
my $currentBuffer = weechat::current_buffer();
my $currentBufferName = weechat::buffer_get_string($currentBuffer, "localvar_name");
my $currentBufferPlugin = weechat::buffer_get_string($currentBuffer, "localvar_plugin");
my $currentBufferServer = weechat::buffer_get_string($currentBuffer, "localvar_server");
my $currentBufferChannel = weechat::buffer_get_string($currentBuffer, "localvar_channel");
my $homeDir = weechat::info_get("weechat_dir", "");
local $/;
open (FILE, "$homeDir/logs/$currentBufferPlugin.$currentBufferName.weechatlog")
or die "Cannot open logfile!";
my $chatLog = <FILE>;
close FILE;
my @urls;
while ($chatLog =~ m{(https?://.+?)(\s|"|>|$|\(|\))}gi) {
my $url = $1;
push(@urls, $url);
}
my $lastURL = $urls[-1];
my $shortURL = makeashorterlink($lastURL);
weechat::print("", "The URL \"$lastURL\" sent from $currentBufferServer, $currentBufferChannel was shortened to $shortURL successfully.");
weechat::print(weechat::current_buffer(), weechat::color("lightgreen")."$shortURL Boom. Shortened.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment