YouTube API CFC

Yesterday one of my clients (roundpeg) asked for a ColdFusion interface to the YouTube API. This turned out to be rather simple since they made use of a REST API. From this work I was able to build a CFC to work with all the functions defined in the API. This lets you get video information, profile information, search for videos etc. Best of all - roundpeg, Inc was cool with me releasing the code. You can download the CFC below.

Now the bad news is that YouTube will eventually be switching to a GData based API. I am not a fan of Google's APIs so frankly I can't see this as being a good thing - but I'll worry about that when the API is updated.

Here are some sample calls:

<cfset devid = "changethis">

<cfset yt = createObject("component", "youtube").init(devid)>

<!--- get videos by user --->
<cfdump var="#yt.getVideosByUser('joerinehart')#" label="Videos by user.">

<!--- get music videos tagged Lush --->
<cfdump var="#yt.getVideosByCategoryAndTag(10,'Lush')#" label="Lush music videos." top="10">

p.s. Note that you need to get a developer profile and key before you use the code.

Comments

This is downright freaky! You are reading my mind! I just googled "YouTube CFC" yesterday and didn't find much. I looked over their API and thought "hmmm, I could build one... if I ever get the time."

Thanks for saving me the time!
# Posted By Tampa ColdFusion | 8/21/07 10:51 PM
Really cool Ray. How would we sort the results of a keyword search by most recent upload?
# Posted By CeeVee | 8/22/07 9:57 AM
Just use query of query on the results.
# Posted By Raymond Camden | 8/22/07 9:59 AM
Thanks for the above response.

Another simple question... using the getVideosByTag function, how would I get the total # of records returned so I could set up "paging" links? It seems that I need to have a default perpage value. Without the total # of records returned based on my search term, I cannot create the paging links.
# Posted By CeeVee | 8/22/07 11:02 AM
It is not currently in the API.

FYI, I have a very cool demo I'll be blogging soon. Mixes CF8+YouTube.cfc together.
# Posted By Raymond Camden | 8/22/07 12:09 PM
I contacted YouTube API dev forum about finding the total number of records when searching by a tag. Here was their response:

"Each youtube.videos.list_by_tag response includes the
video_list.total element, which contains the total
number of records."

I can't figure out what I need to do using your CFC to view the total number of records. Am I missing something? Could you help?
# Posted By CeeVee | 8/23/07 7:57 AM
My code doesn't support it - so it isn't anyhting you are doing wrong. I'll see if I can add this today. Most likely I will forget to post back here - so watch youtubecfc.riaforge.org for the update.
# Posted By Raymond Camden | 8/23/07 8:00 AM
Thanks so much! I was pretty sure this was possible. I'm glad you'll be incorporating this.

BTW, either go buy Bioshock or add to your wishlist. It's amazing.
# Posted By CeeVee | 8/23/07 8:10 AM
Bioshock is already on my wishlist - and already bought. :)

Ok the code is done. I'll be uploading to RIAForge in 5 minutes.
# Posted By Raymond Camden | 8/23/07 8:15 AM
Hey Raymond,
I am trying to use your cfc to upload videos to youtube but got some errors.
The cfhttp statement on youtube.cfc line 484 is not returning anything. When I do a cfdump, I see an empty authtoken. Could you please help?
# Posted By FALCONSEYE | 10/16/08 1:46 PM
Falconseye - This was discussed on another blog entry of mine, but there is a bug w/ CF7 and 8 that is blocking this feature. This HAS been fixed by Adobe and Adobe WILL be releasing a hot fix. Once they do, I'll be announcing the hot fix and uploading my latest YouTubeCFC. I was told sometime this week.
# Posted By Raymond Camden | 10/16/08 1:50 PM
Thanks Raymond. I got an authtoken back from my upload. My code is :

<cfset devid = "AI39si4J8K77YGl7yRXabWq9CFb0M-86TcuYFcYlEmnMDv4XL3OtoK7q1N4ApXYC5Y5ntbFurXGCmGi_U8mRy-CdUHezoMLIXQ">

<cfset yt = createObject("component", "youtube")>
<cfset yt.setDeveloperKey("#devid#")>

<cfset yt.login("myUserName", "myPassword")>
<cfset r = yt.upload('video.wmv','Test Upload','Playing with youtube','Music','testing,fun')>

This returns no error. When I do the following:


<cfdump var="#yt.getVideosByUser('myUserName')#" label="Videos by user.">

The cfdump returns nothing. Do you think this is still caused by the same problem?
# Posted By FALCONSEYE | 10/16/08 2:41 PM
No - but I'd just hold off. The new CFC updates a lot of code.
# Posted By Raymond Camden | 10/16/08 2:43 PM
@Ray:

Any word on the hotfix for CF7/CF8 from Adobe?

Also, what is the best way to go about tweaking the YouTube CFC for CF7 usage?
# Posted By Jason Brookins | 10/20/08 11:31 AM
Another note at the YouTube Developer Site:

"The APIs accessed by this developer ID program have been deprecated and are no longer supported. To use the new APIs or get information about migrating your applications, please visit the YouTube Developer site at code.google.com."

Does that impact YouTube CFC?
# Posted By Jason Brookins | 10/20/08 11:35 AM
@Jason - 1: Not yet. I just checked. I'll give them a day or two and check again. Please feel free to remind me here.

@Jason - 2: It should be CF7 compat. The hot fix they are releasing will be for CF7 as well. If I did use any CF8 functionality, it was probably by accident and a small snippet like X++, which would need to change to x=x+1.

@Jason - 3: This was all tested with the new API.
# Posted By Raymond Camden | 10/20/08 11:51 AM
I checked the demo of this CFC, its really great. BUT, I am unable to find how you display the videos for a search query when youtube gdata returns no videos. For example : "Sizzling Model Scorch The Runway " query on youtube.com doesnt return any videos, but it returns the videos for "runway model" and so does your cfc example/demo page, but when I use your cfc files and do the same search, I do not get any videos in return. Please advice.
# Posted By Sasha | 11/11/08 2:50 PM
I see you emailed me directly. I responded there asking for some sample code.
# Posted By Raymond Camden | 11/11/08 3:12 PM