Skip to content

Instantly share code, notes, and snippets.

@j3tm0t0
Last active August 29, 2015 14:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save j3tm0t0/723e73db8017b3f3bb8b to your computer and use it in GitHub Desktop.
Save j3tm0t0/723e73db8017b3f3bb8b to your computer and use it in GitHub Desktop.
Lambdacast ... podcast rss feed creator with AWS Lambda // upload media files into a bucket, and you are done.
console.log 'Lambdacast Started'
aws = require 'aws-sdk'
s3 = new aws.S3
apiVersion: '2006-03-01'
exports.handler = (event, context) ->
console.log 'Received event:'
console.log JSON.stringify event, null, ' '
bucket = event.Records[0].s3.bucket.name
if event.Records[0].s3.object.key is "rss"
return context.done null,"ignore rss update"
s3.listObjects
Bucket:bucket
(err,data)->
if err
console.log err
else
# console.log data
items=data.Contents.sort (a,b)->
return if a.LastModified > b.LastModified then 0 else 1
# console.log items
xml="""
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>Lambdacast</title>
<link>http://lambdacast.s3w.jp</link>
<description>Sample Feed for Lambdacast</description>
"""
for item in items when item.Key isnt "rss"
xml+="""
<item>
<title>#{item.Key}</title>
<link>http://#{bucket}.s3.amazonaws.com/#{item.Key}</link>
<description>#{item.Size} Bytes</description>
<enclosure url="http://#{bucket}.s3.amazonaws.com/#{item.Key}" type="audio/mpeg"/>
<pubDate>#{item.LastModified}</pubDate>
</item>
"""
xml+="""
</channel>
</rss>
"""
# console.log xml
s3.putObject
Bucket:bucket
Key:'rss'
Body:xml
ContentType: 'application/rss+xml'
(err,data) ->
if err
console.log err
context.done err,xml
else
console.log data
context.done null,xml+data
return
(function() {
var aws, s3;
console.log('Lambdacast Started');
aws = require('aws-sdk');
s3 = new aws.S3({
apiVersion: '2006-03-01'
});
exports.handler = function(event, context) {
var bucket;
console.log('Received event:');
console.log(JSON.stringify(event, null, ' '));
bucket = event.Records[0].s3.bucket.name;
if (event.Records[0].s3.object.key === "rss") {
return context.done(null, "ignore rss update");
}
return s3.listObjects({
Bucket: bucket
}, function(err, data) {
var item, items, xml, _i, _len;
if (err) {
return console.log(err);
} else {
items = data.Contents.sort(function(a, b) {
if (a.LastModified > b.LastModified) {
return 0;
} else {
return 1;
}
});
xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<rss version=\"2.0\">\n<channel>\n <title>Lambdacast</title>\n <link>http://lambdacast.s3w.jp</link>\n <description>Sample Feed for Lambdacast</description>\n";
for (_i = 0, _len = items.length; _i < _len; _i++) {
item = items[_i];
if (item.Key !== "rss") {
xml += "<item>\n <title>" + item.Key + "</title>\n <link>http://" + bucket + ".s3.amazonaws.com/" + item.Key + "</link>\n <description>" + item.Size + " Bytes</description>\n <enclosure url=\"http://" + bucket + ".s3.amazonaws.com/" + item.Key + "\" type=\"audio/mpeg\"/>\n <pubDate>" + item.LastModified + "</pubDate>\n</item>\n";
}
}
xml += "</channel>\n</rss>";
return s3.putObject({
Bucket: bucket,
Key: 'rss',
Body: xml,
ContentType: 'application/rss+xml'
}, function(err, data) {
if (err) {
console.log(err);
return context.done(err, xml);
} else {
console.log(data);
return context.done(null, xml + data);
}
});
}
});
};
return;
}).call(this);
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>Lambdacast</title>
<link>http://lambdacast.s3w.jp</link>
<description>Sample Feed for Lambdacast</description>
<item>
<title>n74.mp3</title>
<link>http://lambdacast.s3.amazonaws.com/n74.mp3</link>
<description>3784012 Bytes</description>
<enclosure url="http://lambdacast.s3.amazonaws.com/n74.mp3" type="audio/mpeg"/>
<pubDate>Sun Nov 30 2014 10:46:33 GMT-0800 (PST)</pubDate>
</item>
<item>
<title>sample_mpeg4.mp4</title>
<link>http://lambdacast.s3.amazonaws.com/sample_mpeg4.mp4</link>
<description>245779 Bytes</description>
<enclosure url="http://lambdacast.s3.amazonaws.com/sample_mpeg4.mp4" type="audio/mpeg"/>
<pubDate>Sun Nov 30 2014 10:29:46 GMT-0800 (PST)</pubDate>
</item>
<item>
<title>n25.mp3</title>
<link>http://lambdacast.s3.amazonaws.com/n25.mp3</link>
<description>2767883 Bytes</description>
<enclosure url="http://lambdacast.s3.amazonaws.com/n25.mp3" type="audio/mpeg"/>
<pubDate>Sun Nov 30 2014 10:27:00 GMT-0800 (PST)</pubDate>
</item>
<item>
<title>n46.mp3</title>
<link>http://lambdacast.s3.amazonaws.com/n46.mp3</link>
<description>4486029 Bytes</description>
<enclosure url="http://lambdacast.s3.amazonaws.com/n46.mp3" type="audio/mpeg"/>
<pubDate>Sun Nov 30 2014 10:23:45 GMT-0800 (PST)</pubDate>
</item>
</channel>
</rss>
@skylans
Copy link

skylans commented Apr 23, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment