Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

hup - home node updater

by ar0n (Priest)
on Jul 21, 2001 at 18:46 UTC ( #98664=sourcecode: print w/ replies, xml ) Need Help??

Category: PerlMonks.org Related Scripts
Author/Contact Info /msg ar0n
Description:

hup is a home node updater. You can use it on the command line, or from a crontab, to automate updating your homenode. As always, suggestions welcome.

Updates

  • 2001-11-14 -- Added scratchpad support, courtesy of thatguy.
  • 2002-01-19 -- Miscellaneous fixes. No longer warns when certain fields are empty; prompts for username and password

#!/usr/bin/perl -w

use strict;

use Term::ReadPassword;

use HTTP::Request::Common 'POST';
use HTTP::Cookies;
use LWP::UserAgent;

use HTML::TokeParser;

use Getopt::Long;

use constant TINYNODE => 16046;
use constant PM => 'http://www.perlmonks.org/index.pl';

$| = 1;

{
  my ($ua, $req);

  my ($homenode, $username, $password, $file);
  my ($realname, $email, $image, $location);
  my ($verbose, $significant);
  my ($setpublic, $scratchfile, $scratchpad);

  GetOptions(
    "username=s"    => \$username,
    "password=s"    => \$password,

    "name=s"        => \$realname,
    "email=s"       => \$email,
    "image=s"       => \$image,
    "location=s"    => \$location,

    "homenode=s"    => \$file,

    "contentofpad=s"=> \$scratchfile,
    "makepublic"    => \$setpublic,

    "significant"   => \$significant,
    "verbose"       => \$verbose
  );

  unless ( defined $username ) {
    $username = "";
    until ( $username =~ /\S/ ) {
      print "Username: ";
      chomp($username = <STDIN>);
    }
  }

  unless ( defined $password ) {
    $password = "";
    until ( $password =~ /\S/ ) {
      $password = read_password("Password: ")
    }
  }

  die usage()
    unless $username && $password;;

  die "Can't find $file\n"
    if defined $file and not -f $file;

    $homenode = join "", do { local @ARGV = $file; <> } if defined $fi
+le;

  die "Can't find $scratchfile\n"
    if defined $scratchfile and not -f $scratchfile;

    $scratchpad = join "", do { local @ARGV = $scratchfile; <> } if de
+fined $scratchfile;

  die "Can't find image $image\n"
    if defined $image and not -f $image;

  $ua = new LWP::UserAgent( agent => "Homenode Updater" );
  $ua->cookie_jar( HTTP::Cookies->new() );

  print "Logging in... "
    if $verbose;

  die "Couldn't log in. Is your username/password right?\n"
    unless $ua->request(
      POST PM, [
        node_id => TINYNODE,
        op      => 'login',
        user    => $username,
        passwd  => $password
      ]
    )->as_string() =~ /userpass/;

  print "done.\n"
    if $verbose;

  print "Fetching... "
    if $verbose;

  my $html = $ua->request(
    POST PM, [
      displaytype => "edit",
      node        => $username
    ]
  )->content();

  print "done.\n"
    if $verbose;

  print "Parsing... "
    if $verbose;

  my %default = parse($html);

  print "done.\n"
    if $verbose;

  print "Updating... "
    if $verbose;

  my $content = [
      displaytype            => "edit",
      node                   => $username,

      user_realname          => $realname || $default{user_realname} |
+| "",
      user_passwd1           => $password,
      user_passwd2           => $password,
      user_email             => $email || $default{user_email} || "",
      user_doctext           => (defined $homenode ? $homenode : $defa
+ult{user_doctext}) || "",

      user_scratchpad        => (defined $scratchpad ? $scratchpad : $
+default{user_scratchpad}) || "",
      setscratchpublic       => ($setpublic ? "on" : $default{setscrat
+chpublic}) || "",

      setsetting_location    => $location || $default{setsetting_locat
+ion} || "",
      significantupdate      => $significant ? "on" : "",

      sexisgood              => "stumbit"
  ];
  push @$content, imgsrc_file => [$image]
    if defined $image;

  $ua->request(
    POST PM,
      Content_Type => 'form-data',
      Content => $content
  );

  print "done.\n"
    if $verbose;
}

sub usage
{
  return <<"EOT";
Usage: $0 -u username -p password [ options ]
  Options:
    -i <file>   Home node picture
    -h <file>   File containing home node contents

    -c <file>   File containing scratchpad contents
    -m          Make scratchpad public

    -n <string> Your real name
    -e <string> Your email address
    -l <string> Your location

    -s          Significant update
    -v          Be a little verbose
EOT
}

sub parse
{
  my $html = shift;

  my %default;
  my @params = qw( user_realname user_email user_passwd1 user_passwd2 
+setsetting_location );

  my $p = new HTML::TokeParser ( \$html );

  while ( my $token = $p->get_tag("textarea") ) {
    $default{user_doctext} = $p->get_text if $token->[1]{name} eq "use
+r_doctext";
    $default{user_scratchpad} = $p->get_text if $token->[1]{name} eq "
+user_scratchpad";
  }

  while ( my $token = $p->get_tag("input") ) {
    next unless exists $token->[1]{name};
    $default{ $token->[1]{name} } = $token->[1]{value} || "" if grep {
+ $_ eq $token->[1]{name} } @params;
    $default{setscratchpublic} = "on" if $token->[1]{name} eq 'setscra
+tchpublic' && $token->[1]{checked};
  }

  return %default;
}

Comment on hup - home node updater
Download Code
Re: hup - home node updater (with scratchpad ability)
by thatguy (Parson) on Sep 20, 2001 at 21:59 UTC
    Since I use it so much, I thought I would add scratchpad capability.

    added options [ -c | --contentsofpad ] and [ -m | --makepublic ]

    #!/usr/bin/perl use strict; use HTTP::Request::Common 'POST'; use HTTP::Cookies; use LWP::UserAgent; use HTML::TokeParser; use Getopt::Long; use constant TINYNODE => 16046; use constant PM => 'http://www.perlmonks.org/index.pl'; $| = 1; { my ($ua, $req); my ($homenode, $username, $password, $file); my ($realname, $email, $image, $location); my ($verbose, $significant); # added for scratchpad my ($setpublic,$scratchfile,$scratchpad); GetOptions( "username=s" => \$username, "password=s" => \$password, "name=s" => \$realname, "email=s" => \$email, "image=s" => \$image, "location=s" => \$location, "homenode=s" => \$file, # added for scratchpad "contentofpad=s"=> \$scratchfile, "makepublic" => \$setpublic, "significant" => \$significant, "verbose" => \$verbose ); die usage() unless $username && $password; die "Can't find $file\n" if defined $file and not -f $file; # added for scratchpad die "Can't find $scratchfile\n" if defined $scratchfile and not -f + $scratchfile; # added for scratchpad $scratchpad = join "", do { local @ARGV = $scratchfile; <> } if de +fined $scratchfile; $homenode = join "", do { local @ARGV = $file; <> } if defined $fi +le; die "Can't find image $image\n" if defined $image and not -f $imag +e; $ua = new LWP::UserAgent("Homenode Updater"); $ua->cookie_jar( HTTP::Cookies->new() ); print "Logging in...\n" if $verbose; $req = POST PM, [ node_id => TINYNODE, op => 'login', user => $username, passwd => $password ]; die "Couldn't log in. Is your username/password right? $!\n" unless $ua->request($req)->as_string() =~ /userpass/; print "done.\n" if $verbose; $req = POST PM, [ displaytype => "edit", node => $username ]; my $html = $ua->request($req)->content(); print "Parsing... " if $verbose; my %default = parse($html); print "done.\n" if $verbose; print "Updating... " if $verbose; $req = POST PM, Content_Type => 'form-data', Content => [ displaytype => "edit", node => $username, imgsrc_file => $image ? [$image] : "", user_realname => $realname || $default{user_realn +ame}, user_passwd1 => $password, user_passwd2 => $password, user_email => $email || $default{user_email}, user_doctext => defined $homenode ? $homenode : +$default{user_doctext}, # added for scratchpad user_scratchpad => defined $scratchpad ? $scratc +hpad : $default{user_scratchpad}, setscratchpublic => $setpublic ? "on" : "", setsetting_location => $location || $default{setsetting +_location}, significantupdate => $significant ? "on" : "", sexisgood => "stumbit" ]; $ua->request($req); print "done.\n" if $verbose; } sub usage { return <<"EOT"; Usage: $0 -u username -p password [ options ] Options: -i <file> Home node picture -h <file> File containing home node contents -c <file> File containing scratchpad contents -m Set Scratchpad public -n <string> Your real name -e <string> Your email address -l <string> Location -s Significant update -v Be a little verbose EOT } sub parse { my $html = shift; my %default; my @params = qw( user_realname user_email user_passwd1 user_passwd +2 setsetting_location ); my $p = new HTML::TokeParser ( \$html ); if ( my $token = $p->get_tag("textarea") ) { $default{user_doctext} = $p->get_text if $token->[1]{name} eq +"user_doctext"; # added for scratchpad $default{user_scratchpad} = $p->get_text if $token->[1]{name} +eq "user_scratchpad"; } while ( my $token = $p->get_tag("input") ) { next unless exists $token->[1]{name}; $default{ $token->[1]{name} } = $token->[1]{value} if grep { $ +_ eq $token->[1]{name} } @params; } return %default; } =head1 NAME hup =head1 DESCRIPTION hup is a simple tool to update your home node. It's basically a command-line interface to the "Edit your user information" link on one's homenode. hup allows you to change anything on that page, except the password. You can use the following switches (of which -u and -p are mandatory): =over 4 =item C<-u> or C<--username> Your Perl Monks username =item C<-p> or C<--password> Your Perl Monks password =item C<-i> or C<--image> The location of your home node picture =item C<-h> or C<--homenode> The location of the file containing home node contents =item C<-n> or C<--name> Your real name =item C<-e> or C<--email> Your email address =item C<-c> or C<--contentofpad> The location of the file containing scratch pad contents =item C<-m> or C<--makepublic> Make scratchpad public =item C<-l> or C<--location> Your Location =item C<-s> or C<--significant> Use this switch to flag it as a significant update =item C<-v> or C<--verbose> Be a little verbose =back =head1 EXAMPLES Update your home node picture: hup -u mynick -p mypassword -i /path/to/image.jpg Update your homenode contents and location and flag it as a significan +t update, and be verbose: hup -u mynick -p mypassword -h /etc/passwd -l "Foo Bar" -s -v =cut

    -p

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://98664]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2014-07-24 03:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (156 votes), past polls