Parse YouTube URL

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

pkmittal
Posts: 111
Joined: Thu Feb 08, 2007 11:27 am
Contact:

Parse YouTube URL

Post by pkmittal » Fri Sep 14, 2012 5:52 am

Hello, I would like to parse YouTube URL. I am gettting little confused and not sure how to use ReplaceText or some other command to get it.

Lets say the Orginal URL is
http://www.youtube.com/watch?v=wRn__ibJ ... ure=relmfu or can be any valid YouTube URL which user can enter.

Now I would basically like to get the vID (Video ID) and then form the following URLs
Video URL
http://www.youtube.com/v/U8qV6OhrtUA/
Snapshot URL
http://img.youtube.com/vi/U8qV6OhrtUA/default.jpg

Can you please suggest me how to get the vId from the you tube URL.. Basically how get data which is between /watch?v= and &

Thanks
Best Regards
Pradeep

pkmittal
Posts: 111
Joined: Thu Feb 08, 2007 11:27 am
Contact:

Re: Parse YouTube URL

Post by pkmittal » Fri Sep 14, 2012 6:01 am

in the PHP, this is how it has been done...
http://webgarb.com/tutorials/how-to-get ... rfect-way/

Anyone can help me the perfect way of getting YouTube ID from valid you tube URLS...(in LiveCode)

Thanks
Pradeep

doc
Posts: 148
Joined: Fri Jun 09, 2006 4:30 pm

Re: Parse YouTube URL

Post by doc » Fri Sep 14, 2012 3:39 pm

While there may be more elegant solutions, the stack file I've attached shows at least one way to do what you want...

I've also added some options in the stack to automatically launch the freshly parsed URL's using your browser and/or to auto start the video playback based on instructions provided in the YouTube API documentation.

-Doc-
Attachments
YouTubeURLParsing.zip
(1.52 KiB) Downloaded 283 times

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Parse YouTube URL

Post by Klaus » Fri Sep 14, 2012 3:47 pm

Hi Pradeep,

a quick and dirty solution:

Code: Select all

function get_VideoID tUrl
  put "/watch?v=" into tDelimiter1
  put length(tDelimiter1) into tOffset2Add

  ## Will give us the number of the fiorst char of tDelimiter1 in the URL
  put offset(tDelimiter1,tURL) into tStartChar

  ## Now we need to add the number of chars of tDelimiter1 
  ## in order to only get everything AFTER that strings

  ## char -1 is the LAST char in a strings, same for char -2, the last but one etc...
  put char (tStartChar + tOffset2Add) to -1 of tUrl into tUrl2

  ## Good old ITEMDELIMITER, always helpful :-)
  set itemdel to "&"
  return item 1 of tURL2
end get_VideoID
...
answer get_VideoID("http://www.youtube.com/watch?v=wRn__ibJ ... ure=relmfu")
...

I'm sure you can now create the necessary video and image urls from that by yourself 8)
Check "offset" and other terms in the docs to get more info and to fully understand this script!


Best

Klaus

pkmittal
Posts: 111
Joined: Thu Feb 08, 2007 11:27 am
Contact:

Re: Parse YouTube URL

Post by pkmittal » Fri Sep 14, 2012 3:55 pm

Thanks for your response... and for sample example.. Here is the code you have mentioned.
replace "=" with "~" in fld "target"
replace "&" with "~" in fld "target"
set the itemdel to "~"
-- with the tilde character now set as the delimiter, everything in front of the first "~" becomes iten 1
-- and our video ID become item 2, making it simple to parse that info for use later in building the
-- target URL's for the image and/or video
put "http://img.youtube.com/vi/" & item 2 of fld "target" & "/default.jpg" into fld "results1"
put "http://www.youtube.com/v/" & item 2 of fld "target" into fld "results2"
However it assume that first parameter in the URL is video Id...
http://www.youtube.com/watch?v=wRn__ibJ ... ure=relmfu
but it is not the case always...the url could be some thing like this...
http://www.youtube.com/watch?param1=val ... ure=relmfu

Please let me know how to handle it.. and what would be the right way of doing it...

Thanks.
Cheers
Pradeep

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Parse YouTube URL

Post by Klaus » Fri Sep 14, 2012 4:00 pm

use my function and change the first line to:

Code: Select all

function get_VideoID tURL
  put "v=" into tDelimiter1
  ...
At least this "v=" SEEMS to be the starting point you are looking for.

Do you have any documtentation on the YouTube URL format? Are there any standards?
If not, then all solutions might work or not 8)


Best

Klaus

pkmittal
Posts: 111
Joined: Thu Feb 08, 2007 11:27 am
Contact:

Re: Parse YouTube URL

Post by pkmittal » Fri Sep 14, 2012 4:15 pm

Klaus, Thanks for your response.. I can not see your function...

I am looking for the simple basic SubString function... It would be sufficient enough to get the value what ever the order of parameter is.. Because we will search the value between "v=" and "&" to get the vId...

function substring(String1, String2, String3)

It should search in String1 and should return the value between String1 and String2

For example String1 = Hello How are you dear? String 2 = How String3 = dear..
So subString function should return "are"

Please let me know if any one has implemented subString function...

Thanks

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Parse YouTube URL

Post by Klaus » Fri Sep 14, 2012 4:40 pm

pkmittal wrote:I can not see your function...
Oh, come on! :shock:

Only two postings of mine on this page, are you kidding?

doc
Posts: 148
Joined: Fri Jun 09, 2006 4:30 pm

Re: Parse YouTube URL

Post by doc » Fri Sep 14, 2012 6:16 pm

My sample was a simple solution based on the URL that was given by example.. and wasn't intended to accommodate every possibility or situation. I was really just trying to point out the simplicity that LiveCode offers when parsing any kind of string. As you see with Klaus' post(s), there are all kinds of different methods and possibilities. :)

-Doc-

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Parse YouTube URL

Post by Klaus » Fri Sep 14, 2012 6:22 pm

EX-ACTLY! :D

pkmittal
Posts: 111
Joined: Thu Feb 08, 2007 11:27 am
Contact:

Re: Parse YouTube URL

Post by pkmittal » Sat Sep 15, 2012 5:12 am

Thanks a lot for your solutions... I would also try to develop an proper SubString function which is available in other languages...

pkmittal
Posts: 111
Joined: Thu Feb 08, 2007 11:27 am
Contact:

Re: Parse YouTube URL

Post by pkmittal » Sat Sep 15, 2012 5:29 am

Thanks a lot and it works perfectly fine... I appreciate. Thanks everyone...

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Parse YouTube URL

Post by Klaus » Sat Sep 15, 2012 11:59 am

Hi Pradeep,
pkmittal wrote:I would also try to develop an proper SubString function which is available in other languages...
For example String1 = "Hello How are you dear?" String 2 = "How" String3 = "dear"
So subString function should return "are"
OK, but that should actually return: " are you "!

Good luck 8)


Best

Klaus

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Parse YouTube URL

Post by Klaus » Sat Sep 22, 2012 12:57 pm

Hi Pradeep,

after spending 5 minutes with GOOGLE, I found that YouTube vieo IDs have a lenght of 11 characters,
which makes parsing even easier:

Code: Select all

function get_VideoID tUrl
  put "v=" into tDelimiter1
  put length(tDelimiter1) into tOffset2Add
  
  ## Will give us the number of the fiorst char of tDelimiter1 in the URL
  put offset(tDelimiter1,tURL) into tStartChar
  
  ## Now we need to add the number of chars of tDelimiter1
  ## in order to only get everything AFTER that strings
  
  put tStartChar + tOffset2Add into VID_start
  put VID_start + 10 into VID_stop
  
  return char VID_start to VID_stop of tURL tURL2
end get_VideoID
Looks like doing a bit research about a problem is not a bad thing 8)

Best

Klaus

pkmittal
Posts: 111
Joined: Thu Feb 08, 2007 11:27 am
Contact:

Re: Parse YouTube URL

Post by pkmittal » Sat Sep 22, 2012 1:20 pm

Thanks.. However your previous solution was good and it seems to work fine.... for all type of URLS... thanks for your help...

Can you please help me on this? (It is related to proper video upload script)
http://forums.runrev.com/phpBB2/viewtop ... 49&t=12954

Are there any paid script? I can be reached on pkmittal81@gmail.com

Post Reply