/
RDF.pl
99 lines (70 loc) · 2.23 KB
/
RDF.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# 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__
=head1 NAME
RDF.pl - Read RDF files into factoids
=head1 PREREQUISITES
LWP::UserAgent
XML::RSS
HTTP::Request::Common
=head1 PARAMETERS
rss
=head1 PUBLIC INTERFACE
<site> is <rss="site.rdf">
=head1 DESCRIPTION
This allows you to read and parse RSS files; RSS is a format
for getting news headlines off web news services.
=head1 AUTHORS
LotR <martijn@earthling.net> and Kevin Lenzo, of course.