Created
April 6, 2019 00:41
-
-
Save adamyanalunas/38c9a76ba8f9e070b0fbc7c2b855bafc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
If you want to change the destination folder, that can be provided via
RSS_DOWNLOAD_DESTINATION
.