Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Extract youtube video id with eval?

Reply
Thread Tools

Extract youtube video id with eval?

 
 
comopasta Gr
Guest
Posts: n/a
 
      04-07-2008
Hi,

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

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

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

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-forum.com/.

 
Reply With Quote
 
 
 
 
comopasta Gr
Guest
Posts: n/a
 
      04-07-2008
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-forum.com/.

 
Reply With Quote
 
 
 
 
Jano Svitok
Guest
Posts: n/a
 
      04-07-2008
On Mon, Apr 7, 2008 at 10:21 PM, comopasta Gr <(E-Mail Removed)> 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/topic/143788

 
Reply With Quote
 
comopasta Gr
Guest
Posts: n/a
 
      04-07-2008
Looks like what I'm looking for is an equivalent to javascripts
toQueryParams().
Is there one?

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

 
Reply With Quote
 
Stefano Crocco
Guest
Posts: n/a
 
      04-07-2008
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=n1NVf...eature=related
> (&feature might or might not be present)
>
> I can get the thum of the video using:
> http://img.youtube.com/vi/n1NVfDlU6yQ/default.jpg
>
> 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.youtube.com/vi/#{id}/default.jpg"

I hope this helps

Stefano

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Youtube: Can I upload an audio file to Youtube ? pokhara67 Computer Support 13 06-28-2007 02:04 PM
A clone of YouTube, but better then YouTube rishil DVD Video 1 03-27-2007 03:07 PM



Advertisments