Erlang Central

Difference between revisions of "How to setup an RSS feed with Yaws"

From ErlangCentral Wiki

(How to configure Yaws)
m (firebird->firefox)
(7 intermediate revisions by 5 users not shown)
Line 1:Line 1:
==How to setup your an RSS feed with Yaws==
+
==Author==
 +
Tobbe
 +
 
 +
==How to setup a RSS feed with Yaws==
 
===Introduction===
 
===Introduction===
You probably already know how useful RSS is to aggregate information feeds. If you don't, then simply use your Mozilla Firebird browser and go to an RSS-enabled site, e.g http://slashdot.org. Note the little orange icon down in the right corner of the browser window. Click on the icon and add the RSS-feed to you personal toolbar. Then click on your toolbar, on the RSS-bookmark and watch the nice menu of slashdot article-summarys popping up.  
+
You probably already know how useful RSS is to aggregate information feeds. If you don't, then simply use your Mozilla Firefox browser and go to an RSS-enabled site, e.g http://slashdot.org. Note the little orange icon down in the right corner of the browser window. Click on the icon and add the RSS-feed to you personal toolbar. Then click on your toolbar, on the RSS-bookmark and watch the nice menu of slashdot article-summarys popping up.  
  
 
The module yaws_rss.erl provides an RSS store/interface so that you can RSS enable your application. It is dead simple to get your RSS feed going, and we will show here how you can do it.  
 
The module yaws_rss.erl provides an RSS store/interface so that you can RSS enable your application. It is dead simple to get your RSS feed going, and we will show here how you can do it.  
  
===To create an RSS feed.===
+
===To create a RSS feed.===
 
Make sure your top page has the corresponding link information in the 'head' part as shown in the example below. Don't forget to change the URL (www.blaha.org) to point to your own server.  
 
Make sure your top page has the corresponding link information in the 'head' part as shown in the example below. Don't forget to change the URL (www.blaha.org) to point to your own server.  
  
<table class="ntable" width="100%" cellspacing="0" cellpadding="0" border="0">
+
{{CodeSnippet|Code listing 1.1: Your top 'index.html'|<pre>
<tr><td class="infohead" bgcolor="#7a5ada"><p class="caption">
+
            Code listing 1.1: Your top 'index.html'</p></td></tr>
+
<tr><td bgcolor="#ddddff"><pre>
+
 
  &lt;html&gt;
 
  &lt;html&gt;
 
    &lt;head&gt;
 
    &lt;head&gt;
Line 24:Line 24:
 
      &lt;/body&gt;
 
      &lt;/body&gt;
 
    &lt;/html&gt;
 
    &lt;/html&gt;
</pre></td></tr>
+
</pre>}}
</table>
+
  
 
The link info above refers to a file rss.yaws which could look something like this:  
 
The link info above refers to a file rss.yaws which could look something like this:  
  
<table class="ntable" width="100%" cellspacing="0" cellpadding="0" border="0">
+
{{CodeSnippet|Code listing 1.2: Your 'rss.yaws' file|<pre>
<tr><td class="infohead" bgcolor="#7a5ada"><p class="caption">
+
            Code listing 1.2: Your 'rss.yaws' file</p></td></tr>
+
<tr><td bgcolor="#ddddff"><pre>
+
 
  &lt;rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"&gt;
 
  &lt;rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"&gt;
 
    &lt;channel&gt;
 
    &lt;channel&gt;
Line 56:Line 52:
 
    &lt;/channel&gt;
 
    &lt;/channel&gt;
 
  &lt;/rss&gt;
 
  &lt;/rss&gt;
</pre></td></tr>
+
</pre>}}
</table>
+
  
 
To insert RSS items into the feed from your application, you can write code like:  
 
To insert RSS items into the feed from your application, you can write code like:  
  
<table class="ntable" width="100%" cellspacing="0" cellpadding="0" border="0">
+
{{CodeSnippet|Code listing 1.3: Adding RSS info from your Erlang code.|<pre>
<tr><td class="infohead" bgcolor="#7a5ada"><p class="caption">
+
            Code listing 1.3: Adding RSS info from your Erlang code.</p></td></tr>
+
<tr><td bgcolor="#ddddff"><pre>
+
 
  ....
 
  ....
 
  yaws_rss:insert(test, xml, "Yaws now support RSS feeds.",
 
  yaws_rss:insert(test, xml, "Yaws now support RSS feeds.",
Line 71:Line 63:
 
                  "Bill Smith").
 
                  "Bill Smith").
 
  ....
 
  ....
</pre></td></tr>
+
</pre>}}
</table>
+
  
 
First comes the application (RSSid), followed by a tag (these were used in rss.yaws when we retrieved the RSS content). Then follows the Title, Link, Description and Creator.  
 
First comes the application (RSSid), followed by a tag (these were used in rss.yaws when we retrieved the RSS content). Then follows the Title, Link, Description and Creator.  
Line 81:Line 72:
 
Inside a <server> block you specify the application you want to produce RSS content. In our example, our application was named: test.  
 
Inside a <server> block you specify the application you want to produce RSS content. In our example, our application was named: test.  
  
<table class="ntable" width="100%" cellspacing="0" cellpadding="0" border="0">
+
{{CodeSnippet|Code listing 1.4: Mandatory configuration|<pre>
<tr><td class="infohead" bgcolor="#7a5ada"><p class="caption">
+
            Code listing 1.4: Mandatory configuration</p></td></tr>
+
<tr><td bgcolor="#ddddff"><pre>
+
 
  &lt;server ....&gt;
 
  &lt;server ....&gt;
  
Line 94:Line 82:
  
 
  &lt;/server.&gt;
 
  &lt;/server.&gt;
</pre></td></tr>
+
</pre>}}
</table>
+
  
 
Other, non-mandatory, configuration parameters are (with explanation and default values):  
 
Other, non-mandatory, configuration parameters are (with explanation and default values):  
  
<table class="ntable" width="100%" cellspacing="0" cellpadding="0" border="0">
+
{{CodeSnippet|Code listing 1.5: Non mandatory configuration|<pre>
<tr><td class="infohead" bgcolor="#7a5ada"><p class="caption">
+
            Code listing 1.5: Non mandatory configuration</p></td></tr>
+
<tr><td bgcolor="#ddddff"><pre>
+
 
  &lt;server ....&gt;
 
  &lt;server ....&gt;
  
Line 115:Line 99:
  
 
  &lt;/server.&gt;
 
  &lt;/server.&gt;
</pre></td></tr>
+
</pre>}}
</table>
+
  
 
So for example, by setting the rss_expire to days you can set the number of days items will show up with rss_days. You can also have expired items removed and/or limit the maximum number of items you want to store.  
 
So for example, by setting the rss_expire to days you can set the number of days items will show up with rss_days. You can also have expired items removed and/or limit the maximum number of items you want to store.  
Line 123:Line 106:
  
 
==Download xml==
 
==Download xml==
[http://wiki.trapexit.erlang-consulting.com/upload/howto/howto_rss_aggregator.xml howto_rss_aggregator.xml]
+
[http://wiki.trapexit.erlang-consulting.com/upload/howto/yaws_rss_feed.xml yaws_rss_feed.xml]
  
 
[[Category:HowTo]]
 
[[Category:HowTo]]

Revision as of 06:03, 19 July 2007

Contents

Author

Tobbe

How to setup a RSS feed with Yaws

Introduction

You probably already know how useful RSS is to aggregate information feeds. If you don't, then simply use your Mozilla Firefox browser and go to an RSS-enabled site, e.g http://slashdot.org. Note the little orange icon down in the right corner of the browser window. Click on the icon and add the RSS-feed to you personal toolbar. Then click on your toolbar, on the RSS-bookmark and watch the nice menu of slashdot article-summarys popping up.

The module yaws_rss.erl provides an RSS store/interface so that you can RSS enable your application. It is dead simple to get your RSS feed going, and we will show here how you can do it.

To create a RSS feed.

Make sure your top page has the corresponding link information in the 'head' part as shown in the example below. Don't forget to change the URL (www.blaha.org) to point to your own server.

Code listing 1.1: Your top 'index.html'

	  <html>
	    <head>
	      <link rel="alternate" 
		    title="Example RSS" 
		    href="//www.blaha.org/rss.yaws" 
		    type="application/rss+xml>
		<title>Example</title>
	      </head>
	      <body>
		Hello world
	      </body>
	    </html>
	

The link info above refers to a file rss.yaws which could look something like this:

Code listing 1.2: Your 'rss.yaws' file

	  <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
	    <channel>
	      <title>Exampel RSS feed</title>
	      <link>http://www.blaha.org/</link>
	      <description>
		Bla bla....
	      </description>
	      <language>iso8859-1</language>
	      
	      <erl>
		
		out(A) ->
		    case yaws_rss:retrieve(test, xml) of
         		{ok, Xml} ->
		            {html, [Xml]};
         		_ ->
		            {html, ""}
                end.
		
	      </erl>
	      
	    </channel>
	  </rss>
	

To insert RSS items into the feed from your application, you can write code like:

Code listing 1.3: Adding RSS info from your Erlang code.

	  ....
	  yaws_rss:insert(test, xml, "Yaws now support RSS feeds.",
	                  "http://www.blaha.org/latest_news.html",
	                  "Yaws has now been extended to support....",
	                  "Bill Smith").
	  ....
	

First comes the application (RSSid), followed by a tag (these were used in rss.yaws when we retrieved the RSS content). Then follows the Title, Link, Description and Creator.

The idea is that several independent application should be able to use the yaws_rss.erl API without affecting each other. Also, one application may have several RSS feeds (hence the Tag). So in your case you could change the RSSid and Tag to something more suitable.

How to configure Yaws

Inside a <server> block you specify the application you want to produce RSS content. In our example, our application was named: test.

Code listing 1.4: Mandatory configuration

	  <server ....>

            .....other configs here....

	    <rss>
	      rss_id = test
	    </rss>

	  </server.>
	

Other, non-mandatory, configuration parameters are (with explanation and default values):

Code listing 1.5: Non mandatory configuration

	  <server ....>

            .....other configs here....

	    <rss>
	      rss_expire  =  false | days   # expire items, or not (false)
	      rss_days    =  <integer>      # days until expired
	      rss_rm_exp  =  <bool>         # remove expired items (false)
	      rss_max     =  <integer>      # max no.of items stored (infinite)
	    </rss>

	  </server.>
	

So for example, by setting the rss_expire to days you can set the number of days items will show up with rss_days. You can also have expired items removed and/or limit the maximum number of items you want to store.

This is it! Your RSS feed is now ready to go. Was this dead simple or not... ;-)

Download xml

yaws_rss_feed.xml