nopaste

BooK on 2009-11-08T16:34:56

Just a quick post to show off a small and useful script I use whenever I need to "nopaste" some text or code:

    #!/usr/bin/perl -w
    use strict;
    use WWW::Mechanize;
    use Getopt::Long;
    
    my %SITE = (
        snit  => 'http://nopaste.snit.ch/',
        scsys => 'http://paste.scsys.co.uk/',
    );
    
    my %CONF = (
        channel => '',
        nick    => '',       # use your own
        summary => '',
        paste   => '',
        site    => 'snit',
        list    => '',
    );
    
    GetOptions( \%CONF, 'lang=s', 'nick=s', 'summary|desc=s', 'paste|text=s',
        'list!', 'site=s' )
        or die "Bad options";
    
    die "No such paste site: $CONF{site}\nValid choices: @{[keys %SITE]}\n"
        if !exists $SITE{ $CONF{site} };
    
    my $m = WWW::Mechanize->new;
    $m->get( $SITE{ $CONF{site} } );
    die $m->res->status_line unless $m->success;
    
    if ( $CONF{list} ) {
        print "Possible channels for $CONF{site}:\n",
            map {"- $_\n"} grep $_,
            $m->current_form()->find_input('channel')->possible_values;
        exit;
    }
    
    unless ( $CONF{paste} ) {
        $CONF{summary} ||= $ARGV[0] || '-';
        $CONF{paste} = join "", <>;
    }
    
    delete @CONF{qw( site list )};
    $m->set_fields(%CONF);
    $m->submit;
    die $m->res->status_line unless $m->success;
    
    print +( $m->links )[0]->url, "\n";

Since it works has a filter, I can call it from vim or pipe to it. It also works with a file parameter, which is used to set the paste title.

Just before posting this, I looked again on CPAN, and found the follwing:

  • App::NoPaste: Seems really complete. But does much more than I need, and I like depending only on WWW::Mechanize for such tools.

  • WWW::Rafb: Well, in an earlier version my script worked with rafb.net, but the site itself is down.

  • WWW::PasteBin: Such a huge collection of distributions, I wouldn't which to install first.

  • WebService::NoPaste: Found it years ago when looking for a nopaste utility, but I preferred to write my own.

Clearly, there is no lack of modules to nopaste stuff, so I'm not going to add my own to the list. :-)

I tried publishing scripts on CPAN, but I feel the toolchain is not really targetting scripts, anyway. Anyway, I have a few utility scripts like the above lying around, and I'm thinking maybe the best way to go with them nowadays is to just publish them on GitHub. Might happen someday.