-
-
Save hezronobuchele/470a7559c237c87f19b5 to your computer and use it in GitHub Desktop.
This file contains 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/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