0

I am trying to pull posts from my RSS feed into an email via Ampscript. I have the code correct and it's pulling the right number of stories. I just want to change the number of stories the script returns to only show items that have been published in the last 7 days.

Here is what I have working now. It just returns the last 5 stories.

%%[Var @xml, @imaged, @titles, @title, @descs, @desc, @links, @link, @cnt
Set @xml = HTTPGet("https://rss.com/feed")
Set @titles = BuildRowsetFromXML(@xml,"//item/title",1)
Set @descs = BuildRowsetFromXML(@xml,"//item/description",1)
Set @links = BuildRowsetFromXML(@xml,"//item/link",1)


If RowCount(@titles) > 5 THEN
SET @rows = 5
ELSE
SET @rows = RowCount(@titles)
ENDIF

IF @rows >= 1 THEN
for @cnt = 1 to @rows do
Set @title = Field(Row(@titles,@cnt),"Value")
Set @desc = Field(Row(@descs,@cnt), "Value")
Set @link = Field(Row(@links,@cnt), "Value") ]%%



<head>
<style type="text/css">
.auto-style1 {
 text-align: left;
}
.auto-style2 {
 color: #000000;
}
.auto-style3 {
 font-size: 14px;
}
.auto-style4 {
 text-align: left;
 font-size: 20px;
}
</style>
</head>

<div style="border: 1px solid #444; background-color: #F7F7F7; margin: 0.76em 0; padding: 0.76em;">
<h1 style="font-style: normal; font-variant: normal; font-weight: bold; line-height: normal; font-family: Arial, Helvetica, sans-serif;" class="auto-style4"><a href="%%=RedirectTo(@link)=%%" alias="%%=v(@title)="%%" title="%%=v(@title)=%% style="color: #000;">
<span class="auto-style2">%%=v(@title)=%%</span></a></h1>
 <div class="auto-style1">
<span style="color: #444; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; font-family: Arial, Helvetica, sans-serif;" class="auto-style3">%%=v(@desc)=%%</span>
 </div>
</div>
%%[ 
NEXT @cnt 
ENDIF
]%%

1 Answer 1

1

There are a couple of examples of parsing and looping through an RSS feed floating around SFSE. The example in the official docs is super-fragile and confusing, IMHO.

Here's one that includes a count and date limit:

<html>
<head>
  <style>
    body, a, input {font-family:sans-serif;}
  </style>
</head>
<body style="font-family:sans-serif">
%%=now(1)=%%

%%[

var @xml, @isXML, @nodes, @rowCount
set @xml = HTTPGet("https://rss.com/feed", false, 0, @CallStatus)

/* strip out invalid elements if needed */
/* set @xml = Replace(@xml, "dscblog:","") */

output(concat("<br>Call Status: ", @CallStatus))

if indexOf(@xml,"<channel>") > 0 then

  set @nodes = BuildRowsetFromXML(@xml,"/rss/channel/item",0)
  set @rowCount = rowcount(@nodes)

  output(concat("<br>rowCount: ", @rowCount))

  set @rowCount = iif(@rowCount > 5, 5, @rowCount)

  output(concat("<br>rowCount (adj): ", @rowCount))

  if @rowCount > 0 then

    for @i = 1 to @rowCount do

      var @pubDate
      var @title
      var @description
      var @link

      set @nodepath = concat("/rss/channel/item[",@i,"]/")

      if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,"pubDate"))) > 0 then
          set @pubDate = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,"pubDate"),0),1),'Value')
          set @pubDate = SystemDateToLocalDate(@pubDate)
      endif 

      if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,"title"))) > 0 then
          set @title = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,"title"),0),1),'Value')
      endif

      if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,"description"))) > 0 then
          set @description = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,"description"),0),1),'Value')
      endif

      if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,"link"))) > 0 then
          set @link = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,"link"),0),1),'Value')
      endif


      if @pubDate >= DateAdd(Now(1), -14, "D") then

        ]%%

         <h4>%%=v(@title)=%%</h4>
         %%=v(@pubDate)=%%
         <p>
           %%=v(@description)=%%
           <br><a href="%%=redirectto(@link)=%%">%%=v(@link)=%%</a>
         </p>


        %%[

      endif

    next @i

  else

   outputline(concat("<br>no items found"))

  endif

else

  outputline(concat("<br>no XML found"))

endif

]%%

<br><br><br><a href="%%profile_center_url%%">Profile/Preference Center</a>
<br><a href="%%subscription_center_url%%">Subscription Center</a>
<br><a href="%%unsub_center_url%%">One-Click Unsubscribe</a>
<p>This email was sent by:
<b>%%Member_Busname%%</b>
<br>%%Member_Addr%%, %%Member_City%%, %%Member_State%%  %%Member_PostalCode%%  %%Member_Country%%</p>
<custom name="opencounter" type="tracking">
</body>
</html>

Output

output screenshot

6
  • The example above is using a 14-day cutoff date since there weren't any posts on that feed in the last 7 days. Sep 17, 2019 at 17:50
  • Sorry I know this is now rather old, I'm trying to use this is in a content block but it doesn't seem to load anything. (I've played with different feeds, and tried in a block - which shows the top few lines of the code but nothing else - or within the email which showed nothing.....if anyone sees and has ideas I'd love to get it working?
    – sjgknight
    Oct 27, 2021 at 10:16
  • Doesn't look like that rss.com feed is valid anymore. You could just substitute another one -- like lorem-rss.herokuapp.com/feed or something. Oct 27, 2021 at 14:10
  • Thanks @adam-spriggs sorry I should've said I did replace that feed with a couple of different ones, and I've tried the one you suggest (thank you). If I create a standalone block,the 'preview' shows part of the code, if I insert it as a snippet in an email the preview shows nothing. Struggling with ideas for troubleshooting!
    – sjgknight
    Oct 27, 2021 at 23:24
  • Please post a new question that includes the RSS feed and relevant code. Oct 28, 2021 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.