[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Extract youtube video id with eval?

comopasta Gr

4/7/2008 8:18:00 PM

Hi,

I'm just doing the research but maybe someone already knows...

I have a youtube video url:
http://www.youtube.com/watch?v=n1NVfDlU6yQ&featu...
(&feature might or might not be present)

I can get the thum of the video using:
http://img.youtube.com/vi/n1NVfDlU6yQ/d...

So, I just need to use the video id.

Now, how to extract the video id easily? Eval?
Yeah simple I know...I'm working on it in the meanwhile.

Thanks
--
Posted via http://www.ruby-....

6 Answers

comopasta Gr

4/7/2008 8:22:00 PM

0

Hmm. Just read some stuff about eval. The video url is initially
provided by users. So is it wise to use eval for that (?)
--
Posted via http://www.ruby-....

Jano Svitok

4/7/2008 8:30:00 PM

0

On Mon, Apr 7, 2008 at 10:21 PM, comopasta Gr <granadojose@yahoo.com> wrote:
> Hmm. Just read some stuff about eval. The video url is initially
> provided by users. So is it wise to use eval for that (?)

Try using Regexps. BTW, somebody asked very similar question not too
long ago: http://www.ruby-forum.com/to...

comopasta Gr

4/7/2008 8:31:00 PM

0

Looks like what I'm looking for is an equivalent to javascripts
toQueryParams().
Is there one?

Cheers
--
Posted via http://www.ruby-....

Stefano Crocco

4/7/2008 8:31:00 PM

0

On Monday 07 April 2008, comopasta Gr wrote:
> Hi,
>
> I'm just doing the research but maybe someone already knows...
>
> I have a youtube video url:
> http://www.youtube.com/watch?v=n1NVfDlU6yQ&featu...
> (&feature might or might not be present)
>
> I can get the thum of the video using:
> http://img.youtube.com/vi/n1NVfDlU6yQ/d...
>
> So, I just need to use the video id.
>
> Now, how to extract the video id easily? Eval?
> Yeah simple I know...I'm working on it in the meanwhile.
>
> Thanks

If you only need to extract the part of the url between watch?v= and either
the end of the string or the &feature part, you can use a simple regexp:

id = url.match(/watch\?v=(.*?)(?:&feature|\Z)/)[1]

This matches the string 'watch?v= followed by any number of characters and by
either the string &feature or the end of the string. The middle part (which I
described as "any number of character") are then stored in the first subgroup
(the element with index 1 of the MatchData object returned by match).

You can create the image url with:

img_url = "http://img.youtub...{id}/default.jpg"

I hope this helps

Stefano

Marcel Molina Jr.

4/7/2008 8:44:00 PM

0

On Tue, Apr 08, 2008 at 05:18:15AM +0900, comopasta Gr wrote:
> I'm just doing the research but maybe someone already knows...
>
> I have a youtube video url:
> http://www.youtube.com/watch?v=n1NVfDlU6yQ&featu...
> (&feature might or might not be present)
>
> I can get the thum of the video using:
> http://img.youtube.com/vi/n1NVfDlU6yQ/d...
>
> So, I just need to use the video id.
>
> Now, how to extract the video id easily? Eval?
> Yeah simple I know...I'm working on it in the meanwhile.

irb(main):001:0> require 'uri'
=> true
irb(main):002:0> uri = URI.parse('http://www.youtube.com/watch?v=n1NVfDlU6yQ&featu...')
=> #<URI::HTTP:0x1b1066 URL:http://www.youtube.com/watch?v=n1NVfDlU6yQ&featu...>
irb(main):003:0> require 'cgi'
=> true
irb(main):004:0> query_string = CGI.parse(uri.query)
=> {"feature"=>["related"], "v"=>["n1NVfDlU6yQ"]}

marcel
--
Marcel Molina Jr. <marcel@vernix.org>

comopasta Gr

4/7/2008 8:46:00 PM

0

Thank you so much guys!

...and sorry for the topic duplication indeed it was covered before

Cheers!
--
Posted via http://www.ruby-....