All pastes #393922 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-03-13 22:33 UTC
rendered paste body

# LotR++ for this one
# minor mods by lenzo@cs.cmu.edu

BEGIN {
    eval qq{
        use LWP::UserAgent;
        use XML::RSS;
        use HTTP::Request::Common qw(GET);
    };

    $no_headlines++ if($@);
}

sub get_headlines {
    my ($rdf_loc) = @_;

    if ($no_headlines) {
        return "error: RDF headlines require LWP::UserAgent, XML::RSS, and HTTP::Request... sorry.";
    }

    if ($rdf_loc) {
        &status("getting headlines from $rdf_loc");

        my $ua = new LWP::UserAgent;
        if (my $proxy = main::getparam('httpproxy')) {
            $ua->proxy('http', $proxy)
        };

        $ua->timeout(10);

        my $request = new HTTP::Request ("GET", $rdf_loc);
        my $result = $ua->request ($request);

        if ($result->is_success) {
            my ($str, $error);
            $str = $result->content;
            $rss = new XML::RSS;
            eval { $rss->parse($str); };
            if ($@) {
                main::status("RDF: $@")
                # but soldier on, in hopes that we've gotten
                # something sensible nonetheless
            }
            my $return;

            if ( scalar(@{$rss->{"items"}}) > 0 ) {
                foreach my $item (@{$rss->{"items"}}) {
                    $return .= $item->{"title"} . "; ";
                    last if length($return) > $param{maxDataSize};
                }

                $return =~ s/; $//;
                $return =~ s/\s+/ /sg;

                return $return;
            } else {
                return "I'm sorry; something went wrong with that RSS feed and I couldn't find anything sensible in it.  Is it well-formed?"
            }
        } else {
            return "error: $rdf_loc wasn't successful";
        }
    } else {
        return "error: no location stored for $where";
    }
};

1;

__END__