Advertisement
Guest User

tii paste

a guest
Jun 24th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.29 KB | None | 0 0
  1. #!/usr/bin/env perl
  2. use 5.014;
  3. use warnings;
  4. use FindBin qw($Bin);
  5. use lib "$Bin/lib/";
  6. use WWW::Pastebin::PastebinCom::API;
  7. use File::Spec::Functions;
  8. use Getopt::Long;
  9.  
  10. my $api_key = get_api_key();
  11. my $opt_title   = 'tii paste';
  12. my $opt_format  = 'GetText';
  13. my $opt_expire  = 'N';
  14. my $opt_help;
  15. my $opt_silent;
  16. my $program_break;
  17. my $opt_append;
  18. $SIG{INT} = sub {
  19.     print "Received Ctrl+C; will finish reading STDIN.\n";
  20.     $program_break = 1;
  21. };
  22.  
  23. my $_opt = GetOptions(
  24.   'a|append'   => \$opt_append,
  25.   'k|apikey=s' => \$api_key,
  26.   's|silent'   => \$opt_silent,
  27.   't|title=s'  => \$opt_title,
  28.   'f|format=s' => \$opt_format,
  29.   'e|expire=s' => \$opt_expire,
  30.   'h|help'     => \$opt_help,
  31. );
  32.  
  33.  
  34. unless ($api_key) {
  35.     die "Pastebin.com api key not found in ~/.pastebin neither supplied via -a\n";
  36. }
  37.  
  38. usage() if ($opt_help);
  39.  
  40. my $bin = WWW::Pastebin::PastebinCom::API->new(
  41.     api_key => "$api_key",
  42. );
  43.  
  44. my $data = '';
  45. my $O;
  46. if (defined $ARGV[0]) {
  47.     if (defined $opt_append) {
  48.         open $O, '>>', "$ARGV[0]" || die "Unable to write to output file: $ARGV[0]\n";
  49.     } else {
  50.         open $O, '>', "$ARGV[0]" || die "Unable to write to output file: $ARGV[0]\n";
  51.  
  52.     }
  53. }
  54. my $count_lines = 0;
  55. while (my $line = <STDIN>) {
  56.     last if ($program_break);
  57.     $data .= $line;
  58.     say $data if (not defined $opt_silent);
  59.     say {$O} $data if (defined $ARGV[0]);
  60.     $count_lines++;
  61. }
  62.  
  63. if ($data) {
  64.     say $bin->paste($data,
  65.         title => $opt_title,
  66.         format => $opt_format,
  67.         expire => $opt_expire,
  68.            ) || die "$bin";
  69.     say STDERR "$count_lines lines received.";
  70. } else {
  71.     say STDERR "No data received\n";
  72. }
  73.  
  74.  
  75. sub get_api_key {
  76.     my $api_file = catfile($ENV{'HOME'} , '.pastebin');
  77.     say $api_file;
  78.     my $key;
  79.     local *STDERR;
  80.     open  STDERR, '>', File::Spec->devnull() or die "could not open STDERR: $!\n";
  81.     my $readkey = eval {
  82.         open my $i, '<', $api_file;
  83.         $key = readline($i);
  84.         chomp($key);   
  85.         1;
  86.     };
  87.    
  88.     $key = 'not_found' unless ($readkey);
  89.     return $key;
  90. }
  91.  
  92. sub usage {
  93.   say STDERR<<END;
  94.  
  95.   cat file.txt | tii [options] [filename]
  96.   --------------------------------------------------------------------------
  97.   Like tee, but will create a pastebin.com entry with your text.
  98.  
  99.   Options:
  100.     -a, --append          Append to filename rather than overwriting
  101.  
  102.     -s, --silent          Will not print the stream to STDOUT
  103.  
  104.     -k, --apikey          Pastebin.com api key.
  105.                           Can be stored (alone) in the ~/.pastebin file
  106.  
  107.     -t, --title           Pastebin title [default: $opt_title]
  108.  
  109.     -f, --format          Syntax highlight like 'perl', 'bash' [default: $opt_format]
  110.  
  111.     -e, --expiry          Default N (never). Alternatives: 10M (10 minutes),
  112.                           1H (1 hour), 1D (1 day), 1W (1 week), 1M (1 month),
  113.                           6M (6 months),1Y (1 year)
  114.                          
  115. END
  116. }
  117. __END__
  118. ##### Private paste with all optional args set
  119.  
  120. $bin = WWW::Pastebin::PastebinCom::API->new(
  121.     api_key => 'a3767061e0e64fef6c266126f7e588f4',
  122. );
  123.  
  124.  
  125. $bin->paste(
  126.     'Stuff to paste',
  127.  
  128.     title => 'Title for your paste',
  129.     private => 1,       # set paste as a private paste
  130.     format => 'perl',   # Perl syntax highlighting
  131.     expiry => 'awhile', # expire the paste after 1 week
  132. ) or die "$bin";
  133.  
  134. print "$bin\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement