Skip to content

Instantly share code, notes, and snippets.

@mhawksey
Created May 17, 2012 18:20
Show Gist options
  • Save mhawksey/2720726 to your computer and use it in GitHub Desktop.
Save mhawksey/2720726 to your computer and use it in GitHub Desktop.
Google Apps Script fetch feed item link
function fetchUrlfromRSS(url) {
var cache = CacheService.getPublicCache(); // using Cache service to prevent too many urlfetch
var cached = cache.get(url);
if (cached != null) { // if value in cache return it
return cached;
}
// otherwise build urlfetch
var options = {"method" : "get"};
try {
var response = UrlFetchApp.fetch(url , options);
var doc = Xml.parse(response.getContentText(),true); // parse content as xml
if (doc.rss.channel.item != undefined ){ // if item is defined get link
// Note using item instead of item.link because urls appear to be slipping out of schema
var res = doc.rss.channel.item.getText().replace(/^\s+|\s+$/g, '');
cache.put(url, res, 3600); // put result in cache for next time
return res;
}
} catch (e){
return "NONE";
}
return "NONE";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment