Skip to content

Instantly share code, notes, and snippets.

@sheeit
Created December 16, 2020 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sheeit/78641aa88deea13dcb68c30e40ec0497 to your computer and use it in GitHub Desktop.
Save sheeit/78641aa88deea13dcb68c30e40ec0497 to your computer and use it in GitHub Desktop.
Download from IPFS
#!/usr/bin/env perl
use strict;
use warnings ( FATAL => 'all' );
use feature 'say';
use autodie;
use utf8;
use Carp;
use Readonly;
use English '-no_match_vars';
Readonly our $VERSION => '0.0.1';
use LWP::Simple qw(mirror is_success);
sub get_clipboard {
require IPC::Cmd; # qw(can_run run run_forked);
my %commands = (
xsel => [qw(-b -o)],
xclip => [qw(-selection clipboard -out)],
);
my $method;
CLIPBOARD_GETTER:
while ( my $name = each %commands ) {
my $tmp = IPC::Cmd::can_run($name);
if ($tmp) {
$method = $name;
unshift @{ $commands{$name} }, $tmp;
last CLIPBOARD_GETTER;
}
}
$method
or croak(q(I couldn't find any method to get the clipboard's contents));
my ( $ok, $err, undef, $stdout, $stderr ) = IPC::Cmd::run(
command => $commands{$method},
verbose => 0,
timeout => 3
);
$ok and return join q() => @{$stdout};
croak("Clipboard command failed: $err: @{$stderr}");
}
sub highlight_line {
my @lines = @_;
require Term::ANSIColor;
Readonly my $COLOR => Term::ANSIColor::color('bright_black');
Readonly my $RESET => Term::ANSIColor::color('reset');
Readonly my @DELIMS => ( q(<<< ), q( >>>) );
my $colorize = sub {
my $s = shift;
return $COLOR . $s . $RESET;
};
return join "\n" => map {
join $_ => map { $colorize->($_) }
@DELIMS
} @lines;
}
my $url = get_clipboard();
say "URL: ${\highlight_line($url)}" or croak("say failed: $OS_ERROR");
( my $name = $url ) =~ s{
\A .+ [?] filename = (.+?) \z
}{
(my $r = $1) =~ s/[%]([[:xdigit:]]{2})/chr hex $1/egomsx; $r;
}eomsx;
say "name: ${\highlight_line($name)}" or croak("say failed: $OS_ERROR");
my $code = mirror( $url, $name );
if ( !is_success($code) ) {
croak("LWP::Simple::mirror($url, $name) failed: $code");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment