Skip to content

Instantly share code, notes, and snippets.

@vysogot
Created September 16, 2017 18:13
Show Gist options
  • Save vysogot/52e6bb198a525f2ad32d1528735aceb8 to your computer and use it in GitHub Desktop.
Save vysogot/52e6bb198a525f2ad32d1528735aceb8 to your computer and use it in GitHub Desktop.
Generates RSS file from the files in a folder with subfolders
#!/usr/bin/env ruby -wKU
#
# based on a script
# by Kelan Champagne
# http://kelan.io
require 'date'
# Config values
podcast_title = "Title"
podcast_description = "Description"
public_url_base = "put_the_remote_file_url_here"
# Generated values
date_format = '%a, %d %b %Y %H:%M:%S %z'
podcast_pub_date = DateTime.now.strftime(date_format)
rss_archive_filename = "archive.rss"
rss_output_filename = "new.rss"
# Build the items
items_content = ""
Dir["/Path/To/Files/**/*"].each do |file|
next if file =~ /^\./ # ignore invisible files
next unless file =~ /\.(mp3|m4a)$/ # only use audio files
item_pub_date = File.mtime(file)
previous_pub_date = File.mtime(rss_archive_filename)
next if previous_pub_date > item_pub_date
puts "adding file: #{file}"
item_size_in_bytes = File.size(file).to_s
item_title = File.basename(file, '.m4a')
item_url = "#{public_url_base}"
item_content = <<-HTML
<item>
<title>#{item_title}</title>
<enclosure url="#{item_url}" length="#{item_size_in_bytes}" type="audio" />
<pubDate>#{item_pub_date.strftime(date_format)}</pubDate>
</item>
HTML
items_content << item_content
end
# Build the whole file
content = <<-HTML
<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0">
<channel>
<title>#{podcast_title}</title>
<description>#{podcast_description}</description>
<pubDate>#{podcast_pub_date}</pubDate>
#{items_content}
</channel>
</rss>
HTML
# write it out
output_file = File.new(rss_output_filename, 'w')
output_file.write(content)
output_file.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment