1 8f7f2f4a 2021-12-17 jrmu ================================================================================
3 8f7f2f4a 2021-12-17 jrmu RSSBot Explained
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.
8 8f7f2f4a 2021-12-17 jrmu To learn more about RSS, visit: https://rss.softwaregarden.com/aboutrss.html
10 8f7f2f4a 2021-12-17 jrmu In RSSBot, we use the XML::RSS::Parser module from CPAN:
12 8f7f2f4a 2021-12-17 jrmu use XML::RSS::Parser;
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:
17 8f7f2f4a 2021-12-17 jrmu my $url = 'https://wiki.ircnow.org/index.php?n=Site.AllRecentChanges?action=rss';
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.
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:
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>
33 8f7f2f4a 2021-12-17 jrmu Let's take a look at inside the subroutine 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');
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,
53 8f7f2f4a 2021-12-17 jrmu First, we check if a user types a message that begins with !rss
55 8f7f2f4a 2021-12-17 jrmu if ($arguments->{body} =~ /^!rss/) {
57 8f7f2f4a 2021-12-17 jrmu If the user does, then we create a new Parser object and assign this to $p:
59 8f7f2f4a 2021-12-17 jrmu my $p = XML::RSS::Parser->new;
61 8f7f2f4a 2021-12-17 jrmu Next, we parse (analyze) the feed using $p, and assign this to $feed:
63 8f7f2f4a 2021-12-17 jrmu my $feed = $p->parse_uri($url);
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.
70 8f7f2f4a 2021-12-17 jrmu foreach my $i ( $feed->query('//item') ) {
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.
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');
79 8f7f2f4a 2021-12-17 jrmu For each item, we send a message to the channel with the title, contributor,
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,
87 8f7f2f4a 2021-12-17 jrmu ================================================================================
89 8f7f2f4a 2021-12-17 jrmu Further Reading
91 8f7f2f4a 2021-12-17 jrmu XML::RSS::Parser module: https://metacpan.org/pod/XML::RSS::Parser
93 8f7f2f4a 2021-12-17 jrmu ================================================================================
95 8f7f2f4a 2021-12-17 jrmu Learn about Loops
97 8f7f2f4a 2021-12-17 jrmu View the file ~/control to learn about control structures for perl.
99 8f7f2f4a 2021-12-17 jrmu ================================================================================