How to create a news binder on your site

Who I am
Martí Micolau
@martimicolau
Author and references

The presence of new updated content is a determining factor for the success of a site. Creating and updating the content constantly is, however, not an easy operation and not without costs.

Among the things that can be done with the use of Google Api, there is to provide our site with one or more news collectors, through research and publication of RSS feeds made available by the most disparate sites. By resorting to the search engine loader, we can read a number of news, videos and blog comments based on a geographic location, etc.



In short, we can enrich the home page with fresh news from any area, area or date.

Clicking on a specific feed leads to a Google page (Add to Google) where it is possible to use the green link to be used as a parameter to be passed to the Google API search function.

We ask Google for a key

In order to use the Google APIs, you must have a Google account (Gmail is fine too) and go to the registration area at the address http://code.google.com/intl/it-IT/apis/loader/signup.html, and type the address relative to the page where we want to exploit the API. We thus obtain an alphanumeric key 86 characters long to be used in the script that we must implement to create our news container.

Together with the key, the service also provides a script, to be used on the page where we want to collect the news, with which to search and publish news, videos and comments based on geographical location.

Let's examine the code provided

Let's focus on the code of the OnLoad () function:


function Onload(){


// Create a search control
var searchControl=new google.search.SearchControl()

// add a full set for search (web, video, blog)
var localSearch = new google.search.LocalSearch();

searchControl.addSearcher(localSearch);
searchControl.addSearcher(new google.search.WebSearch());
searchControl.addSearcher(new google.search.VideoSearch());
searchControl.addSearcher(new google.search.BlogSearch());

// Establish a geo-referenced location for the search
localSearch.setCenterPoint(“New York,NY”);

// Tell Google the name of the page element (usually a

that greases from container
searchControl.draw(document.getElementById(“searchcontrol”));

// Perform the search
searchControl.execute(“Google”);
}

// Make the function call (Ajax)
google.setOnLoadCallback(OnLoad);
 

We can see how, with a few instructions, we have obtained the main body of a potential script to be used to create a news container.

A custom container

In addition to geo-referenced search, there are other ways to use Google APIs to collect news. For example, we can ask the engine to read the content of a feed of a specific topic and published on a particular site. Let's see below how to conceive a script, to be implemented in an Html page or inside a container, to be used on our home page to enrich it with interesting and always fresh news.

We can use as a starting point, the header of the script contained in the email, making use of our API-Key:

“type=”text javascript”>

If we access our Google account through an SSL connection, we must use the Https protocol instead of Http. The second tag:

google.load(“feeds”,”1″);

Make the call to the Google API in their version 1.


The heart of the Script

Let's analyze the main function of the script, which we are going to conceive. We can call her Onload() as in the example provided with the API Key. The following line of code:


var attributes=["title","link","publishedDate","contentSnippet"];

Defines the attributes of the published feeds that the Google engine will have to collect and return to our search, the title, the link to the content, the publication date, the summary (see box Lo standard Rss). The line:

var RSSfeed = new google.feeds.Feed(“http://rss.feedportal.com/c/32276/f/566673/index.rss”)

Create a container that we initialize, through the method = new google.feeds.Feed to which we pass, as a parameter, the address where the feed we are interested in is located.

With code

RSSfeed.setNumEntries (3);

We ask the engine to return the first three results, which are returned via the call:


feed.load(function(result)

 

A container for the results

The display of the news must necessarily take place within an element of the page where the script resides. An element is generally used for this purpose :

var container=document.getElementById(“RSSfeed”);

The name entered (RSSfeed) is the Id that we must assign, through an appropriate tag, to the element that acts as a container (for example ).

We organize the News

The completion of the search occurs through the creation of a cycle (for (var i = 0; i <result.feed.entries.length; i ++)), based on the value previously set for the number of results, within which we must collect and organize the information fished by the engine. Through the method document.createElement we declare variables that we will need for the construction of the Html code, generated by the script, necessary to view the news. Generating this code is done with the method appendChild of each variable.


The use of the Google API takes place through an Ajax call (google.setOnLoadCallback(OnLoad)) to which we pass the name of the main function of the script as a parameter.





google.load(“feeds”,”1″);

function On Load(){

var attributes=[“title”,”link”,”publishedDate”,”contentSnippet”];
var RSSfeed=new google.feeds.Feed(“http://rss.feedsportal.com/c/32276/f/566673/index.rss”);
RSSfeed.setNumEntries (3);
feed.load(function(result){

if(!result.error){

var container=document.getElementById(“RSSfeed”);

for (var i = 0; i

add a comment of How to create a news binder on your site
Comment sent successfully! We will review it in the next few hours.