Posted in ColdFusion | Posted on 08-21-2007 | 7,661 views
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:
2
3<cfset yt = createObject("component", "youtube").init(devid)>
4
5<!--- get videos by user --->
6<cfdump var="#yt.getVideosByUser('joerinehart')#" label="Videos by user.">
7
8<!--- get music videos tagged Lush --->
9<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.


Thanks for saving me the time!
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.
FYI, I have a very cool demo I'll be blogging soon. Mixes CF8+YouTube.cfc together.
"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?
BTW, either go buy Bioshock or add to your wishlist. It's amazing.
Ok the code is done. I'll be uploading to RIAForge in 5 minutes.
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?
<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?
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?
"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?
@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.
First of, great job on youtube.cfc !
Do you have an example on how to delete a video? I saw the function on the .cfc page and tried calling it using the following:
<cfset yt = createObject("component", "/yt/youtube")>
<cfset yt.setDeveloperKey("mydevkey")>
<cfset yt.login("myusername", "mypassword")>
<cfset r = yt.delete('#form.deleteytvideoid#')>
But it doesn't seem to be working. I'm only passing the video id as "form.deleteytvideoid". I am missing anything?
Thanks!
Anthony
I found the answer to my question above.
The url had actually changed to:
<cfset var theurl = "http://gdata.youtube.com/feeds/api/users/#variable...;
Now it's working!
Thanks again for a great code and everything you do for the CF Community!
- Anthony
When uploading a video, what does it mean when I get this error:
"YouTubeCFC Upload Error: Domain=yt:validation, Code=too_short"
Does it have to do with the number of entries into the video name or description?
Thanks,
Anthony
<cfset yt = createObject("component", "/yt/youtube")>
<cfset yt.setDeveloperKey("<key>")>
<cfset yt.login("<login>", "<password>")>
<cfset r = yt.upload('<path_to_video>','Cow Test Number 3','This is a test description for the new cause/project.','Music','Cow Test Number 3, United States, Charity, Donation, Help, ')>
- Anthony
this is my code:
<cfset yt = createObject("component", "youtube")>
<cfset result = yt.getVideosByTag('#form.tag#')>
<!---<cfdump var="#result#">--->
<table width="100%" height="100%" border="0">
<cfoutput>
<cfset catname = "#form.cat#">
<h4>Reslts for Tag : <em>#form.tag#</em> and Category : <em>#catname#</em></h4>
<cfloop query="result" >
<cfif catname eq "#result.CATEGORIES#">
<cfset vid=listgetAt('#result.THUMBNAIL_URl#',4, '/')>
<tr><td>Title : #result.TITLE#</td></tr>
<tr><td><a href="test_getvideo.cfm?v=#vid#" ><img src="#result.THUMBNAIL_URL#" /></a></td></tr>
</cfif>
</cfloop>
</cfoutput>
</table>
the function i used is:
<!---Changed to GDATA yotube API--->
<cffunction name="getVideosByTag" access="public" returnType="query" output="false" hint="Searches videos by tag.">
<cfargument name="tag" type="string" required="true">
<cfargument name="page" type="numeric" required="false" default="1">
<cfargument name="perpage" type="numeric" required="false" default="40">
<cfset arguments.perpage = pageFix(arguments.perpage)>
<cfreturn getVideos("#variables.rooturl#feeds/api/videos?vq=#urlEncodedFormat(arguments.tag)#&page=#arguments.page#&per_page=#arguments.perpage#")>
</cffunction>
I do not know if this is an issue with API or something else
What you exactly mean by "Cap PER Page you can't go over"?
:(
getMostLinkedVideos()
whay so! is that function is of no more usage
getMostLinkedVideos()
whay so! is that function is of no more usage
As for getMostLinkedVideos - it may have been removed by YouTube's API. If it still exists I could add it back end.
because search can return me 500+ records, so your say i fetched 50 records and displayed on a page,
then i clicked again next and it again ran the query and fetched next records, but what parameters i pass so it start from next onwards, any IDEA
I hope you might have some IDEA which you would like to share
if not then this cannot be an easy way out
Can you show me how you are calling my CFC with the start parameter?
here what i am doing!
<cfif IsDefined('url.showNext') AND url.showNext IS NOT 0>
<cfset mostRecent = #yt.getRecentlyFeaturedVideos(#url.showNext#,#url.showNext#)#>
<cfelse>
<cfset mostRecent = #yt.getRecentlyFeaturedVideos()#>
</cfif>
at the end i am just doing a recordcount so it should count the records on page and then add the nbext 50 or 25 in it, i might be wrong the way i am doing, but that is all what i found!
i tested it a bit was not working as i expected,
<cfset vids = yt.getRecentlyFeaturedVideos()>
<cfdump var="#vids#">
<cfset vids = yt.getRecentlyFeaturedVideos(25)>
<cfdump var="#vids#">
and the second dump shows results 25-50. It's working. I'm not sure what else to tell you.
Premature end of file"
[Add Comment] [Subscribe to Comments]