Skip to content

Instantly share code, notes, and snippets.

@haraldmartin
Last active October 5, 2017 19:08
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 haraldmartin/1bd71041d0489b644332762b971302e7 to your computer and use it in GitHub Desktop.
Save haraldmartin/1bd71041d0489b644332762b971302e7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'tempfile'
require 'open-uri'
# Settings
SUBDOMAIN = "mypodcast"
DOWNLOAD_DIR = "downloads"
# Opinion urls
PODCAST_URL = "http://#{SUBDOMAIN}.madewithopinion.com"
FEED_URL = "http://madewithopinion.com/feeds/#{SUBDOMAIN}/rss/"
# New urls
NEW_FEED_FILE = "feed.rss"
NEW_ASSETS_URL = "https://my-podcast.s3.amazonaws.com/assets"
NEW_PODCAST_URL = "http://my-domain.se/my-podcast"
NEW_FEED_URL = "https://my-podcast.s3.amazonaws.com/feed.rss"
MATCH_ASSETS_RE = %r{
(
https?://
[^"< ]+?/
([^/]+
\.(?:m4a|jpg)
))
}x
def create_download_dir
puts "Creating download dir"
Dir.mkdir(File.join(__dir__, DOWNLOAD_DIR))
rescue Errno::EEXIST
# All good
end
def download_assets(contents)
urls = contents.scan(MATCH_ASSETS_RE).to_a.map { |e| e[0] }.join("\n")
file = Tempfile.new('urls')
begin
file.write(urls)
puts "Downloading all audio and image files. This might take a while…"
puts "You can watch the progress using the \`watch\` command in another Terminal window, like:"
puts "\`watch 'ls #{DOWNLOAD_DIR} | wc -l\`"
Dir.chdir(DOWNLOAD_DIR) do
`xargs -P 10 -n 1 curl -sOL < #{file.path}`
end
puts "Done, all downloaded"
ensure
file.close
file.unlink
end
end
def create_new_feed_file(contents)
new_feed = contents
.sub(FEED_URL, NEW_FEED_URL)
.gsub(PODCAST_URL, NEW_PODCAST_URL)
.gsub(MATCH_ASSETS_RE, NEW_ASSETS_URL + '/\2')
puts "Writing new feed file"
File.write(NEW_FEED_FILE, new_feed)
end
open(FEED_URL) do |f|
contents = f.read()
create_download_dir()
download_assets(contents)
create_new_feed_file(contents)
end
puts "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment