Advertisement
Guest User

tii paste

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