]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/slashdot.pl
* Add vim formatting comments ( # vim:ts=4:sw=4:expandtab:tw=80 )
[infobot.git] / src / Modules / slashdot.pl
1 #
2 # Slashdot.pl: Slashdot headline retrival
3 #      Author: Chris Tessone <tessone@imsa.edu>
4 #    Modified: dms
5 #   Licensing: Artistic License (as perl itself)
6 #     Version: v0.4 (19991125)
7 #
8
9 ###
10 # fixed up to use XML'd /. backdoor 7/31 by richardh@rahga.com
11 # My only request if this gets included in infobot is that the
12 # other header gets trimmed to 2 lines, dump the fluff ;) -rah
13 #
14 # added a status message so people know to install LWP - oznoid
15 # also simplified the return code because it wasn't working.
16 ###
17
18 package Slashdot;
19
20 use strict;
21
22 sub slashdotParse {
23     my @list;
24
25     foreach (@_) {
26         next unless (/<title>(.*?)<\/title>/);
27         my $title = $1;
28         $title =~ s/&amp\;/&/g;
29         push(@list, $title);
30     }
31
32     return @list;
33 }
34
35 sub Slashdot {
36     my @results = &::getURL("http://slashdot.org/slashdot.xml");
37     my $retval  = "i could not get the headlines.";
38
39     if (scalar @results) {
40         my $prefix      = 'Slashdot Headlines ';
41         my @list        = &slashdotParse(@results);
42         $retval         = &::formListReply(0, $prefix, @list);
43     }
44
45     &::performStrictReply($retval);
46 }
47
48 sub slashdotAnnounce {
49     my $file = "$::param{tempDir}/slashdot.xml";
50
51     my @Cxml = &::getURL("http://slashdot.org/slashdot.xml");
52     if (!scalar @Cxml) {
53         &::DEBUG("sdA: failure (Cxml == NULL).");
54         return;
55     }
56
57     if (! -e $file) {           # first time run.
58         open(OUT, ">$file");
59         foreach (@Cxml) {
60             print OUT "$_\n";
61         }
62         close OUT;
63
64         return;
65     }
66
67     my @Oxml;
68     open(IN, $file);
69     while (<IN>) {
70         chop;
71         push(@Oxml,$_);
72     }
73     close IN;
74
75     my @Chl = &slashdotParse(@Cxml);
76     my @Ohl = &slashdotParse(@Oxml);
77
78     my @new;
79     foreach (@Chl) {
80         last if ($_ eq $Ohl[0]);
81         push(@new, $_);
82     }
83
84     if (scalar @new == 0) {
85         &::status("Slashdot: no new headlines.");
86         return;
87     }
88
89     if (scalar @new == scalar @Chl) {
90         &::DEBUG("sdA: scalar(new) == scalar(Chl). bad?");
91     }
92
93     open(OUT,">$file");
94     foreach (@Cxml) {
95         print OUT "$_\n";
96     }
97     close OUT;
98
99     return "Slashdot: News for nerds, stuff that matters -- ".
100                         join(" \002::\002 ", @new);
101 }
102
103 1;
104
105 # vim:ts=4:sw=4:expandtab:tw=80