Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
#!/usr/local/bin/perl -w # # Invoke with './ [-u username] [-p password]' # # Alternatively, username and/or password can be embedded into the sc +ript, if you don't want # command line arguments. # # Displays a users total writeups, total reputation, along with min, +max, and average. Only # works for your own account, since reps are 'proprietary' # # Requires: # HTML::TableExtract # LWP::Simple # # Copyright 2000(c) J.C.Wren # No rights reserved, use as you see fit. I'd like to know about it, + though, just for kicks. # # This module has more code than is actually necessary for the functi +onality provided. # I was originally writing a module for another function, and this ca +me out of it. The # @articles array that is returned from get_article_list() contains a +n array reference # that has the articles name, prefixed by the string 'node_id=xxx:', +where 'xxx' is the # number the article title refers to, the reputation of the article, +and the date the # article was written. I'm sure you can imagine some uses for this.. +. # use strict; use LWP::Simple; use Getopt::Std; my $def_username = ""; # Set this to your user name if you don't want + to use the -u option my $def_password = ""; # Set this to your pass word if you don't want + to use the -p option my $pmsite = ""; { my %args = (); getopts ('u:p:', \%args); my $username = $args{u} || $def_username; my $password = $args{p} || $def_password; die "No password and/or username. Program terminated.\n" if (!$use +rname || !$password); show_reps ($username, $password); } sub show_reps { @_ == 2 or die "Incorrect number of parameters"; my ($username, $password) = @_; my $total = 0; my $rarticles = get_article_list ($username, $password); die "You have no articles, perhaps?\n" unless ($#$rarticles >= 0); $total += @$_[1] for (@$rarticles); @$rarticles = sort {@$a[1] <=> @$b[1]} @$rarticles; print "\n"; print sprintf (" User: %s\n", $username); print sprintf (" Total articles: %d\n", $#$rarticles + 1); print sprintf (" Total reputation: %d\n", $total); print sprintf (" Min reputation: %d\n", @$rarticles [0]->[1]) +; print sprintf (" Max reputation: %d\n", @$rarticles [-1]->[1] +); print sprintf ("Average reputation: %3.2f\n", $total / ($#$rarticle +s + 1)); print "\n"; } sub get_article_list { @_ == 2 or die "Incorrect number of parameters"; my ($username, $password) = @_; my @articles = (); $LWP::Simple::FULL_LWP = 1; for (my $i = 0; 1; $i += 50) { my $url = "$pmsite?user=$username&passwd=$password&op=login&node +=perl+monks+user+search&usersearch=$username&orderby=createtime%20des +c&count=$i"; if (my $page = get ($url)) { last if (get_article_page ($page, ['Writeup', 'Rep', 'Create +Time'], \@articles, $i % 50) < 50); } else { die "Get on $url failed."; } } return (\@articles); } sub get_article_page { @_ == 4 or die "Incorrect number of parameters"; my ($html, $tablecols, $rarticles, $lines) = @_; my $rowcnt = 0; $html =~ s/bgcolor=>/bgcolor="">/mg; my $te = new jcwExtract (headers => $tablecols)->parse ($html); die sprintf ("Wrong number of tables (%d) returned! (Probably bad u +sername/password)\n", scalar $te->table_states) if (scalar $te->table +_states != 1) ; foreach my $ts ($te->table_states) { foreach ($ts->rows) { last if (@$_[2] !~ /\d+-\d+\d+/); push @$rarticles, [ @$_ ]; $rowcnt++; } } return ($rowcnt); } BEGIN { # # This is not good code. It's really evil and the both the author + of this package, and the author # of HTML::TableExtract should be severely beaten about the head a +nd shoulders. # package jcwExtract; use strict; use HTML::TableExtract; @jcwExtract::ISA = qw(HTML::TableExtract HTML::TableExtract::TableS +tate); my $node_id = undef; { local $^W = 0; # # Override the _add_text mode that if $node_id is defined, we'l +l insert the node_id # value at the front of the string. # eval 'sub HTML::TableExtract::TableState::_add_text { my ($self, $txt, $skew_column) = @_; defined $txt or return; my $row = $self->{content}[$#{$self->{content}}]; $txt = sprintf ("node_id=%d:%s", $node_id, $txt) if define +d ($node_id); $node_id = undef; $row->[$skew_column] .= $txt; $txt; }'; } # # Overridden start method, so we can look for <A HREF=...> tags # sub start { my $self = shift; my ($tag, $attr, $attrseq, $origtext) = @_; $self->SUPER::start (@_); # # If it's a <A HREF=...> tag, and has a node_id, then $1 will c +ontain the node_id. # If we're in a table cell, set $node_id to $1, otherwise undef + it. We don't simply # set $node_id to undef if it's not a <A> tag, because we want +to save the last value # if there is a subsequent <B> or <i> or somesuch tag between t +he <A> and </A> tags. # if ($tag eq 'a' && defined ($attr->{'href'}) && $attr->{'href'} +=~ /\bnode_id=(\d+)/i) { $node_id = ($self->_current_table_state->{in_cell}) ? $1 : un +def; } } 1; }

In reply to by jcwren

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (1)
As of 2023-05-28 06:20 GMT
Find Nodes?
    Voting Booth?

    No recent polls found