Skip to content

Instantly share code, notes, and snippets.

@adamyanalunas
Created April 6, 2019 00:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamyanalunas/38c9a76ba8f9e070b0fbc7c2b855bafc to your computer and use it in GitHub Desktop.
Save adamyanalunas/38c9a76ba8f9e070b0fbc7c2b855bafc to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#coding:utf-8
require "open-uri"
require "fileutils"
destination_path = ENV['RSS_DOWNLOAD_DESTINATION'] || File.expand_path('.')
feed_url = ENV['RSS_FEED']
feed_file = URI.parse(feed_url).open
feed_file.close
feed = File.read(feed_file.path)
file_regex = /((?:https:\/\/)(?:www)?[-a-zA-Z0-9@:%_\+.~#?\/=]+\.mp3)/
matches = feed.scan(file_regex)
matches.each { |url|
path = url[0]
uri = URI.parse(path)
filename = File.basename(uri.path)
destination = "#{destination_path}/#{filename}"
puts "Downloading #{path}"
STDOUT.flush
tempfile = URI.parse(path).open
tempfile.close
FileUtils.mv tempfile.path, destination
puts "Done. Saved to #{destination}"
STDOUT.flush
}
@adamyanalunas
Copy link
Author

A quick script to look for mp3 files in an RSS feed and download them to the same location as the script.

Don’t forget to first make the script executable by running chmod +x feed_muncher.rb.

The RSS feed can be provided via environment variable like:

RSS_FEED=https://domain.com/feed.rss ./feed_muncher.rb

If you want to change the destination folder, that can be provided via RSS_DOWNLOAD_DESTINATION.

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