Created
May 17, 2012 18:20
-
-
Save mhawksey/2720726 to your computer and use it in GitHub Desktop.
Google Apps Script fetch feed item link
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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