1

img_url_wget.pl:

* try to add windows support
  it's still wip as everything looks ok, but wget refuses to save file and shows cmd.exe like black window that disappears in a nick of second
This commit is contained in:
eleksir
2017-11-26 21:08:44 +03:00
parent 075bde3bb4
commit de4a53f130

View File

@@ -1,3 +1,7 @@
# one of hexchat contributors said this on freenode's #hexchat
# 2017-11-26 15:36 < TingPing> eleksir, the hexchat api isn't thread safe
# on practice it developes to memory leak
use HexChat qw(:all); use HexChat qw(:all);
use threads; use threads;
use threads::shared; use threads::shared;
@@ -7,7 +11,14 @@ use warnings "all";
use MIME::Base64; use MIME::Base64;
use URI::URL; use URI::URL;
use Image::Magick; my $IMAGEMAGICK = undef; # use on demand, perl that shipped with hexchat-2.12.4 on windows miss this module
if ($^O ne 'cygwin') { # cygwin 2.882 64-bit has broken Image::Magick
$IMAGEMAGICK = eval {
require Image::Magick;
import Image::Magick qw(ping);
return 1;
}
}
sub dlfunc($); # thread, that checks and downloads stuff sub dlfunc($); # thread, that checks and downloads stuff
sub is_picture($); sub is_picture($);
@@ -23,7 +34,7 @@ sub freehooks;
my $wgetpath; share($wgetpath); my $wgetpath; share($wgetpath);
my $script_name = "Image URL Downloader"; my $script_name = "Image URL Downloader";
HexChat::register($script_name, '0.9', 'Automatically downloads image URLs via wget', \&freehooks); HexChat::register($script_name, '0.10', 'Automatically downloads image URLs via wget', \&freehooks);
HexChat::print("$script_name loaded\n"); HexChat::print("$script_name loaded\n");
my $help = 'Usage: my $help = 'Usage:
@@ -132,22 +143,38 @@ sub dlfunc($) {
$wgetpath = '/usr/bin/wget'; $wgetpath = '/usr/bin/wget';
} elsif (-f "/usr/local/bin/wget") { } elsif (-f "/usr/local/bin/wget") {
$wgetpath = '/usr/local/bin/wget'; $wgetpath = '/usr/local/bin/wget';
} elsif (-f "c:\\perl\\bin\\wget.exe") {
$wgetpath = "c:\\perl\\bin\\wget.exe";
} elsif (-f "c:\\perl64\\bin\\wget.exe") {
$wgetpath = "c:\\perl64\\bin\\wget.exe";
} elsif (-f "$ENV{'PROGRAMFILES'}\\perl\\bin\\wget.exe") {
$wgetpath = "$ENV{'PROGRAMFILES'}\\perl\\bin\\wget.exe";
} elsif (-f "$ENV{'PROGRAMFILES'}\\perl64\\bin\\wget.exe") {
$wgetpath = "$ENV{'PROGRAMFILES'}\\perl64\\bin\\wget.exe";
} }
my $extension = is_picture($url); my $extension = is_picture($url);
if (defined($extension)) { if (defined($extension)) {
my $savepath = sprintf("%s/imgsave", HexChat::get_info("configdir")); my $savepath = sprintf("%s/imgsave", HexChat::get_info("configdir"));
if ($^O eq 'MSWin32') {
$savepath = $ENV{'USERPROFILE'} . '/Pictures';
}
mkdir ($savepath) unless (-d $savepath); mkdir ($savepath) unless (-d $savepath);
$savepath = $savepath . "/" . s/[^\w!., -#]/_/gr . ".$extension";
if ( (lc($url) =~ /\.(gif|jpeg|png|webm|mp4)$/) and ($1 eq $extension) ){ if ( (lc($url) =~ /\.(gif|jpeg|png|webm|mp4)$/) and ($1 eq $extension) ){
$savepath = HexChat::get_info("configdir") . "/imgsave/" . s/[^\w!., -#]/_/gr; $savepath = $savepath . "/" . s/[^\w!., -#\?\:]/_/gr;
} else {
$savepath = $savepath . "/" . s/[^\w!., -#\?\:]/_/gr . ".$extension";
} }
$url = urlencode($url); $url = urlencode($url);
system($wgetpath, '--no-check-certificate', '-q', '-T', '20', '-O', $savepath, '-o', '/dev/null', $url); system($wgetpath, '--no-check-certificate', '-q', '-T', '20', '-O', $savepath, '-o', '/dev/null', "$url");
if (($^O ne 'cygwin') and defined($IMAGEMAGICK)) {
eval {
if ($savepath =~ /(png|jpe?g|gif)$/i){ if ($savepath =~ /(png|jpe?g|gif)$/i){
my $im = Image::Magick->new(); my $im = Image::Magick->new();
my $rename = 1; my $rename = 1;
@@ -162,6 +189,8 @@ sub dlfunc($) {
undef $im; undef $rename; undef $im; undef $rename;
} }
}
}
undef $savepath; undef $savepath;
} }