Generating an RSS feed for HubPress.io

The first problem I found when using HubPress.io was that it doesn’t support RSS feeds yet. So my first choice was to use a service like Feed 43, and it works. But that service (an others similar) has the limitation of checking the original site every 6 hours. It’s also very fragile because it crawls the home page for posts.

I’m using also Zapier for posting to Twitter and Facebook when a new entry is found on the RSS, so with checking every 6 hours it could happen that my posts were going to be published during the night. I know I could have used Buffer instead, to schedule the publishing at the times I wanted, but being a developer has to be an advantage in your life.

So I decided to create a Groovy script to read the generated HTML output by HubPress, and write an RSS feed to a file that then is pushed to GitHub. If you are using HubPress and want to use this approach, those are the steps:

  • Install Groovy. The easiest way is to use GVM.

  • Go to the master branch of your site repo and create this file:

generateFeed.groovy
@Grab('com.rometools:rome:1.5.0')
@Grab('org.jsoup:jsoup:1.8.2')
import com.rometools.rome.feed.synd.SyndCategory
import com.rometools.rome.feed.synd.SyndCategoryImpl
import com.rometools.rome.feed.synd.SyndContent
import com.rometools.rome.feed.synd.SyndContentImpl
import com.rometools.rome.feed.synd.SyndEntry
import com.rometools.rome.feed.synd.SyndEntryImpl
import com.rometools.rome.feed.synd.SyndFeed
import com.rometools.rome.feed.synd.SyndFeedImpl
import com.rometools.rome.io.SyndFeedOutput
import groovy.io.FileType
import groovy.json.JsonSlurper
import org.jsoup.Jsoup
import org.jsoup.nodes.Document

import java.text.DateFormat
import java.text.SimpleDateFormat

def conf = new JsonSlurper().parse(new File('hubpress/config.json'))

SyndFeed feed = new SyndFeedImpl()
feed.with {
    title = conf.site.title
    link = conf.site.url
    description = conf.site.description
    feedType = 'rss_2.0'
    author = conf.socialnetwork.email
    publishedDate = new Date()
    language = 'en-UK'
}

List<SyndEntry> entries = []
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
df.setTimeZone(TimeZone.getTimeZone('GMT-2'))

new File('2015').eachFileRecurse(FileType.FILES) { File post ->
    SyndEntry entry = new SyndEntryImpl()
    Document document = Jsoup.parse(post, 'UTF-8')

    SyndContent syndDescription = new SyndContentImpl()
    syndDescription.value = document.getElementsByTag('meta').find { it.attr('property') == 'og:description' }.attr('content')

    entry.with {
        title = document.getElementsByTag('meta').find { it.attr('property') == 'og:title' }.attr('content')
        link = document.getElementsByTag('meta').find { it.attr('property') == 'og:url' }.attr('content')
        description = syndDescription
        publishedDate = df.parse(document.getElementsByTag('meta').find { it.attr('property') == 'article:modified_time' }.attr('content'))
        categories = document.getElementsByTag('meta').find { it.attr('property') == 'article:tag' }.attr('content').tokenize('').collect {String tag ->
            SyndCategory category = new SyndCategoryImpl()
            category.name = tag
            return category
        }
    }

    entries << entry
}

feed.entries = entries

Writer writer = new FileWriter('rss');
SyndFeedOutput output = new SyndFeedOutput()
output.output(feed,writer)
writer.close()
  • Generate the RSS feed: groovy generateFeed.groovy

  • Add the resulting file to the repo: git add rss && git commit -m "Updated feed" && git push origin master

Then your feed will be available at http://<REPO>.github.io/rss, in my case, http://alvarosanchez.github.io/rss.

The script speaks by itself, and should work by default, but feel free to modify it.

The final flow to update the feed is the following:

  1. Write a post from the web interface and publish it.

  2. git pull origin master to fetch the generated HTML files.

  3. groovy generateFeed.groovy to generate the updated RSS feed.

  4. git add rss && git commit -m "Updated feed" && git push origin master to publish the feed.

comments powered by Disqus