Downloading Videos From RubyPlus with Ruby (and unix)
When I discovered that I had an account at rubyplus.org and realized how many screencasts they had that were available for my viewing pleasure, I got excited… Real excited!
And then I noticed that I would have to click the download button for each and every one of the videos; I want the world, I want the whole world…
I knew I could use Ruby to help me download every video with ease, so I made an ultra tiny script:
(1..69).each do |episode_number|
`wget http://ldenman:blindedyo@rubyplus.org/episodes/#{episode_number}/download`
end
There were 69 (meow) episodes created from RubyPlus, hence the (1..69).
Surely you know the range method:
(1..10).each {|num| print num} #=> 12345678910
A Range represents an interval—a set of values with a start and an end. Ranges may be constructed using the s..e and s…e literals, or with Range::new. Ranges constructed using .. run from the start to the end inclusively. Those created using … exclude the end value. When used as an iterator, ranges return each value in the sequence.
We are using our range as an iterator
so I specify the number I want to start with, episode 1 and I specify the number I want to end with 69 and then I join those two numbers together with.. (I could use … to join the numbers together, but it would exclude the last number from the output.)
I then send the message each on our range. Now, each takes a block. A block is basically a chunk of code run between a do and an end keyword. so we have a block:
#Block with keywords
(1..5).each do |number|
print number
end
#=>12345
#Block with brackets
(1..5).each {|number| print number}
#=>12345
That’s right, same answer… different syntax.
So, now we look at the code inside the block(line 2):
(1..69).each do |episode_number|
`wget http://ldenman:blindedyo@rubyplus.org/episodes/#{episode_number}/download`
end
For each number in our range, wget will be called with our specified url. And our episode_number will continue to increase until we have reached 69.
I guess I should say that you have to place any console code between these little `grave accents`
GNU Wget is a free software package for retrieving files using HTTP, HTTPS and FTP, the most widely-used Internet protocols. It is a non-interactive commandline tool, so it may easily be called from scripts, cron jobs, terminals without X-Windows support, etc.
Hey! Wget may be easily called from scripts!!! That’s What I Just Did Weeeeeeeee! And It Was Easy, Wasn’t It?!
Then, we finalize the script with our end keyword and then we run it in a directory we want it.
ruby download_all_files.rb
Whoomp There It Is!
Ruby
makes it easy to do stuff like this and it’s all fun and games too!
gregf wrote:
Your script helped a bunch as I really like the screen casts there as well. I had to set my wget line differently than yours. I could not just put my user:pass@rubyplus.org. I needed to go view my cookies in firefox for the site and get the session id from _rcast_session. So mine looked more like this.
Any benefit of using “ over system?
Posted on 01-Jun-08 at 6:31 pm | Permalink
admin wrote:
There is no benefit that I know of.
I actually didn’t know I could use system. Cool.
Posted on 03-Jun-08 at 12:04 pm | Permalink
admin wrote:
Hey gregf,
You could also use %x to fire off commands.
%x(wget http://www.google.com)
According to Benchmark, ’system’ takes the least amount of time.
Posted on 13-Jul-08 at 7:35 am | Permalink
seo blog wrote:
That was a well written article, very intereting,thank you for a good read.
Posted on 03-Aug-08 at 12:38 pm | Permalink