You're right - had I known within the first 2 weeks of joining PM, I probably would have done it with XML rather than HTML - the HTML parsing is kinda ugly. ;-) That said, it works at the moment, and I'm not exactly one to change things that are working. If it stops working, I'll take a look at which is easier - ripping out the HTML::Parser and replacing it with XML::Twig (my favourite XML handler), or just tweaking it. :-)

As for the XML xml ticker - given how frequently that changes, I think I'm doing both of us a favour by not using it. As in, I try not to hit the monastary for anything I don't need ;-)

... ok, I tried the conversion quickly. Of course, XML::Twig overwhelms the whole process to take longer ...

#! /usr/bin/perl use strict; use warnings; use LWP::Simple qw(); use XML::Twig; use Date::Parse; use Data::Dumper; use Term::ANSIColor qw(:constants); use DBI; use FindBin; my $user = shift or die "Need to pass in user name!"; my @convert = ( [ 1, '%02d', 'sec' ], [ 60, '%02d:', 'min' ], [ 60, '%2d:', 'hr' ], [ 24, '%d ', 'd' ], ); sub convert_seconds { my $sec = shift; my @c = @convert; my @vals = ($sec); my $output = ' %s'; while (my $c = shift @c) { if ($vals[0] > $c->[0]) { $output = $c->[1] . $output; unshift @vals, int($vals[0] / $c->[0]); $vals[1] %= $c->[0]; $vals[-1] = $c->[2]; } else { last; } } sprintf $output, @vals; } my $xml = #do { use IO::File;local $/; my $f = IO::File->new('i.xml'); + <$f> }; LWP::Simple::get('http://www.perlmonks.org/index.pl?displaytype=xml;no +de=' . $user); if ($xml) { my %data; my $twig = XML::Twig->new(); $twig->parse($xml); my $rootnode = $twig->root(); $data{created} = $rootnode->att('created'); for my $want (qw(experience numwriteups)) { ($data{$want} = ($rootnode->descendants("field[\@name=\"$want\ +"]"))[0]->text()) =~ s/\s+//g; } my $table_file = File::Spec->catfile($FindBin::Bin,'stats.' . $use +r); my $eol = -e $table_file ? "\n" : ', '; my $since = $data{created}; #$since =~ s/\s\S*$//; #$since =~ s/at //; my $tbegin = str2time($since, 'GMT'); my $tdiff = time - $tbegin; my $duration = $tdiff / (60 * 60 * 24); if ($eol =~ /^\s*$/) { print $user,$/; print '-' x (length $user), $/; } else { print "[$user] stats: "; } printf "Member for: %.3f days%s", $duration, $eol; println('Experience: ', $data{experience}, $duration); print $eol; println('Writeups: ', $data{numwriteups}, $duration); print $eol; printf("Which makes it %s%.3f%s XP per writeup!\n", BOLD.RED, ($data{experience} / $data{numwriteups}, RESET)); #/)); if (-e $table_file) { my $dbh = DBI->connect('dbi:CSV:f_dir=' . $FindBin::Bin . ";cs +v_eol=\n") or warn "Can't connect to DBI"; $dbh->{csv_tables}->{stats} = { file => $table_file, }; my $total = $dbh->selectall_arrayref('select sum(xp) from stat +s')->[0][0]; if ($total != $data{experience}) { require Time::localtime; my $lt = Time::localtime::localtime(time() + (2 * 60 * 60) +); my $date = sprintf("%04d-%02d-%02d", $lt->year + 1900, $lt +->mon + 1, $lt->mday); #print "Today is $date\n"; my $count = $dbh->selectall_arrayref('select count(*) from + stats where date = ?', {}, $date)->[0][0]; my $cur = $dbh->selectall_arrayref('select xp from stats w +here date = ?', {}, $date); $cur = $cur->[0] while $cur and ref $cur; my $gained = $data{experience} - $total; printf "%s %s%d%s XP!\n", $gained > 0 ? "Gained" : "Lost", + RED.BOLD, abs($gained), RESET; if ($count) { $cur += $gained; print "Updating today ($date) to be XP = $cur\n"; $dbh->do('update stats set xp = ? where date = ?', und +ef, $cur, $date); } else { $cur = $gained; print "Inserting into today to be XP = $cur\n"; $dbh->do('insert into stats (date,xp) values(?,?)', {} +, $date,$cur); } } } } else { print "Can't get node for $user\n"; } sub println { my ($type, $data, $duration) = @_; my $rate = $data / $duration; my $per = 24 * 60 * 60 / $rate; printf("%s %d (%s%.3f%s per day, or 1 per %s%s%s)", $type, $data, BLUE.BOLD, $rate, RESET, BOLD.BLUE, convert_s +econds($per), RESET); }
I'm sure there's a faster way ... but whatever, it's just a cheap parlour trick anyway :-)

Updated: one line in the code still had the old HTML field of "User since" rather than the xml field name of "created". Fixed. Output looks better, too.


In reply to Re^2: Personal stats by Tanktalus
in thread Personal stats by Tanktalus

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.