Filter Zapier RSS feed

My wife (Sarah) has been spending a lot of her time building our her company. In the 21st century this means she is spending most of her time building out her company’s website (that you can see here: Science of Connectedness).

To more directly target her audience, she had a need to take her Instagram posts and send an email once a week through MailChimp, effectively highlighting all the posts that were generated in the previous 7 days. This way her Instagram-less audience can also see what she is up to. The only way to really automate this is via RSS -> MailChimp, meaning we need a way to go from Instagram -> RSS.

After checking out a couple of the free Instagram -> RSS feeds, she arrived on Zapier as it has a lot of really cool customization functionalities. Now we had a way to make her Instagram into a semi-customized RSS feed, however we quickly realized that it would just show all the posts, not just the previous week. Therefore, we needed the ability to filter out the posts and limit the number of pages that are automatically emailed to her followers. Zapier does not appear to have this functionality.

As a quick workaround, I made a little PHP script that can automatically filter out posts when the page is requested:

<?php
    // the below lines are a very insecure way of securing this application. We
    // use it as a way to stop crawlers from hitting this link and burning
    // zapier credits
    $appId = "APPID";
    $suppliedAppId = $_GET['app_id'];
    if (isset($suppliedAppId)) {
      if ($appId <> $suppliedAppId) {
        exit();
      }
    } else {
      exit();
    }
    //limit specifies the number of posts to show in the feed. Default 7
    // days limits the number of days before the run time of the rss php app
    // to be run. Default 7
    $limit = $_GET['limit'];
    $days = $_GET['days'];
    if (!isset($limit)) {
      $limit = 5;
    }
    if (!isset($days)) {
      $days = 7;
    }
    // this allows you to specify the website in the get response
    // recommend putting a default to enable default operation
    $suppliedWebsite = $_GET['rss_feed'];
    if (!isset($suppliedWebsite)) {
      $suppliedWebsite = 'DEFAULTWEBSITE';
    }
    $rss = new DOMDocument();
    $rss->load($suppliedWebsite);
    $feed = array();
    $cnt = 0;
    // This is the header of the new rss feed. This strips out the zapier
    // version, correctly point back to your website.
    $headXML=<<<XML
    <rss version="2.0">
    <channel>
    <title>Science of Connectedness</title>
    <link>https://www.scienceofconnectedness.com</link>
    <description>
    Science of Connectedness RSS Instagram Feed
    </description>
    </channel>
    </rss>
    XML;
    $xml=new SimpleXMLElement($headXML);
    $previousDays = strtotime('-'.$days.' days');
    // loop over each 'item' in the rss feed and keep those that have a 'pubDate'
    // greater than $previousDays
    foreach ($rss->getElementsByTagName('item') as $node) {
        if (strtotime($node->getElementsByTagName('pubDate')->item(0)->nodeValue) >= $previousDays ) {
          $item = $xml->channel->addChild('item', '');
          foreach($node->childNodes as $childNode) {
            $item->addChild($childNode->nodeName, htmlspecialchars($childNode->nodeValue));
          }
        }
        $cnt++;
        if ($cnt >= $limit) {
          break;
        }
    }
    // display the xml
    echo $xml->asXML();
?>

The above script does several things:

  1. It takes an app_id in order to execute. This is not very secure, but as the free level of Zappier is limited in the number of times you can trigger it, we wanted to make sure it did not execute all the time from search engine crawlers or other automated tools.
  2. You can specify the number of posts, the number of days, and the rss feed to use in the URL. This gives the code flexibility for Sarah to tinker without needing me to recode everything.
  3. It removes Zapier branding from the feed. This is useful if you want to share the feed with others.

At a high level this simply wraps the rss feed and loops over the items in that feed until either the count is satisfied or the time limit has been reached. This code is meant to be simple so that you can customize it for your own RSS project.

Find out more here: https://github.com/levilentz/ZapierRSSFilter

Leave a Reply

Your email address will not be published. Required fields are marked *