Create RSS Feed for iTunes | Part 2

Posted By : Archna Dhingra | 23-Jul-2014

In this blog we’ll learn how to create an RSS feed for iTunes in grails. To know about what is an RSS feed and how it looks like, you can refer Create RSS Feed for iTunes | Part-1.

 

To start with, create a controller named FeedController.groovy. Now create an action here which will create the XML for your RSS feed using XML Markup Builder.


FeedController.groovy

def createRSSFeed() {
   def writer = new StringWriter()
   def xml = new MarkupBuilder(writer)
   xml.mkp.xmlDeclaration(version:'1.0', encoding:'UTF-8')
   JSONObject channelDetails = getiTunesChannelDetails()
   JSONObject itemDetails = getItemDetails()
   xml.rss('xmlns:itunes':"http://www.itunes.com/dtds/podcast-1.0.dtd", version:"2.0"){
      iTunesRSSService.createChannelTag(xml, writer, channelDetails, itemDetails)
   }
   render(text: writer.toString(), contentType: "text/xml")
}

Next we need to create a service named ITunesRSSService.groovy which will contain different methods, each creating specific xml tags for the RSS feed.


ITunesRSSService.groovy

def createChannelTag(def xml, def writer, JSONObject channelDetails, JSONObject itemDetails){
   xml.channel {
      titleTag(xml, writer, channelDetails.channelName)
      descriptionTag(xml, writer, channelDetails.channelDescription)
      linkTag(xml, writer, channelDetails.websiteUrl)
      languageTag(xml, writer, channelDetails.language)
      copyrightTag(xml, writer, channelDetails.copyright)
      itunesAuthorTag(xml, writer, channelDetails.author)
      itunesSummaryTag(xml, writer, channelDetails.channelDescription)
      itunesOwnerTag(xml, writer, channelDetails.owner)
      itunesKeywordsTag(xml, writer, channelDetails.keywords)
      itunesCategoryTag(xml, writer, channelDetails.category, channelDetails.subCategory)
      itunesImageTag(xml, writer, channelDetails.podcastCoverUrl)
      itunesExplicitTag(xml, writer, channelDetails.explicit)
      itunesBlockTag(xml, writer, channelDetails.block)
      channelDetails.episodes.each{ episode ->
         createItemTag(xml, writer, itemDetails, episode)
      }
   }
   return writer
}

def createItemTag(def xml, def writer, JSONObject itemDetails, def episode){
   xml.item{
      titleTag(xml, writer, episode.title)
      descriptionTag(xml, writer, episode.description)
      linkTag(xml, writer, episode.link)
      enclosureTag(xml, writer, episode.link, episode.fileSize, episode.fileType)
      guidTag(xml, writer, episode.link)
      publishedDateTag(xml, writer, episode.dateCreated)
      itunesAuthorTag(xml, writer, episode.author)
      itunesSummaryTag(xml, writer, episode.description)
      itunesDurationTag(xml, writer, episode.duration)
      itunesKeywordsTag(xml, writer, episode.keywords)
      itunesImageTag(xml, writer, episode.episodeCoverUrl)
      itunesExplicitTag(xml, writer, episode.explicit)
      itunesBlockTag(xml, writer, episode.block)
   }
   return writer
}

def titleTag(def xml, def writer, def title){
   xml.title(title)
   return writer
}

def descriptionTag(def xml, def writer, def description){
   xml.description(description)
   return writer
}

def linkTag(def xml, def writer, def link){
   xml.link(link)
   return writer
}

def languageTag(def xml, def writer, def language){
   xml.language(language)
   return writer
}

def copyrightTag(def xml, def writer, def copyright){
   xml.copyright(copyright)
   return writer
}

def enclosureTag(def xml, def writer,  def mediaUrl, def length, def type){
   String mediaType = "video/"+type
   xml.enclosure(url:mediaUrl, length:length, type:mediaType)
}

def guidTag(def xml, def writer, def guid){
   xml.guid(guid)
   return writer
}

def publishedDateTag(def xml, def writer, Date pubDate){
   String pubDateFormatted = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US).format(new Date());
   xml.pubDate(pubDateFormatted)
   return writer
}

def itunesDurationTag(def xml, def writer, def duration){
   String time
   String h = String.format("%02d", TimeUnit.SECONDS.toHours(duration))
   String m = String.format("%02d", TimeUnit.SECONDS.toMinutes(duration) - (TimeUnit.SECONDS.toHours(duration)* 60))
   String s = String.format("%02d", TimeUnit.SECONDS.toSeconds(duration) - (TimeUnit.SECONDS.toMinutes(duration)* 60))
   if(h == "00" && m != "00" && s != "00")
      time = m+":"+s
   else if(h == "00" && m == "00" && s != 0)
      time = s
   else if(h == "00" && m == "00" && s == "00")
      time = ""
   else
      time = h+":"+m+":"+s
   xml.'itunes:duration'(time)
   return writer
}

def itunesAuthorTag(def xml, def writer, def author){
   xml.'itunes:author'(author)
   return writer
}

def itunesSummaryTag(def xml, def writer, def summary){
   xml.'itunes:summary'(summary)
   return writer
}

def itunesOwnerTag(def xml, def writer, def owner){
   String ownerName = owner.firstName+" "+owner.lastName
   String ownerEmailAddress = owner.email
   xml.'itunes:owner'{
      'itunes:name'(ownerName)
      'itunes:email'(ownerEmailAddress)
   }
   return writer
}

def itunesKeywordsTag(def xml, def writer, List keywords){
   String keys = keywords.toString()
   xml.'itunes:keywords'(keys.substring(1,keys.indexOf("]")))
      return writer
}

def itunesCategoryTag(def xml, def writer, def category, def subCategory){
   xml.'itunes:category'(text:category){
      if(subCategory != null && subCategory != "")
         itunesSubCategoryTag(xml, writer, subCategory)
   }
   return writer
}

def itunesSubCategoryTag(def xml, def writer, def subCategory){
   xml.'itunes:category'(text:subCategory)
   return writer
}

def itunesImageTag(def xml, def writer, def imageUrl){
   xml.'itunes:image'(href:imageUrl)
   return writer
}

def itunesExplicitTag(def xml, def writer, def explicit){
   String explicitTag = explicit == true ? "Yes" : "No"
   xml.'itunes:explicit'(explicitTag)
   return writer
}

def itunesBlockTag(def xml, def writer, def block){
   String blockTag = block == true ? "Yes" : "No"
   xml.'itunes:block'(blockTag)
   return writer
}

 

Thanks,

Archna Dhingra.

About Author

Author Image
Archna Dhingra

Archna is a bright Groovy and Grails developer and has worked on development of various SaaS applications using Grails framework.

Request for Proposal

Name is required

Comment is required

Sending message..