Need help extracting image src url from a pinterest rss feed

2.4k views Asked by At

Basically I am trying to add a Pinterest feed widget to an ExpressionEngine site. I found this tutorial which was very helpful but the issue is that I can't use the Tag Stripping plugin that is referenced because the site I am helping out with uses ExpressionEngine 1x and the plugin is only for 2x.

If you look at the source of a Pinterest rss feed like: http://pinterest.com/amazon/feed.rss you will see that within the <description> tags is the url for the Pinterest image along with the pin's title, tags and a relative link to the pin on pinterest. I'd like to be able to extract only the value between the double quotes after 'img src'.

The tutorial also uses a plugin called Magpie which is already loaded on the site so I am good there.

Ideally, I'd like my code to look something like this:

{exp:magpie url="http://pinterest.com/amazon/feed.rss" parse="inward"
refresh="720" limit="5"} {items}<a href="{link}" target="_blank"><img
src="<?php code-to-grab-the-img-src->{description} ?>"
alt="{title}"></a>{/items} {/exp:magpie}

Obviously "code-to-grab-the-img-src->{description}" doesn't work so I am looking for a function or something that will. How can I tell Magpie to extract only the img src value without using the SuperGeekery Tag Stripper plugin?

Thanks!

3

There are 3 answers

3
Derek Hogue On BEST ANSWER

I'd suggest building a simple plugin to handle this, you could use the same regex as my other answer (which is failing due to trouble getting the {description} content into PHP).

Here's what your plugin could look like:

<?php

$plugin_info = array(
  'pi_name' => 'Get Pinterest Image',
  'pi_version' => '1.0',
  'pi_author' => 'Derek Hogue',
  'pi_author_url' => 'http://amphibian.info',
  'pi_description' => 'Grabs the image URL from a Pinterest RSS feed description element.',
  'pi_usage' => Get_pinterest_image::usage()
  );

class Get_pinterest_image
{

var $return_data = "";

  function Get_pinterest_image()
  {
    global $TMPL;
    $fallback = $TMPL->fetch_param('fallback_image');
    preg_match("/src=\"(.+)(?=\"&gt;&lt;\/a&gt;)/ui", $TMPL->tagdata, $matches);
    $this->return_data = (!empty($matches)) ? $matches[1] : $fallback;
  }

  function usage()
  {
  ob_start(); 
  ?>
    {exp:get_pinterest_image fallback="/path/to/fallback_image.png"}{description}{/exp:get_pinterest_image}

    Where {description} is the "description" XML node from the Pinterest RSS feed (likely parsed via the Magpie plugin).
  <?php
  $buffer = ob_get_contents();
  ob_end_clean(); 
  return $buffer;
  }  

}

?>

This would, as per EE conventions, be named get_pinterest_image.php, and go into your /plugins/ folder. Then in your template:

{exp:magpie url="http://pinterest.com/amazon/feed.rss" refresh="720" limit="5" parse="inward"}
    {items}
        <a href="{link}" target="_blank"><img src="{exp:get_pinterest_image fallback="/path/to/fallback_image.png"}{description}{/exp:get_pinterest_image}" alt="{title}" /></a>
    {/items}
{/exp:magpie}
8
Derek Hogue On

Sure - enable PHP on output, then use this regex:

{exp:magpie url="http://pinterest.com/amazon/feed.rss" refresh="720" limit="5"}
{items}
<?php
    preg_match("/src=\"(.+)(?=\"&gt;&lt;\/a&gt;)/ui", '{description}', $matches);
    $src = (!empty($matches)) ? $matches[1] : '/path/to/default-image.png';
?>
    <a href="{link}" target="_blank"><img src="<?php echo $src; ?>" alt="{title}"></a>
{/items}
{/exp:magpie}

This code includes a fallback for another image to use in case the image src can't be found for some reason.

Not tested, but it should work.

0
Scott Hepler On

RSS parsing is hard! If you're willing to go a little more complicated and bring the feed content into EE, I've had good luck with the EE2-only DataGrab, so you might find some happiness with the free EE1 predecessor: FeedGrab.