# Downloading an MP3 from the internet

**URL:** <https://rubytalk.org/t/downloading-an-mp3-from-the-internet/51144>\
**Category:** ruby-talk\
**Created:** [14 January 2009 01:15 UTC](https://rubytalk.org/t/downloading-an-mp3-from-the-internet/51144 "2009-01-14T01:15:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathaniel\_Escribano](https://avatars.discourse-cdn.com/v4/letter/n/96bed5/32.png) [@Nathaniel\_Escribano](https://rubytalk.org/u/Nathaniel_Escribano)\
**Post date:** [14 January 2009 01:15 UTC](https://rubytalk.org/t/downloading-an-mp3-from-the-internet/51144/1 "2009-01-14T01:15:39Z")

</div>

I am trying to write a simple script that downloads a music file from the  
internet.  
For example: if i want to download  
[http://cdn.stereogum.com/mp3/My%20Morning%20Jacket%20-%20Celebration%20(Live).mp3](http://cdn.stereogum.com/mp3/My%20Morning%20Jacket%20-%20Celebration%20(Live).mp3)  
how exactly would you do it?

I found someone who was able to download a flickr photo using this script:

require 'net/http'

Net::HTTP.start("[farm1.static.flickr.com](http://farm1.static.flickr.com)") { |http|  
&nbsp;&nbsp;&nbsp;&nbsp;resp = http.get("/92/218926700\_ecedc5fef7\_o.jpg")  
&nbsp;&nbsp;&nbsp;&nbsp;open("fun.jpg", "wb") { |file|  
&nbsp;&nbsp;&nbsp;&nbsp;file.write(resp.body)  
&nbsp;&nbsp;&nbsp;&nbsp;}  
}  
puts 'Downloaded'

With a slight modification shouldn't this work for the music file?  
What makes downloading a photo any different than an mp3 file?

require 'net/http'

Net::HTTP.start("[cdn.stereogum.com](http://cdn.stereogum.com)") { |http|  
&nbsp;&nbsp;&nbsp;&nbsp;resp = http.get("/mp3/My Morning Jacket - Celebration (Live).mp3")  
&nbsp;&nbsp;&nbsp;&nbsp;open("Celebration.mp3", "wb") { |file|  
&nbsp;&nbsp;&nbsp;&nbsp;file.write(resp.body)  
&nbsp;&nbsp;&nbsp;&nbsp;}  
}  
puts "the song has been downloaded"

Why doesn't it work? What needs to be changed?  
Any help would be great! Thanks.

---

<div class="post-metadata">

**Author:** ![Andrew\_Timberlake](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@Andrew\_Timberlake](https://rubytalk.org/u/Andrew_Timberlake)\
**Post date:** [14 January 2009 03:15 UTC](https://rubytalk.org/t/downloading-an-mp3-from-the-internet/51144/2 "2009-01-14T03:15:16Z")

</div>

Probably because you're not encoding the path

> **···**
>
> On Wed, Jan 14, 2009 at 3:15 AM, Nathaniel Escribano \<americaskate@gmail.com \> wrote:
> 
> > I am trying to write a simple script that downloads a music file from the  
> > internet.  
> > For example: if i want to download
> > 
> > [http://cdn.stereogum.com/mp3/My%20Morning%20Jacket%20-%20Celebration%20(Live).mp3&lt;http://cdn.stereogum.com/mp3/My%20Morning%20Jacket%20-%20Celebration%20(Live).mp3&gt](http://cdn.stereogum.com/mp3/My%20Morning%20Jacket%20-%20Celebration%20(Live).mp3&lt;http://cdn.stereogum.com/mp3/My%20Morning%20Jacket%20-%20Celebration%20%28Live%29.mp3&gt);  
> > how exactly would you do it?
> > 
> > I found someone who was able to download a flickr photo using this script:
> > 
> > require 'net/http'
> > 
> > Net::HTTP.start("farm1.static.flickr.com") { |http|  
> > &nbsp;&nbsp;&nbsp;resp = http.get("/92/218926700\_ecedc5fef7\_o.jpg")  
> > &nbsp;&nbsp;&nbsp;open("fun.jpg", "wb") { |file|  
> > &nbsp;&nbsp;&nbsp;file.write(resp.body)  
> > &nbsp;&nbsp;&nbsp;}  
> > }  
> > puts 'Downloaded'
> > 
> > With a slight modification shouldn't this work for the music file?  
> > What makes downloading a photo any different than an mp3 file?
> > 
> > require 'net/http'
> > 
> > Net::HTTP.start("cdn.stereogum.com") { |http|  
> > &nbsp;&nbsp;&nbsp;resp = http.get("/mp3/My Morning Jacket - Celebration (Live).mp3")  
> > &nbsp;&nbsp;&nbsp;open("Celebration.mp3", "wb") { |file|  
> > &nbsp;&nbsp;&nbsp;file.write(resp.body)  
> > &nbsp;&nbsp;&nbsp;}  
> > }  
> > puts "the song has been downloaded"
> > 
> > Why doesn't it work? What needs to be changed?  
> > Any help would be great! Thanks.
> 
> --  
> Andrew Timberlake  
> [http://ramblingsonrails.com](http://ramblingsonrails.com)  
> [http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)
> 
> "I have never let my schooling interfere with my education" - Mark Twain

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [14 January 2009 09:30 UTC](https://rubytalk.org/t/downloading-an-mp3-from-the-internet/51144/3 "2009-01-14T09:30:13Z")

</div>

Nathaniel Escribano wrote:

> require 'net/http'
> 
> Net::HTTP.start("cdn.stereogum.com") { |http|  
> &nbsp;&nbsp;&nbsp;&nbsp;resp = http.get("/mp3/My Morning Jacket - Celebration (Live).mp3")  
> &nbsp;&nbsp;&nbsp;&nbsp;open("Celebration.mp3", "wb") { |file|  
> &nbsp;&nbsp;&nbsp;&nbsp;file.write(resp.body)  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> }  
> puts "the song has been downloaded"

Here is an alternative way of doing it:

&nbsp;&nbsp;require 'open-uri'  
&nbsp;&nbsp;open("[http://cdn.stereogum.com/mp3/My](http://cdn.stereogum.com/mp3/My) Morning Jacket - Celebration  
(Live).mp3") do |src|  
&nbsp;&nbsp;&nbsp;&nbsp;open("Celebration.mp3","wb") do |dst|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while blk = src.read(65536)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dst.write(blk)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

This gives a clear error:

/usr/lib/ruby/1.8/uri/common.rb:436:in `split': bad URI(is not URI?):  
[http://cdn.stereogum.com/mp3/My](http://cdn.stereogum.com/mp3/My) Morning Jacket - Celebration (Live).mp3  
(URI::InvalidURIError)

Fix the URI and it works:

&nbsp;&nbsp;...  
&nbsp;&nbsp;open("[http://cdn.stereogum.com/mp3/My%20Morning%20Jacket%20-%20Celebration%20(Live).mp3&quot;\](http://cdn.stereogum.com/mp3/My%20Morning%20Jacket%20-%20Celebration%20%28Live%29.mp3&quot;%5C))  
do |src|  
&nbsp;&nbsp;...

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Nathaniel\_Escribano](https://avatars.discourse-cdn.com/v4/letter/n/96bed5/32.png) [@Nathaniel\_Escribano](https://rubytalk.org/u/Nathaniel_Escribano)\
**Post date:** [14 January 2009 16:03 UTC](https://rubytalk.org/t/downloading-an-mp3-from-the-internet/51144/4 "2009-01-14T16:03:47Z")

</div>

Thank you guys very much!  
I was able to get it to work by fixing my path.  
I wasn't able to get it to work using alternate way even after changing the  
URL.  
I get the following error.

syntax error, unexpected kDO, expecting $end  
do |src|

Just out of curiosity- how can I fix this.  
I would like to know so that I can work through it and figure out exactly  
what the script is doing and  
compare it to the previous script.  
Thanks again!

> **···**
>
> On Wed, Jan 14, 2009 at 4:30 AM, Brian Candler \<b.candler@pobox.com\> wrote:
> 
> > Nathaniel Escribano wrote:  
> > \> require 'net/http'  
> > \>  
> > \> Net::HTTP.start("cdn.stereogum.com") { |http|  
> > \> resp = http.get("/mp3/My Morning Jacket - Celebration (Live).mp3")  
> > \> open("Celebration.mp3", "wb") { |file|  
> > \> file.write(resp.body)  
> > \> }  
> > \> }  
> > \> puts "the song has been downloaded"
> > 
> > Here is an alternative way of doing it:
> > 
> > require 'open-uri'  
> > open("[http://cdn.stereogum.com/mp3/My](http://cdn.stereogum.com/mp3/My) Morning Jacket - Celebration  
> > (Live).mp3") do |src|  
> > &nbsp;&nbsp;&nbsp;open("Celebration.mp3","wb") do |dst|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while blk = src.read(65536)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dst.write(blk)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > This gives a clear error:
> > 
> > /usr/lib/ruby/1.8/uri/common.rb:436:in `split': bad URI(is not URI?):  
> > [http://cdn.stereogum.com/mp3/My](http://cdn.stereogum.com/mp3/My) Morning Jacket - Celebration (Live).mp3  
> > (URI::InvalidURIError)
> > 
> > Fix the URI and it works:
> > 
> > ...  
> > open("  
> > [http://cdn.stereogum.com/mp3/My%20Morning%20Jacket%20-%20Celebration%20(Live).mp3](http://cdn.stereogum.com/mp3/My%20Morning%20Jacket%20-%20Celebration%20%28Live%29.mp3)  
> > ")  
> > do |src|  
> > ...  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [14 January 2009 16:06 UTC](https://rubytalk.org/t/downloading-an-mp3-from-the-internet/51144/5 "2009-01-14T16:06:55Z")

</div>

Nathaniel Escribano wrote:

> I get the following error.
> 
> syntax error, unexpected kDO, expecting $end  
> do |src|

It was a line-wrap from ruby-forum. Move this to the end of the line  
above.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Davi\_Vidal](https://avatars.discourse-cdn.com/v4/letter/d/b77776/32.png) [@Davi\_Vidal](https://rubytalk.org/u/Davi_Vidal)\
**Post date:** [14 January 2009 16:58 UTC](https://rubytalk.org/t/downloading-an-mp3-from-the-internet/51144/6 "2009-01-14T16:58:34Z")

</div>

Nathaniel Escribano wrote:  
[...]

> > Here is an alternative way of doing it:
> > 
> > require 'open-uri'  
> > open("[http://cdn.stereogum.com/mp3/My](http://cdn.stereogum.com/mp3/My) Morning Jacket - Celebration  
> > (Live).mp3") do |src|  
> > &nbsp;&nbsp;&nbsp;open("Celebration.mp3","wb") do |dst|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while blk = src.read(65536)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dst.write(blk)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;end  
> > end

[...]

My $ 0.02:

require 'open-uri'  
require 'cgi'

url = '[http://cdn.stereogum.com/mp3/&#39](http://cdn.stereogum.com/mp3/&#39); + CGI::escape('My Morning Jacket -  
Celebration (Live).mp3')

open(url) do |src|  
&nbsp;&nbsp;open('Celebration.mp3', 'wb') do |dst|  
&nbsp;&nbsp;&nbsp;&nbsp;while blk = src.read(65536)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dst.write(blk)  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

&nbsp;&nbsp;hth,

davi
