Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Feed Autodiscovery using Javascript, the Google feed API and YQL
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Feed Autodiscovery</title>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
google.load("feeds", "1");
google.load("jquery", "1.6.2");
//---
//http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html
function setup() {
$.extend({
getUrlVars: function () {
var vars = [],
hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
},
getUrlVar: function (name) {
return $.getUrlVars()[name];
}
});
}
//---
function OnLoad() {
setup()
if (!($.getUrlVar('url'))) $('#url').val('http://blog.ouseful.info');
else $('#url').val(unescape($.getUrlVar('url')));
lookupFeed()
}
function yqlAutodisc(){
var yqurl='http://query.yahooapis.com/v1/public/yql/psychemedia/feedautodetect?url='+escape($('#url').val())+'&format=json'
$('#ycontent').html('<em>...trying...</em>')
$.getJSON(yqurl, function(response){
// response is a variable containing the JSON object, if it fetched it successfully
if (!(response.query.count))
$('#ycontent').html('<em>No autodiscoverable feeds...</em>')
else {
$('#ycontent').html('')
$.each(response.query.results.link, function(i,item){
$('#ycontent').append('<div><em>'+item.title+'</em>: <a href="'+item.href+'">'+item.href+'</a></div>')
});
}
})
}
function lookupFeed() {
$('#content').html("<em>...trying...</em>")
// Go find it! Call lookupDone when the search is complete.
var url = $('#url').val()
google.feeds.lookupFeed(url, lookupDone);
yqlAutodisc();
}
function lookupDone(result) {
// Make sure we didn't get an error.
if (!result.error && result.url != null) {
var url = result.url;
// Print the feed found to the page. Note that we only ever get one result
// back from a lookupFeed.
$('#content').html('<a href="' + url + '">' + url + '</a>');
$('#furl').val(url)
lookupFeedHistory()
} else $('#content').html("<em>No feed found...</em>")
}
function feedLoaded(result) {
if (!result.error) {
$('#histcontent').html('')
// Loop through the feeds, putting the titles onto the page.
// Check out the result object for a list of properties returned in each entry.
// http://code.google.com/apis/ajaxfeeds/documentation/reference.html#JSON
for (var i = 0; i < result.feed.entries.length; i++) {
var entry = result.feed.entries[i];
$('#histcontent').append('<div>' + i + ': <a href="'+entry.link+'">' + entry.title + '</a></div>');
}
} else $('#histcontent').html('<em>Something appears to be wrong:-(</em>')
}
function lookupFeedHistory() {
$('#histcontent').html("<em>...trying...</em>")
var url = $('#furl').val()
var feed = new google.feeds.Feed(url);
feed.includeHistoricalEntries(); // tell the API we want to have old entries too
feed.setNumEntries($('#furlmax').val()); // should really noddy check this first for an appropriate value
// Calling load sends the request off. It requires a callback function.
feed.load(feedLoaded);
}
google.setOnLoadCallback(OnLoad);
</script>
</head>
<body style="font-family: Arial;border: 0 none;">
<form action="javascript:lookupFeed()"><span>Try your own URL:</span> <input type="text" size="60" id="url"/> <input id="newgo" type="submit" value="Go"/></form>
<div>Top feed for this page (via Google feed api): <span id="content"></span><br/><br/>
Feeds detected via YQL: <span id="ycontent"></span></div>
<br/>
<form action="javascript:lookupFeedHistory()"><span>Try your own feed URL:</span> <input type="text" size="60" id="furl"/> Items: <input type="text" size="5" id="furlmax" value='10'/> (max 250) <input id="histgo" type="submit" value="Go"/></form>
<div>Historical entries for this feed: <span id="histcontent"></span></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.