public
Last active

Google Apps Script fetch feed item link

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
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";
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.