Blame


1 8f7f2f4a 2021-12-17 jrmu ================================================================================
2 8f7f2f4a 2021-12-17 jrmu
3 8f7f2f4a 2021-12-17 jrmu RSSBot Explained
4 8f7f2f4a 2021-12-17 jrmu
5 8f7f2f4a 2021-12-17 jrmu News feeds are generally handled using RSS feeds. RSS is an open format that
6 8f7f2f4a 2021-12-17 jrmu allows websites to quickly show which articles have been updated recently.
7 8f7f2f4a 2021-12-17 jrmu
8 8f7f2f4a 2021-12-17 jrmu To learn more about RSS, visit: https://rss.softwaregarden.com/aboutrss.html
9 8f7f2f4a 2021-12-17 jrmu
10 8f7f2f4a 2021-12-17 jrmu In RSSBot, we use the XML::RSS::Parser module from CPAN:
11 8f7f2f4a 2021-12-17 jrmu
12 8f7f2f4a 2021-12-17 jrmu use XML::RSS::Parser;
13 8f7f2f4a 2021-12-17 jrmu
14 8f7f2f4a 2021-12-17 jrmu Many of your favorite websites have RSS feeds. Use a search engine to find
15 8f7f2f4a 2021-12-17 jrmu them. In this example, we will use IRCNow's Almanack:
16 8f7f2f4a 2021-12-17 jrmu
17 8f7f2f4a 2021-12-17 jrmu my $url = 'https://wiki.ircnow.org/index.php?n=Site.AllRecentChanges?action=rss';
18 8f7f2f4a 2021-12-17 jrmu
19 8f7f2f4a 2021-12-17 jrmu You can replace this URL with the RSS feed from your favorite website to
20 8f7f2f4a 2021-12-17 jrmu change the news that your bot displays.
21 8f7f2f4a 2021-12-17 jrmu
22 8f7f2f4a 2021-12-17 jrmu We recommend you download an RSS feed and open it with a text editor to
23 8f7f2f4a 2021-12-17 jrmu see what the RSS format looks like. Here is one sample:
24 8f7f2f4a 2021-12-17 jrmu
25 8f7f2f4a 2021-12-17 jrmu <title>Ircnow / Servers</title>
26 8f7f2f4a 2021-12-17 jrmu <link>https://wiki.ircnow.org/index.php?n=Ircnow.Servers</link>
27 8f7f2f4a 2021-12-17 jrmu <dc:contributor>mkf</dc:contributor>
28 8f7f2f4a 2021-12-17 jrmu <dc:date>2021-08-29T15:27:58Z</dc:date>
29 8f7f2f4a 2021-12-17 jrmu <pubDate>Sun, 29 Aug 2021 15:27:58 GMT</pubDate>
30 8f7f2f4a 2021-12-17 jrmu </item>
31 8f7f2f4a 2021-12-17 jrmu <item>
32 8f7f2f4a 2021-12-17 jrmu
33 8f7f2f4a 2021-12-17 jrmu Let's take a look at inside the subroutine said:
34 8f7f2f4a 2021-12-17 jrmu
35 8f7f2f4a 2021-12-17 jrmu sub said {
36 8f7f2f4a 2021-12-17 jrmu my $self = shift;
37 8f7f2f4a 2021-12-17 jrmu my $arguments = shift;
38 8f7f2f4a 2021-12-17 jrmu if ($arguments->{body} =~ /^!rss/) {
39 8f7f2f4a 2021-12-17 jrmu my $p = XML::RSS::Parser->new;
40 8f7f2f4a 2021-12-17 jrmu my $feed = $p->parse_uri($url);
41 8f7f2f4a 2021-12-17 jrmu foreach my $i ( $feed->query('//item') ) {
42 8f7f2f4a 2021-12-17 jrmu my $title = $i->query('title');
43 8f7f2f4a 2021-12-17 jrmu my $contributor = $i->query('dc:contributor');
44 8f7f2f4a 2021-12-17 jrmu my $link = $i->query('link');
45 8f7f2f4a 2021-12-17 jrmu $self->say(
46 8f7f2f4a 2021-12-17 jrmu channel => $arguments->{channel},
47 8f7f2f4a 2021-12-17 jrmu body => $title->text_content.' - '.$contributor->text_content.': '.$link->text_content,
48 8f7f2f4a 2021-12-17 jrmu );
49 8f7f2f4a 2021-12-17 jrmu }
50 8f7f2f4a 2021-12-17 jrmu }
51 8f7f2f4a 2021-12-17 jrmu }
52 8f7f2f4a 2021-12-17 jrmu
53 8f7f2f4a 2021-12-17 jrmu First, we check if a user types a message that begins with !rss
54 8f7f2f4a 2021-12-17 jrmu
55 8f7f2f4a 2021-12-17 jrmu if ($arguments->{body} =~ /^!rss/) {
56 8f7f2f4a 2021-12-17 jrmu
57 8f7f2f4a 2021-12-17 jrmu If the user does, then we create a new Parser object and assign this to $p:
58 8f7f2f4a 2021-12-17 jrmu
59 8f7f2f4a 2021-12-17 jrmu my $p = XML::RSS::Parser->new;
60 8f7f2f4a 2021-12-17 jrmu
61 8f7f2f4a 2021-12-17 jrmu Next, we parse (analyze) the feed using $p, and assign this to $feed:
62 8f7f2f4a 2021-12-17 jrmu
63 8f7f2f4a 2021-12-17 jrmu my $feed = $p->parse_uri($url);
64 8f7f2f4a 2021-12-17 jrmu
65 8f7f2f4a 2021-12-17 jrmu RSS feeds contain a list of news items, and we need to process each item
66 8f7f2f4a 2021-12-17 jrmu one at a time. For this task, we will use a foreach loop. We are going
67 8f7f2f4a 2021-12-17 jrmu to query (ask) $feed for all items, then use the foreach loop to iterate
68 8f7f2f4a 2021-12-17 jrmu through each item. We assign each item to $i.
69 8f7f2f4a 2021-12-17 jrmu
70 8f7f2f4a 2021-12-17 jrmu foreach my $i ( $feed->query('//item') ) {
71 8f7f2f4a 2021-12-17 jrmu
72 8f7f2f4a 2021-12-17 jrmu Next, we query $i to find the title, contributor, and link of each item.
73 8f7f2f4a 2021-12-17 jrmu We assign these values to $title, $contributor, and $link.
74 8f7f2f4a 2021-12-17 jrmu
75 8f7f2f4a 2021-12-17 jrmu my $title = $i->query('title');
76 8f7f2f4a 2021-12-17 jrmu my $contributor = $i->query('dc:contributor');
77 8f7f2f4a 2021-12-17 jrmu my $link = $i->query('link');
78 8f7f2f4a 2021-12-17 jrmu
79 8f7f2f4a 2021-12-17 jrmu For each item, we send a message to the channel with the title, contributor,
80 8f7f2f4a 2021-12-17 jrmu and link.
81 8f7f2f4a 2021-12-17 jrmu
82 8f7f2f4a 2021-12-17 jrmu $self->say(
83 8f7f2f4a 2021-12-17 jrmu channel => $arguments->{channel},
84 8f7f2f4a 2021-12-17 jrmu body => $title->text_content.' - '.$contributor->text_content.': '.$link->text_content,
85 8f7f2f4a 2021-12-17 jrmu );
86 8f7f2f4a 2021-12-17 jrmu
87 8f7f2f4a 2021-12-17 jrmu ================================================================================
88 8f7f2f4a 2021-12-17 jrmu
89 8f7f2f4a 2021-12-17 jrmu Further Reading
90 8f7f2f4a 2021-12-17 jrmu
91 8f7f2f4a 2021-12-17 jrmu XML::RSS::Parser module: https://metacpan.org/pod/XML::RSS::Parser
92 8f7f2f4a 2021-12-17 jrmu
93 8f7f2f4a 2021-12-17 jrmu ================================================================================
94 8f7f2f4a 2021-12-17 jrmu
95 8f7f2f4a 2021-12-17 jrmu Learn about Loops
96 8f7f2f4a 2021-12-17 jrmu
97 8f7f2f4a 2021-12-17 jrmu View the file ~/control to learn about control structures for perl.
98 8f7f2f4a 2021-12-17 jrmu
99 8f7f2f4a 2021-12-17 jrmu ================================================================================