Serverless podcast RSS feed parser

A friend of mine, all the way back from school days who I’ve kept in touch with over the years, runs a podcast about the Korean peninsula — he’s into politics, is an academic type, and has managed to get some really interesting interviews of historical events on the Peninsula; hence why he has a podcast.

The way I got involved is that he needed help to get the information in the RSS feed which is in xml format, into a website.

I don’t know about xml, but I know a few things in Node using the Serverless framework so I though I’d give this one a go.


To begin with, I found a good NPM package to parse rss feeds called Node Podcast Parser. You can install it:

yarn add node-podcast-parser

So here’s how I built the solution for his website— firstly create a working directory and cd into it.

Create a serverless.yml file and paste the following into it:

service: podcast-rss-parser
provider:
    name: aws
    runtime: nodejs6.10
functions:
    hello:
        handler: src/handler.hello
        events:
          - http:
              path: feed
              method: get

Create a directory in the current directory call src/ and create a file called handler.js and paste the following into it:

'use strict';
const request = require('request');
const parsePodcast = require('node-podcast-parser');
function createResponse(statusCode, message) {
return {
statusCode: statusCode,
headers: {
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify(message)
};
}
module.exports.hello = (event, context, callback) => {
request('http://korea-now-podcast.libsyn.com/rss', (err, res, data) => {
if (err) {
console.error('Network error', err);
return;
}
parsePodcast(data, (err, data) => {
if (err) {
console.error('Parsing error', err);
return;
}
console.log(data);
callback(null, createResponse(200, data));
})
});
};

Once that’s done, don’t forget to get the node podcast parser package in the src directory:

yarn add node-podcast-parser

From here you want to deploy the Serverless function by running:

sls deploy

Now once that has run, you should have a url that you can use and it will output a JSON of the RSS feed.

From here, you want to present this in a webpage. To do this go back to your original working directory and create a folder called frontend and cd into that and create an app.js file and paste the following code into it (note: you can update the url in the http.get() with the url you got from your sls function):

'use strict';
angular.module('rssApp', [])
.controller('rssCtrl', ['$scope', '$http', function($scope, $http) {
$http.get("https://vw1lgnll4j.execute-api.us-east-1.amazonaws.com/dev/feed")
.then(function(response) {
$scope.rssData = response.data.episodes;
});
}]);

Then create an index.html file and paste the following code into it:

<!doctype html>
<html lang="en">
<head>
<title>Podcast RSS Feed</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script async src="app.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body>
<div ng-app="rssApp" ng-controller="rssCtrl">
<div class="container">
<div class="row">
<div class="col-md-12" ng-repeat="x in rssData" style="padding-bottom: 20px">
<div class="col-md-4">
<img class="img-responsive" src="{{x.image}}" alt="" width="450" height="300">
</div>
<div class="col-md-8">
<a href="{{x.enclosure.url}}"><h4>{{x.title}}</h4></a>
<p>{{x.published | date :  "fullDate"}}</p>
</div>
</div>
</div>
</div>
</div>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</body>
</html>

Now you should have a nice feed being presented:

If you would like more information on how to do this, you can check out my Github, or if you would like to check out my friends podcast or website, you can do that too — I’m sure you’ll enjoy the stories.

Happy coding!


Peter Hanssens is a Data Engineer / Data Scientist — when he’s not working on making data great again, he’s trying to learn functional programming or get more cloud computing certs.