Code for the Youtube thumbnail getter

I had some questions on the Youtube thumbnail getter project I did for fun a few days back so I wanted to post the code. Take it, use it, do whatever you want with it, just don’t blame me if it breaks the internets. :-)

The code is pretty well commented but lemme give a quick rundown. I set up the parameters to cover the ID being passed in either via the URL or form. You can pass just the ID or the entire URL to the youtube video (has to be on youtube.com). Once you isolate the video ID with the sorry excuse for regex you’re golden. Simply make an http request for the video, make sure it’s a valid video (still hosted) by checking the response and from there you do whatever you want with the thumbnails.

It’s actually pretty simple and probably overkill but very usefull as a little utility in an application where users are posting videos and you need to get the thumbnails.

btw, I stripped the code down of anything unnecessary in order to stay close to the task at hand, getting the thumbnails.

<!--- set the params, can accept an ID by url or form --->
<cfparam name="URL.ID" default="0" type="string">
<cfparam name="FORM.ID" default="#URL.ID#" type="string">
<cfparam name="vidID" default="#FORM.ID#" type="string">
<cfparam name="message" default="" type="string">
<cfparam name="found" default="0" type="numeric">

<!--- if an ID has been submitted (either by URL or form) then start the processing --->
<cfif vidID NEQ 0>

	<!--- super lame attempt at regex, just stripping out everything but the ID
		  100% guarantee you could do better --->
	<cfset vidID = ReReplaceNoCase(vidID,"http:|www.youtube.com|watch|v=|/|\?|&|=|related|feature","","ALL")>

	<!--- check length of the video ID --->
	<cfif Len(vidID) EQ 11>

		<!--- set the video URL --->
		<cfset theVideo = "http://www.youtube.com/watch?v=#vidID#">

		<!--- check the video on youtube --->
		<cfhttp method="get" url="#theVideo#" result="objHttp" >

		<!--- if the page is found and the video video is available --->
		<cfif FindNoCase("200",objHttp.Filecontent) AND Not FindNoCase("The video you have requested is not available.",objHttp.Filecontent)>
			<cfset found = 1>
		<cfelse>
			<cfset message="Hmm, that video didn't cut it. :-\">
		</cfif>

	<cfelse>
		<!--- if there's a problem here it's most likely because we didn't isolate the ID properly (better regex would solve that)--->
		<cfset message="There was a problem with the video ID (possibly wasn't 11 characters or a valid id)">
	</cfif>

</cfif>

<cfoutput>

	<!--- display the message --->
	<cfif Len(message)>
		<div style="margin:10px 0; padding:5px; background:##FFFFCC;">#message#</div>
	</cfif>

	<!--- if the video was found --->
	<cfif found EQ 1>

		<!--- Now that they're found you can pretty much do anything with them
			  from here, i'm just outputting them to the browser --->

		<h1>Here's your thumbnails</h1>
		<cfloop from="0" to="3" index="i">
			<img src="http://img.youtube.com/vi/#vidID#/#i#.jpg" />
		</cfloop>
		<img src="http://img.youtube.com/vi/#vidID#/default.jpg" />

	<cfelse>

		<h1>Enter a URL to a youtube video or a youtube video ID</h1>
		<form method="post" action="#CGI.SCRIPT_NAME#">
			<input name="ID" type="text" maxlength="50" size="50">
			<input name="submit" type="submit" value="Get 'em">
		</form>	

	</cfif>

</cfoutput>