Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

parse the "Private Message XML Ticker" with index and substr

by PodMaster (Abbot)
on Mar 18, 2002 at 08:28 UTC ( #152432=snippet: print w/ replies, xml ) Need Help??

Description: Like the title says. Uses LWP::Simple to fetch the ticker. Requires that you provide a username and password.
#!/usr/bin/perl -w

=head1 parse the "Private Message XML Ticker" with C<index> and C<subs
+tr>

For real.  Probably won't work if the "DTD" changes (not bloody likely
+).

=cut

use strict;
use LWP::Simple;
# private message xml ticker 15848
my $user = shift or die "please supply username";
my $pass = shift or die "please supply password";
my $html = get('http://perlmonks.org/index.pl?node_id=15848;op=login;t
+icker=yes;user='
               .$user.';passwd='.$pass);

die unless $html;
warn $html;

my $fh = [split "\n", $html];

while(@{$fh}) {
    parse_private_messages(shift @{$fh}, $fh);
}
exit;


sub fmat_timestamp {
    my $s = shift;
#yyyymmddhhmmss
    my $y = substr($s,0,4,'');
    my $m = substr($s,0,2,'');
    my $d = substr($s,0,2,'');
    my $h = substr($s,0,2,'');
    my $mm = substr($s,0,2,'');
    my $ss = substr($s,0,2,'');

#yyyy-mm-dd hh:mm:ss
    return "$y-$m-$d $h:$mm:$ss";
}

sub parse_private_messages {
    my $s = shift;
    my $fh = shift;

#<message status="active" user_id="83485" author="blakem" message_id="
+539766685" time="20011130001441">
    my $sX = index($s,'<message status="'); # 17 active|archived
    my $iX = index($s,'" user_id="'); # 11
    my $aX = index($s,'" author="'); # 10
    my $mX = index($s,'" message_id="'); # 14
    my $tX = index($s,'" time="'); # 8 ++ 14

    my ($status, $id, $author, $mid, $time);

    if($sX >=0 and $iX >= 0 and $aX >= 0 and $mX >= 0 and $tX >= 0) {
        printf '(%s)(%s)(%s)(%s)(%s)'."\n", $sX, $iX, $aX, $mX, $tX;
        printf '(%s)(%s)(%s)(%s)(%s)'."\n",
            $status = substr($s,$sX + 17, $iX - $sX - 17),
            $id = substr($s,$iX + 11, $aX - $iX - 11),
            $author = substr($s,$aX + 10, $mX - $aX - 10),
            $mid= substr($s,$mX + 14, $tX - $mX - 14),
            $time= fmat_timestamp(substr($s,$tX + 8, 14)),
            ,;

        $s = shift(@{$fh});
        #</message> # 10
        print substr($s,0, -10);
        print "\n\n";
    }
}

__END__

<CHATTER><INFO site="http://perlmonks.org" sitename="Perl Monks">Rende
+red by the
 Private Message XML Ticker</INFO>
        <message status="active" user_id="83485" author="blakem" messa
+ge_id="539
766685" time="20011130001441">
heh, [podmaster] is Cheesy WebRing Member #69 ;-)</message>
        <message status="active" user_id="78006" author="no_slogan" me
+ssage_id="
539897350" time="20020314043623">
Re pseudorandom: Touche.</message>
</CHATTER>


(1)(24)(40)(56)(79)
(active)(83485)(blakem)(539766685)(2001-11-30 00:14:41)
heh, [podmaster] is Cheesy WebRing Member #69 ;-)

(1)(24)(40)(59)(82)
(active)(78006)(no_slogan)(539897350)(2002-03-14 04:36:23)
Re pseudorandom: Touche.

Comment on parse the "Private Message XML Ticker" with index and substr
Download Code

Back to Snippets Section

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2014-08-31 11:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls