The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

XML::RSS::Headline - Wrapper for RSS Feed Headline Items

SYNOPSIS

Headline object to encapsulate the headline/URL combination of a RSS feed. It provides a unique id either by way of the URL or by doing an MD5 checksum on the headline (when URL uniqueness fails).

CONSTRUCTOR

XML::RSS::Headline->new( headline => $headline, url => $url )
XML::RSS::Headline->new( item => $item )

A XML::RSS::Headline object can be initialized either with headline/url or with a parse XML::RSS item structure. The argument 'headline_as_id' is optional and takes a boolean as its value.

METHODS

$headline->id

The id is our unique identifier for a headline/url combination. Its how we can keep track of which headlines we have seen before and which ones are new. The id is either the URL or a MD5 checksum generated from the headline text (if $headline->headline_as_id is true);

$headline->multiline_headline

This method returns the headline as either an array or array reference based on context. It splits headline on newline characters into the array.

$headline->item( $item )

Init the object for a parsed RSS item returned by XML::RSS.

$headline->set_first_seen
$headline->set_first_seen( Time::HiRes::time )

Set the time of when the headline was first seen. If you pass in a value it will be used otherwise calls Time::HiRes::time.

$headline->first_seen

The time (in epoch seconds) of when the headline was first seen.

$headline->first_seen_hires

The time (in epoch seconds and milliseconds) of when the headline was first seen.

GET/SET ACCESSOR METHODS

$headline->headline
$headline->headline( $headline )

The rss headline/title

$headline->url
$headline->url( $url )

The rss link/url. URI->canonical is called to attempt to normalize the URL

$headline->description
$headline->description( $description )

The description of the RSS headline.

$headline->headline_as_id
$headline->headline_as_id( $bool )

A bool value that determines whether the URL will be the unique identifier or the if an MD5 checksum of the RSS title will be used instead. (when the URL doesn't provide absolute uniqueness or changes within the RSS feed)

This is used in extreme cases when URLs aren't always unique to new healines (Use Perl Journals) and when URLs change within a RSS feed (www.debianplanet.org / debianplanet.org / search.cpan.org,search.cpan.org:80)

$headline->timestamp
$headline->timestamp( Time::HiRes::time() )

A high resolution timestamp that is set using Time::HiRes::time when the object is created.

SUBCLASS EXAMPLE

You can also subclass XML::RSS::Headline to provide a 'multiline' RSS headline based on additional information inside the RSS Feed. Here is an example for the Perl Jobs (jobs.perl.org) RSS feed.

    use XML::RSS::Feed;
    use LWP::Simple qw(get);
    use PerlJobs;

    my $feed = XML::RSS::Feed->new(
        url   => "http://jobs.perl.org/rss/standard.rss",
        hlobj => "PerlJobs",
        name  => "perljobs",
        delay => 60,
    );

    while (1) {
        $feed->parse(get($feed->url));
        print $_->headline . "\n" for $feed->late_breaking_news;
        sleep($feed->delay); 
    }

Here is PerlJobs.pm which is subclassed from XML::RSS::Headline in this example and $headline->item is redefined to init $self->headline with more nested info located with in the parsed RSS item structure. (Notice that we're just concatinating bits of info with newlines to seperate the lines.

    package XML::RSS::Headline::PerlJobs;
    use strict;
    use base qw(XML::RSS::Headline);

    sub item
    {
        my ($self,$item) = @_;
        $self->SUPER::item($item); # set url and description
        $self->headline( 
            $item->{title} . "\n" . 
            $item->{'http://jobs.perl.org/rss/'}{company_name} || "Unknown Company" . " - " .
            $item->{'http://jobs.perl.org/rss/'}{location} || "Unknown Location" . "\n" .  
            $item->{'http://jobs.perl.org/rss/'}{hours} || "Unknown Hours" . ", " . 
            $item->{'http://jobs.perl.org/rss/'}{employment_terms} || "Unknown Terms"
        );
    }

    1;

Here is the output from rssbot on irc.perl.org in channel #news (which uses these modules)

    <rssbot>  + Part Time Perl
    <rssbot>    Brian Koontz - United States, TX, Dallas
    <rssbot>    Part time, Independent contractor (project-based)
    <rssbot>    http://jobs.perl.org/job/950

AUTHOR

Jeff Bisbee
CPAN ID: JBISBEE
cpan@jbisbee.com
http://search.cpan.org/author/JBISBEE/

COPYRIGHT

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the LICENSE file included with this module.

SEE ALSO

XML::RSS::Feed, POE::Component::RSSAggregator