| Digg that contains just stories from specific topics and/or with specfic keywords. The demo is built using one REST robot that retreives the story topics from Digg and one RSS robot that creates the RSS feed. This is a good example of how to use both REST robots with JSON output and RSS robots. Robots and the full source code are of course available for download. |
 |
The Demo
In this demo the user can specify what topics and keywords that should be used to create a personal RSS feed from Digg. The process of the demo is:
- The REST robot is executed when the page loads. This robot goes out to Digg.com and retreive all the available subject topics and returns this in a JSON array.
- Javascript reads the JSON array and populates the form with the right dropdowns and input text fields etc.
- The user fills in the form with the configurations he/she wants for the personal Digg RSS feed.
- When the user is done with the configuration and clicks "Generate RSS URL" a URL to the RSS robot is created by the javascript. This URL contains all the user configurations as parameters.
- The user can read the RSS feed by calling the generated URL. The RSS robot is executed and gets the stories from Digg.
The REST robot
The REST robot does not take any input. It goes to
Digg.com and loops through the available topics and return one output object per topic. For each topic url, title and parent is returned. Parent is an optional value and is only populated if it is a subtopic (for example "Programming" is a subtopic to "Technology", so for the "Programming" object parent would be set to "Technology"). The robot uses a Global Variable to keep track of what topic that is parent of what subtopic.
Click image to enlarge
This robot is called from a <script> tag:
<script type="text/javascript"
src="http://service.openkapow.com/demo/getdiggtopicsrest.rest?
resultformat=json&json.callbackFunction=parseTopics" />
</script>
The script tag calls the REST robot using the URL to openkapow.com. The parameter resultformat=json specifies that the output of the robot should be in JSON format and the parameter json.callbackFunction=parseTopics specifies that once the robot is done the javascript function parseTopics() should be executed with the robot JSON output as input parameter. This parseTopic() javascript function looks like this:
function parseTopics(jsonResponse){
topics = eval(jsonResponse);
}
So it simply interprets the robot output using the standard javascript eval() function. When the parseTopics() function is done the topics variable contains the whole robot output in javascript object format. That means it is ready to use just as any other javascript object. For example, to get the URL of the first topic it would be done like this:
topics[0].topic.url
The combination openkapow REST robots that sends a JSON output to a javascript callback function is very powerful and can be used in many contexts.
The JavaScript
There is a lot of DOM scripting from withing the Javascript, form fields are added and removed etc. Basically all the removing and adding of configurations are done in the Javascript by removing and adding elements from/to the DOM. The code below is an example of how to add elements to the DOM, in this case a new form input text field is created and added to a new DIV that is then added to the DOM as a child to the FORM.
var form = document.getElementById("configForm");
var div = document.createElement("div");
...
var text = document.createElement("input");
text.setAttribute("type", "text");
text.setAttribute("id", id+"text");
text.setAttribute("maxlength", "50");
text.setAttribute("name", "searchText");
text.setAttribute("class", "searchText");
div.appendChild(text);
...
form.appendChild(div);
Finally the Javascript function createURL() puts together the URL to the RSS robot using the data from the form to create the parameters for the robot.
The RSS robot
There is only one input value to the robot, named
parameters. This input value can take a string containing several search configurations. Each configuration is in the format
"topic:searchText:numberOfItems", for example
"programming:kapow:5" means that the RSS robot should get 5 stories with the keyword "kapow" from the programming topic. The "searchText" is optional, if no search term is specified the parameter for the example would be
"programming::5" instead. Each of these configurations are seperated by ";" (semicolon). This can build up to quite a long string of configurations for the RSS robot to use.
The robot divides the input value into it's individual pieces and for each configuration it seaches Digg for the stories matching the topic and keywords. Of course this robot could be called directly without using this demo to build up the configuration string.
Summary
Basically this demo just assists the user to configure the RSS robot. In doing so it is a good example of how to use REST robots with JSON output and also how to make usefull RSS robots. The RSS robot reuses Digg's search functionality and uses it to present the data the user is interested in, there is no database involved, which makes this demo very lightweight. This demo could easily be expanded to not only do a personal RSS feed from one source (Digg) but also from a number of other sources (
Reddit for example).