Categories
Code

Display RSS Feed on a Web Page with Google Feed

You can use the Google Feeds API to fetch and display the content of any RSS feed on a web page. The feed URL can be specified in the div container using HTML5 data attributes.




  
    
    Show RSS Feed
    
    
  
  

  
    
Loading RSS Feed..
Categories
Code

Make an RSS Feed with Google Apps Script

ThinkAmI uses the XMLService of Google Apps Script to create a valid RSS feed that is served to the browser using ContentService with the MIME type set as RSS. Should be handy for creating RSS feeds for services like the Google Search Scraper that do not natively offer feeds.


/* Credit: https://gist.github.com/thinkAmi */

function doGet() {
  
  var rss = makeRss();
  
  rss.setTitle('RSS 2.0 Feed with Google Apps Script');
  rss.setLink('http://example.com');
  rss.setDescription('RSS 2.0 Feed');
  rss.setLanguage('en');
  rss.setAtomlink('http://example.com/rss');
  
  for (var i=1; i < 3; i++) {
    rss.addItem({title: 'TITLE:' + i,
                 link: 'http://example.com/#' + i,
                 description: 'DESCRIPTION: ' + i,
                 pubDate: new Date()
                })
  }
  
  return ContentService.createTextOutput(rss.toString())
  .setMimeType(ContentService.MimeType.RSS);
}

var makeRss = function(){
  var channel = XmlService.createElement('channel');
  var root = XmlService.createElement('rss')
  .setAttribute('version', '2.0')
  .setAttribute('xmlnsatom', "http://www.w3.org/2005/Atom")
  .addContent(channel);
  
  var title = '';
  var link = '';
  var description = '';
  var language = '';
  var atomlink = '';
  var items = {};
  
  var createElement = function(element, text){
    return XmlService.createElement(element).setText(text);
  };
  
  
  return {
    setTitle: function(value){ title = value; },
    setLink: function(value){ link = value; },
    setDescription: function(value){ description = value; },
    setLanguage: function(value){ language = value; },
    setAtomlink: function(value){ atomlink = value; },
    
    addItem: function(args){
      if (typeof args.title === 'undefined') { args.title = ''; }
      if (typeof args.link === 'undefined') { args.link = ''; }
      if (typeof args.description === 'undefined') { args.description = ''; }
      if (!(args.pubDate instanceof Date)) { throw 'pubDate Missing'; }
      if (typeof args.timezone === 'undefined') { args.timezone = "GMT"; }
      if (typeof args.guid === 'undefined' && typeof args.link === 'undefined') { throw 'GUID ERROR'; }
      
      
      var item = {
        title: args.title,
        link: args.link,
        description: args.description,
        pubDate: Utilities.formatDate(args.pubDate, args.timezone, "EEE, dd MMM yyyy HH:mm:ss Z"),
        guid: args.guid === 'undefined' ? args.link : args.link
      }
      
      items[item.guid] = item;
    },
    
    toString: function(){
      channel.addContent(XmlService.createElement("atomlink")
                         .setAttribute('href', atomlink)
                         .setAttribute('rel', 'self')
                         .setAttribute('type', 'application/rss+xml')
                        );
      
      channel.addContent(createElement('title', title));
      channel.addContent(createElement('link', link));
      channel.addContent(createElement('description', description));
      channel.addContent(createElement('language', language));
      
      
      for (var i in items) {
        channel.addContent(
          XmlService
          .createElement('item')
          .addContent(createElement('title', items[i].title))
          .addContent(createElement('link', items[i].link))
          .addContent(createElement('description', items[i].description))
          .addContent(createElement('pubDate', items[i].pubDate))
          .addContent(createElement('guid', items[i].guid))
        );
      }
      
      var document = XmlService.createDocument(root);
      var xml = XmlService.getPrettyFormat().format(document)
      
      var result = xml.replace('xmlnsatom', 'xmlns:atom')
      .replace('';
  $head .= '';
  $head .= '';
  $head .= 'Amazon ';
  $head .= 'http://www.amazon.com';
  $head .= 'Amazon RSS Feed';
   
  $url = "http://www.amazon.com/Best-Sellers-Kindle-Store/zgbs/digital-text/";
   
  $text = file_get_html($url); 

  foreach ($text->find("div.zg_item_compact") as $class) {
    foreach ($class->find('strong.price') as $price) {
      if ($price->plaintext == "Free") {
              $rssfeed .= '';
              foreach ($class->find("div.zg_title a") as $book) { 
                preg_match("/\/dp\/(.*)\/ref/", $book->href, $matches);                
                $ASIN  = trim($matches[1]);
                $url   = "http://www.amazon.com/dp/" . $ASIN . "/?tag=ctrlqorg-20";
                $img   = "http://images.amazon.com/images/P/" . $ASIN . ".01.LZZZZZZZ.jpg";
                $title = htmlentities(trim($book->plaintext));                
                $rssfeed .= '' . $title . '';
                $rssfeed .= '' . $url . '';
                $rssfeed .= '' . $url . '';
                $rssfeed .= '';
              }
              foreach ($class->find("div.zg_byline a") as $author) {
                  $bio  = "http://www.amazon.com" . $author->href . "/?tag=publisherapi-20";
                  $name = htmlentities(trim($author->plaintext));
                  $rssfeed .= 'By ' . $name . '';
              }
              $rssfeed .= '';
              $rssfeed .= '' . $now . '';
              $rssfeed .= '';
          }
      }
  }

  $footer  = '';
  $rssfeed = $head . $rssfeed . $footer;
  $fh      = fopen("amazon.rss", "w");

  fwrite($fh, $rssfeed);
  fclose($fh);

?>
Categories
Code

Parse Stack Exchange RSS Feeds

This Google Script will fetch and parse the XML RSS Feeds of any Stack Exchange website. You can then use the MailApp service to automatically push the content of the RSS feed to another email address on a regular basis. Or you can build an RSS to email service using Google Scripts.


/* Code from https://github.com/nmaxcom/StackExchange-Hot-Questions */
function parseXML() {
  
  var feed = "http://stackexchange.com/feeds";
  
  var xml = UrlFetchApp.fetch(feed).getContentText();
  var root = XmlService.parse(xml).getRootElement();
  var atom = XmlService.getNamespace('http://www.w3.org/2005/Atom');
  
  var entries = root.getChildren('entry', atom);
  
  for (var i = 0; i < entries.length; i++) {
    
    var title = entries[i].getChild('title', atom).getText();
    var categoryElements = entries[i].getChildren('category', atom);
    
    var updated = entries[i].getChild('updated', atom).getValue();
    var url = entries[i].getChild('id', atom).getValue();
    var summary = entries[i].getChild('summary', atom).getText();
    var user = entries[i].getChildren('author', atom)[0].getChild('name', atom).getValue();
    var userURL = entries[i].getChildren('author', atom)[0].getChild('uri', atom).getValue();
    
    var regexSubSite = /http:\/\/(.*?).stackexchange\.com/.exec(url);
    var site, siteURL;
    
    if (!regexSubSite) // If it's not a "subsite"...
    {
      site = /http:\/\/(.*)\.com/.exec(url);
      site = site ? site[1] : "noneFound";      
      siteURL = site ? "http://www." + site + ".com" : "noneFound";
      
    } else {
      site = regexSubSite[1];
      siteURL = regexSubSite[0];
    }
    
    // url     - Question URL 
    // title   - Question Title
    // user    - Question Author
    // userURL - Author Profile URL
    // site    - Stack Exchange Site Name
    // siteURL - SE Site URL
    
  }
}
Categories
Code

Combine Multiple RSS Feeds with Google Scripts

This Google Script takes 2 or more RSS feeds and mashes them up into a JSON feed that you can call from an external app by publishing the script as a web app. Internally, the script uses URLFetchApp to fetch the XML feed and parses it using the Xml service.

This can also be used for single widgets that output data from multiple RSS feeds. You can also mix and mashup RSS feeds with Yahoo Pipes.

function RSSMashup() {
  
  var RSS = [
    "http://feeds.labnol.org/labnol", 
    "http://podgallery.org/feed", 
    "http://hundredzeros.com/feed"
  ];
  
  for (var i=0; i

		
Categories
Code

Parse XML RSS Feeds with Google Scripts

This Google Script will fetch and parse any XML feed and inserts the items as rows in a Google Spreadsheet in reverse chronological order.

Internally, script uses the UrlFetchApp service of Apps Script to fetch the content of the raw XML feed and parses it using the built-in XMLService service. Since spreadsheet data can be externally published as CSV and other formats, this can be extended to convert your RSS feeds into other formats though Google Sheets.


function execute() {
    var url = "http://feeds.labnol.org/labnol";
    main(url);
}

/* Credit: https://gist.github.com/agektmr */
function main(url) {

  var ss = SpreadsheetApp.getActiveSheet();

  var property = PropertiesService.getDocumentProperties();
  var last_update = property.getProperty('last_update');
  last_update = last_update === null ? 0 : parseFloat(last_update);
  
  var feed = UrlFetchApp.fetch(url).getContentText();
  var items = getItems(feed);
  var i = items.length - 1;
  while (i > -1) {
    var item = items[i--];
    var date = new Date(item.getChildText('pubDate'));
    if (date.getTime() > last_update) {
      insertRow(item, sheet);
    }
  }
  property.setProperty('last_update', date.getTime());
}
 
function getItems(feed) {
  var doc = XmlService.parse(feed);
  var root = doc.getRootElement();
  var channel = root.getChild('channel');
  var items = channel.getChildren('item');
  return items;
}
 
function insertRow(item, sheet) {
  var title = item.getChildText('title');
  var url = item.getChildText('link');
  var author = item.getChildText('author');
  var date = new Date(item.getChildText('pubDate'));
  sheet.insertRowBefore(2);
  sheet.getRange('B2:E2').setValues([[title, url, author, date.toLocaleString()]]);
}
Categories
Code

RSS Feed for Podcasts – WordPress Template

You can use your WordPress blog to publish podcasts on iTunes. Most podcasters use FeedBurner to add iTunes specific author information to podcast XML feed but if you prefer to skip FeedBurner, use these feed template.

Create a new page in your WordPress dashboard and give it a custom slug (say podcast). Now create a page-podcast.php file in your WordPress themes folder and copy-paste the following code. Remember to replace information in the highlighted lines.

The permalink of that WordPress Page will become the URL of your Podcast feed which you can now publish to the iTunes Podcasting directory. Here’s a sample Podcast XML feed generated using this RSS template.


/* RSS Feed for iTunes Podcast  */
/* Source: www.labnol.org       */

'; ?>
?>


    
        <? bloginfo_rss('name'); wp_title_rss(); ?>
        
        
        
        no
        
            amit@labnol.org
            Amit Agarwal
        
        Amit Agarwal 
        
        tech, computers, tutorials
        Get the most out of your tech.
        Podcast hosted by Amit Agarwal.
        
            
        
        Creative Commons License        
        25,'tag'=>'podcast')); ?>
        have_posts()) : $q->the_post(); ?>
            
                <? the_title() ?>
                
                
                    
                
                
                    
                
                
                    ]]>
                
                no
                
                
                Amit Agarwal
                computers, tech, tutorials
                
            
        
        
    


Categories
Code

Embed Twitter with RSS Feeds

You use widgets in Twitter for embedding Twitter timelines into your website and this Google Script will convert those widgets into RSS format. Thus you will be able to subscribe to Twitter RSS Feeds in IFTTT, Feedly, or another RSS Reader.


 

function getTweets(id) {
  
  try {
           
    var widget, json, tweets, regex, tweet, list, time, url, when, rss, heading, title, link; 
    
    title = "Twitter RSS Feed :: " + id;
    link  = "http://www.labnol.org/#" + id;       

    // This the ID of your Twitter widget       
    url   = "http://cdn.syndication.twimg.com/widgets/timelines/" + id;
    
    widget  = UrlFetchApp.fetch(url);
    json    = Utilities.jsonParse(widget);   

    // If the Twitter widget doesn't exist, do nothing    
    if ( ! json.body ) {
      return;
    }
       
    // Remove all whitespaces from the Twitter's JSON response
    tweets  = json.body.replace(/\s+/g, ' ');
    
    // Get the Feed Title and URL from the response heading (H1)
    regex = new RegExp(/]*>(.*?)<\/h1>/ig);
    
    if ((heading = regex.exec(tweets)) !== null) {
      
      regex = RegExp(/href="(.*?)"/ig);
      link = regex.exec(heading[1])[1];
      
      regex = RegExp(/title="(.*?)"/ig);
      if ((title = regex.exec(heading[1])) !== null) {
        title = title[1];
      }
    }
    
    rss  = '';
    rss += ' ' + title + '';
    rss += ' ' + link + '';
    rss += ' ';
    rss += ' ' + title + ' :: RSS Feed for Twitter widget #' + id + ' generated by Google Scripts.';
    
    regex = RegExp(/]*>(.*?)<\/ol>/ig);
    
    if ((list = regex.exec(tweets)) !== null) {
      
      // Remove all the extra classes, DIV tags, SPAN tags from the tweets.
      list = list[1].replace(/
]*>(.*?)<\/div>/gi, "") .replace(/]*>(.*?)<\/time>/gi, "") .replace(/data-tweet-id=".*?"|class=".*?"|rel=".*?"|title=".*?"|target=".*?"|data-expanded-url=".*?"|data-query-source=".*?"|dir=".*?"|data-pre-embedded=".*?"/gi, ""); regex = RegExp(/]*>(.*?)<\/li>/ig); while ((tweets = regex.exec(list)) !== null) { tweet = tweets[1].replace(/ /g, ' ').replace(/\s+/g, ' ') .replace(/<\s*(div|span|b|p)[^>]*>/gi, "") .replace(/<\s*\/\s*(div|span|b|p)[^>]*>/gi, ""); // Extract the Date and Time of the tweet time = RegExp(/<\/a>/gi); if ((time = time.exec(tweet)) !== null) { url = time[1]; when = time[2]; tweet = tweet.replace(/]*>\s*<\/a>/gi, ""); rss += ""; rss += " " + url.split("/")[3] + ": " + tweet + ""; rss += " " + when.replace('T', ' ') + ""; rss += " " + url + ""; rss += " " + url + ""; rss += " " + tweet + ""; rss += ""; } } } rss += ""; return rss; } catch (e) { Logger.log(e.toString()); } } function doGet(e) { var cache = CacheService.getPublicCache(); var id = "twitter" + e.queryString; var rss = cache.get(id); if ( ! rss ) { rss = getTweets(e.queryString); cache.put(id, rss, 120); // Expire RSS Feed in 2 minutes } // Use the HTML Service in Google Apps Script to serve Twitter RSS Feeds return ContentService.createTextOutput(rss) .setMimeType(ContentService.MimeType.RSS); }
Categories
Code

Parsing RSS Feeds with Google Feed API

The RSS Search Engine and the Social Sharing Analytics tool use the Google Feed API to fetch and parse RSS Feeds.

Here’s how Google Feed API can be used to fetch any RSS Feeds – it can load a maximum of 20 entries per feed.


// Load the Google JavaScript API
<script type="text/javascript" src="http://www.google.com/jsapi"></script>

<script type="text/javascript">

// Load the Google Feeds API
google.load("feeds", "1");

// Parse the query parameters in the REQUEST URL
function qs(search_for) {
    var query = window.location.search.substring(1);
    var parms = query.split('&');
    for (var i = 0; i < parms.length; i++) {
        var pos = parms[i].indexOf('=');
        if (pos > 0 && search_for == parms[i].substring(0, pos)) {
            return parms[i].substring(pos + 1);
        }
    }
    return "http://feeds.labnol.org/labnol";
}

// The RSS feed is in the "q" variable of the Query String
function initialize() {

    var rss = qs("q") 
    var feed = new google.feeds.Feed(rss);

    // Maximum number of allowed entries is 20
    feed.setNumEntries(20);

    feed.load(function (result) {
        if (!result.error) {
            for (var i = 0; i < result.feed.entries.length; i++) {
              console.log ("Title: " + entry.title);
              console.log ("URL: "   + entry.link);
              console.log ("Date: "  + entry.publishedDate);
              console.log ("HTML: "  + entry.content);
            }
        } else 
              console.log("Google Feed API could not find the RSS Feed"); 
    });
 }
 google.setOnLoadCallback(initialize);
</script>