Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Warning:The following code is HEAVILY based on OeufMayos XML::Parser tutorial and lhowards LWP/HTTP/PM node...Most credit goes to them.

I shamelessly abused the code so I could get both XPs and messages from the inbox and get them in XChat, which allows perl scripting...
I split it up in 2 bits, because the actual grabbing and parsing takes a few moments, that will stall XChat for a short period.
Edit: Tweak Monestary related stuff... thanks epoptai !
#!/usr/bin/perl -w # XP Grabber... use strict; use XML::Parser; use LWP::UserAgent; use URI::URL; use HTTP::Cookies; my $parser = new XML::Parser ( Handlers => { Start => \&hdl_start, End + => \&hdl_end, Char => \&hdl_char, Default => \&hdl_def }); my $pm_user=''; #ofcourse fill you alias in here my $pm_password=''; #and your password in here my $WWWAgent = new LWP::UserAgent(); my $co=new HTTP::Cookies(file=>'./cookies.dat',autosave=>1,ignore_disc +ard=>1); $WWWAgent->cookie_jar($co); my $url=new URI::URL 'http://www.perlmonks.org/index.pl'; $url->query_form(op => "login",node_id => "16046",user => $pm_user,pas +swd => $pm_password, expires=> "+10y"); my $WWWRequest = new HTTP::Request 'GET', $url->as_string() ; my $WWWResult = $WWWAgent->request($WWWRequest); die "Error logging in $WWWResult->code $WWWResult->message" if(!$WWWRe +sult->is_success); my $cb_ticker = $WWWResult->content; $parser->parse($cb_ticker); sub hdl_start{ my ($p, $elt, %atts) = @_; if ($elt eq "XP") { open (XP,">/tmp/XP.db") || die $!; flock(XP,2); print XP "XP: ",$atts{xp},"\nXPs to next level: ",$atts{xp2nextlev +el}; close(XP); } } sub hdl_end{ my ($p, $elt) = @_; } sub hdl_char { my ($p, $str) = @_; } sub hdl_def { }


Similar chunk of code for Inbox grabber, but with the following differences...
my $url=new URI::URL 'http://www.perlmonks.org/index.pl'; $url->query_form(op => "login",node_id => "15848",user => $pm_user,pas +swd => $pm_password,expires=> "+10y"); my $WWWRequest = new HTTP::Request 'GET', $url->as_string() ; my $WWWResult = $WWWAgent->request($WWWRequest); die "Error logging in $WWWResult->code $WWWResult->message" if(!$WWWRe +sult->is_success); $parser->parse($cb_ticker); my $key = undef; sub hdl_start{ my ($p, $elt, %atts) = @_; if ($elt eq "message") { open(INBOX,">>/tmp/inbox.db") || die $!; my $time = $atts{time}; $time =~ s/(\d{4})(\d{2})(\d{2})(\d{2})(\d +{2})(\d{2})/$4\:$5\:$6 $3\/$2\/$1/; $key++; print INBOX "Time: ",$time," Message from: ",$atts{author}," Statu +s: ",$atts{status},"\n"; close(INBOX); } } #Edit: Oops, pasted an older snippet sub hdl_end{ my ($p, $elt) = @_; } sub hdl_char { my ($p, $str) = @_; $str =~ s/^\n+//g; if ($key && $str) { $str =~ s/\[id\:\/\/(\d*?)\]/http\:\/\/www\.perlmonks\.com\/index\.pl +\?node_id\=$1/g; print $str,"\n"; open(INBOX,">>/tmp/inbox.db") || die $!; print INBOX $str,"\n"; close(INBOX); } } sub hdl_def { }
The 2 grabbers run as cronjob, while the XChat script just reads the XP and Inbox file...
#!/usr/bin/perl IRC::print "\0035:: Loading X-Chat Perlmonks XML Ticker ::\003 \n"; IRC::register ("PM XML Ticker", "0.1", "", ""); IRC::add_timeout_handler(300000, getXP); #Add a timeout handler that checks every 5 mins IRC::add_timeout_handler(300000, getInbox); #again :) but diff sub handler sub getXP { open(XP,"</tmp/XP.db") || die $!; $xp = <XP>; close(XP); chomp $xp; IRC::print $xp,"\n"; IRC::add_timeout_handler(300000, getXP); } sub getInbox { open(INBOX,"</tmp/inbox.db") || die $!; (@Inbox) = <INBOX>; close(INBOX); if (@Inbox) { IRC::print @Inbox; } IRC::add_timeout_handler(300000, getInbox); }
For some reason, this stuff sometimes crashes XChat.... I'll get an update here ASAP.
Greetz
Beatnik
... Quidquid perl dictum sit, altum viditur.

In reply to XP & Inbox XChat Perl script by Beatnik

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, 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, 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.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (4)
    As of 2020-02-23 15:00 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      What numbers are you going to focus on primarily in 2020?










      Results (102 votes). Check out past polls.

      Notices?