Skip to content

Instantly share code, notes, and snippets.

@saltlakeryan
Last active July 6, 2022 20:24
Show Gist options
  • Save saltlakeryan/5ec887e5598adda627acebf5558db889 to your computer and use it in GitHub Desktop.
Save saltlakeryan/5ec887e5598adda627acebf5558db889 to your computer and use it in GitHub Desktop.
generate podcast feed based on contents of directory
package com.company;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;
public class PodCastFeedGenerator {
private String sourceFileDirectory;
private String destinationXmlFeed;
private String websiteUrlBase;
public static void main(String[] args) throws Exception {
checkArgs(args);
String sourceFiles = args[0];
String destinationXmlFeed = args[1];
String websiteUrlBase = args[2];
PodCastFeedGenerator feedGenerator = new PodCastFeedGenerator(sourceFiles, destinationXmlFeed, websiteUrlBase);
feedGenerator.generateFeed();
}
public static void checkArgs(String[] args) {
if (args.length < 3) {
System.out.println("Please provide args: sourceFiles destinationXml websiteUrlBase");
System.exit(1);
}
}
public PodCastFeedGenerator(String sourceFileDirectory, String destinationXmlFeed, String websiteUrlBase) {
this.sourceFileDirectory = sourceFileDirectory;
this.destinationXmlFeed = destinationXmlFeed;
this.websiteUrlBase = websiteUrlBase;
}
public void generateFeed() throws Exception {
List<File> sourceFiles = listAllFilesInDirectoryWithExtension(sourceFileDirectory, "m4a");
String xmlFeed = generateRssFeedXmlHeader() + generateRssFeedXmlBody(sourceFiles) + generateRssFeedXmlFooter();
writeXmlFeedToFile(xmlFeed, destinationXmlFeed);
}
private void writeXmlFeedToFile(String xmlFeed, String destinationXmlFeed) throws IOException {
FileWriter fileWriter = new FileWriter(destinationXmlFeed);
fileWriter.write(xmlFeed);
fileWriter.close();
}
//takes a directory as a string and returns a list of all files in that directory
//that match the file extension (eg. "mp3")
private List<File> listAllFilesInDirectoryWithExtension(String sourceFileDirectory, String extension) {
File directory = new File(sourceFileDirectory);
File[] files = directory.listFiles();
List<File> matchingFiles = new java.util.ArrayList<File>();
for (File file : files) {
if (file.getName().endsWith(extension)) {
matchingFiles.add(file);
}
}
return matchingFiles;
}
private String generateRssFeedXmlHeader() {
DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
String formattedDate = df.format(new Date());
String rssFeedXml = "<?xml version='1.0' encoding='UTF-8'?>\n" +
"<rss xmlns:atom=\"http://www.w3.org/2005/Atom\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\" xmlns:itunes=\"http://www.itunes.com/dtds/podcast-1.0.dtd\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" version=\"2.0\">\n" +
" <channel>\n" +
" <title>Gorbanvs Great Podcast Name</title>\n" +
" <link>" + this.websiteUrlBase + "</link>\n" +
" <description>PodCast Description</description>\n" +
" <lastBuildDate>" + formattedDate + "</lastBuildDate>\n" ;
return rssFeedXml;
}
private String generateRssFeedXmlBody(List<File> sourceFiles) throws Exception {
String rssFeedXml = "";
String link = "";
long fileLength = 0;
DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
String formattedDate = "";
for (File file : sourceFiles) {
link = java.net.URLEncoder.encode(file.getName(), "UTF-8").replace("+", "%20");
fileLength = file.length();
//get modified date of file
long lastModified = file.lastModified();
//convert long to date
java.util.Date date = new java.util.Date(lastModified);
formattedDate = df.format(date);
rssFeedXml += " <item>\n" +
" <title><![CDATA[" + file.getName() + "]]></title>\n" +
" <pubDate>" + formattedDate + "</pubDate>\n" +
" <guid isPermaLink=\"false\"><![CDATA[" + file.getName() + "]]></guid>\n" +
" <link>" + this.websiteUrlBase + "/" + link + "</link>\n" +
" <description><![CDATA[" + file.getName() + "]]></description>\n" +
" <enclosure url=\"" + this.websiteUrlBase + "/" + link + "\" length=\"" + String.valueOf(fileLength) + "\" type=\"audio/mpeg\" />\n" +
" </item>\n";
}
return rssFeedXml;
}
private String generateRssFeedXmlFooter() {
String rssFeedXml = " </channel>\n" +
"</rss>\n";
return rssFeedXml;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment