Skip to content

Instantly share code, notes, and snippets.

@hammady
Forked from modsaid/download_railscasts.rb
Last active December 15, 2015 22:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save hammady/5330925 to your computer and use it in GitHub Desktop.
Save hammady/5330925 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/ruby
# A script to download the latest episodes from railscasts.com
#
# author: modsaid <mahmoud@modsaid.com>
# mechanize support: Hossam Hammady <github@hammady.net>
# gem install mechanize
require 'rubygems'
require 'mechanize'
RAILS_CASTS_FEED='http://feeds.feedburner.com/railscasts'
DOWNLOAD_DIRECTORY='.'
downloaded_episodes = Dir.glob("#{DOWNLOAD_DIRECTORY}/*.mp4").map {|episode| File.basename(episode, ".mp4")}
puts "Found #{downloaded_episodes.length} episode(s) cached"
puts "Checking railscasts feeds for new episodes..."
agent = Mechanize.new
agent.user_agent_alias = 'Linux Mozilla'
agent.history.max_size = 0 # so that memory usage doesn't get bloated by unnecessary pages
page = agent.get(RAILS_CASTS_FEED)
rss = Nokogiri::XML.parse(page.body, Mechanize::Util::uri_escape(RAILS_CASTS_FEED))
count=0
agent.pluggable_parser.default = Mechanize::Download
(rss/'//item').each do |rss_item|
episode = rss_item.at('./link').text.split('/').last
print "Found episode: #{episode}: "
unless downloaded_episodes.include?(episode)
title = rss_item.at('./title').text
size = rss_item.at('./enclosure/@length').text.to_i/1024/1024
puts "downloading #{size}MB..."
link = rss_item.at('./enclosure/@url')
agent.download(link, "#{DOWNLOAD_DIRECTORY}/#{episode}.mp4.tmp")
File.rename("#{DOWNLOAD_DIRECTORY}/#{episode}.mp4.tmp", "#{DOWNLOAD_DIRECTORY}/#{episode}.mp4") # protect against broken downloads
count += 1
else
puts "cached"
end
end
puts "Done. #{count} new episodes were downloaded"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment