<% ' YouTube class ' ' The YouTube class is a class that allows you to search for and display ' YouTube videos on your own web pages. The class will once it reaches final ' release also include support for video uploading to YouTube. ' ' -------------------------------------------------------------------------- ' Usage: ' Set results = Server.CreateObject("Scripting.Dictionary") ' Set yt = new YouTube ' yt.Search results, "search phrase", "username", 1, 10, "video", true, 0, "relevance" ' ' keys = results.Keys ' For i = 0 To results.Count -1 ' Response.Write(results.Item(keys(i)).Title & "
") ' Response.Write(results.Item(keys(i)).ShortDescription & "
") ' Next ' -------------------------------------------------------------------------- ' ' @author Peter Törnstrand , http://www.tornstrand.com/ ' @version 0.5, Last revision 2007-11-30 Class YouTube Private yt_updated Public Property Get Updated() Updated = yt_updated End Property Private yt_totalResults Public Property Get TotalResults() TotalResults = yt_totalResults End Property Private yt_startIndex Public Property Get StartIndex() StartIndex = yt_startIndex End Property Private yt_itemsPerPage Public Property Get ItemsPerPage() ItemsPerPage = yt_itemsPerPage End Property Private yt_feeds Public Property Get Feeds() Feeds = yt_feeds End Property ' Constructor Private Sub Class_Initialize() ' Add feeds Set yt_feeds = Server.CreateObject("Scripting.Dictionary") yt_feeds.Add "video", "http://gdata.youtube.com/feeds/api/videos" yt_feeds.Add "entertainment", "http://gdata.youtube.com/feeds/api/videos/-/Entertainment" yt_feeds.Add "topRated", "http://gdata.youtube.com/feeds/api/standardfeeds/top_rated" yt_feeds.Add "mostViewed", "http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed" yt_feeds.Add "recentlyFeatured", "http://gdata.youtube.com/feeds/api/standardfeeds/recently_featured" yt_feeds.Add "videosForMobilePhones", "http://gdata.youtube.com/feeds/api/standardfeeds/watch_on_mobile" End Sub ' Get XML feed ' ' @param string feed Feed url with querystring parameters ' @return string Xml data Private Function GetXML(feed) On Error Resume Next Set objXml = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0") If Err.Number <> 0 Then Response.Write("

Error: " & Err.Description & "

") End If objXml.Open "GET", feed, False objXml.Send() ' Check response status If objXml.Status <> 200 Then GetXML = "Could not retrieve XML from source! Status HTTP1.1/" & objXml.Status Else ' Get response GetXML = objXml.responseText End If Set objXml = Nothing End Function ' Parse XML feed and return YouTubeVideo objects ' ' @param Dictionary dict Dictionary object ' @param string xml XML results Private Sub ParseResults(dict, xml) ' Create objects Set objXml = Server.CreateObject("MSXML2.DOMDocument.3.0") objXml.async = False ' Load string objXml.loadXML xml Set xmlNode = objXml.documentElement ' Set object attributes yt_updated = xmlNode.childNodes(1).Text yt_totalResults = xmlNode.childNodes(11).Text yt_startIndex = xmlNode.childNodes(12).Text If xmlNode.childNodes.length > 13 Then yt_itemsPerPage = xmlNode.childNodes(13).Text For Each oNode In xmlNode.childNodes If oNode.nodeName = "entry" Then ' Create YouTubeVideo object Set objVideo = New YouTubeVideo ' Set object attributes tmpId = GetNamedItem(oNode, "id") objVideo.ID = Mid(tmpId, InStrRev(tmpId, "/")+1, Len(tmpId)) objVideo.Published = GetNamedItem(oNode, "published") objVideo.Updated = GetNamedItem(oNode, "updated") objVideo.Title = GetNamedItem(oNode, "title") objVideo.Statistics = GetNamedItemAttr(oNode, "yt:statistics", "viewCount") objVideo.Rating = GetNamedItemAttr(oNode, "gd:rating", "average") For Each oChildNode in oNode.childNodes If oChildNode.nodeName = "media:group" Then objVideo.ShortDescription = GetNamedItem(oChildNode, "media:description") objVideo.Duration = GetNamedItemAttr(oChildNode, "yt:duration", "seconds") 'objVideo.MediaURL = GetNamedItemAttr(oChildNode, "media:content", "url") 'objVideo.MediaType = GetNamedItemAttr(oChildNode, "media:content", "type") objVideo.ThumbnailURL = GetNamedItemAttr(oChildNode, "media:thumbnail", "url") objVideo.ThumbnailWidth = GetNamedItemAttr(oChildNode, "media:thumbnail", "width") objVideo.ThumbnailHeight = GetNamedItemAttr(oChildNode, "media:thumbnail", "height") GetMediaContent oChildNode, objVideo End If If oChildNode.nodeName = "media:group" Then objVideo.CommentsLink = GetNamedItemAttr(oChildNode, "gd:feedLink", "href") End If Next ' Add object to results 'yt_results.Add objVideo.ID, objVideo dict.Add objVideo.ID, objVideo ' Destroy object Set objVideo = Nothing End If Next End Sub ' Get media content ' ' @param XMLNode oNode XML Node ' @param YouTubeVideo oVideo Video object Private Sub GetMediaContent(oNode, oVideo) For Each oChildNode in oNode.childNodes If oChildNode.nodeName = "media:content" Then If oChildNode.getAttribute("isDefault") = "true" Then oVideo.MediaURL = oChildNode.getAttribute("url") oVideo.MediaType = oChildNode.getAttribute("type") End If End If Next End Sub ' Get named node ' ' @param XMLNode oNode XML Node ' @param string name Node name ' @return XMLNode Private Function GetNamedItem(oNode, name) For Each oChildNode in oNode.childNodes If oChildNode.nodeName = name Then GetNamedItem = oChildNode.Text End If Next End Function ' Get named node attribute ' ' @param XMLNode oNode XML Node ' @param string name Node name ' @param string attribute Attribute name ' @return XMLNode Private Function GetNamedItemAttr(oNode, name, attr) For Each oChildNode in oNode.childNodes If oChildNode.nodeName = name Then GetNamedItemAttr = oChildNode.getAttribute(attr) End If Next End Function ' Search for videos ' ' @param Dictionary dict Dictionary object to hold results ' @param string query The query ' @param string author User name ' @param int startIndex Start index (1-based) ' @param int maxResults Number of results to return ' @param string feed Name of feed to search ' @param bool restricted Include restricted (adult) content ' @param int format Only include specific video format in results (allowed values: 0, 1, 5) ' @param string order Sort results (allowed values: updated, viewCount, rating, relevance) Public Sub Search(dict, query, author, startIndex, maxResults, feed, restricted, format, order) ' Convert bool to string If restricted Then restricted = "include" Else restricted = "exclude" End If ' Construct query url = yt_feeds.Item(feed) If query <> "" Then url = url & "?vq=" & Trim(query) If author <> "" Then url = url & "?author=" & author url = url & "&start-index=" & CInt(startIndex) _ & "&max-results=" & CInt(maxResults) _ & "&racy=" & restricted _ & "&orderby=" & order If format <> "0" Then url = url & "&format=" & CInt(format) ' Get XML results strXml = GetXML(url) ' Parse results and load into yt_results variable ParseResults dict, strXml End Sub End Class ' Class representing a YouTube video ' ' The YouTubeVideo class is a object representation of a YouTube video item. The ' class is used by the YouYube class witch allows you to search for and display ' YouTube videos on your own web pages. ' ' @author Peter Törnstrand , http://www.tornstrand.com/ ' @version 0.5, Last revision 2007-11-30 Class YouTubeVideo Private ytv_id Public Property Get ID() ID = ytv_id End Property Public Property Let ID(ytv_val) ytv_id = ytv_val End Property Private ytv_published Public Property Get Published() Published = ytv_published End Property Public Property Let Published(ytv_val) ytv_published = ytv_val End Property Private ytv_updated Public Property Get Updated() Updated = ytv_updated End Property Public Property Let Updated(ytv_val) ytv_updated = ytv_val End Property Private ytv_title Public Property Get Title() Title = ytv_title End Property Public Property Let Title(ytv_val) ytv_title = ytv_val End Property Private ytv_mediaWidth Public Property Get MediaWidth() MediaWidth = ytv_mediaWidth End Property Public Property Let MediaWidth(ytv_val) ytv_mediaWidth = ytv_val End Property Private ytv_mediaHeight Public Property Get MediaHeight() MediaHeight = ytv_mediaHeight End Property Public Property Let MediaHeight(ytv_val) ytv_mediaHeight = ytv_val End Property Private ytv_mediaUrl Public Property Get MediaURL() MediaURL = ytv_mediaUrl End Property Public Property Let MediaURL(ytv_val) ytv_mediaUrl = ytv_val End Property Private ytv_mediaType Public Property Get MediaType() MediaType = ytv_mediaType End Property Public Property Let MediaType(ytv_val) ytv_mediaType = ytv_val End Property Private ytv_mediaDuration Public Property Get Duration() Duration = ytv_mediaDuration End Property Public Property Let Duration(ytv_val) ytv_mediaDuration = ytv_val End Property Private ytv_thumbnailUrl Public Property Get ThumbnailURL() ThumbnailURL = ytv_thumbnailUrl End Property Public Property Let ThumbnailURL(ytv_val) ytv_thumbnailUrl = ytv_val End Property Private ytv_thumbnailWidth Public Property Get ThumbnailWidth() ThumbnailWidth = ytv_thumbnailWidth End Property Public Property Let ThumbnailWidth(ytv_val) ytv_thumbnailWidth = ytv_val End Property Private ytv_thumbnailHeight Public Property Get ThumbnailHeight() ThumbnailHeight = ytv_thumbnailHeight End Property Public Property Let ThumbnailHeight(ytv_val) ytv_thumbnailHeight = ytv_val End Property Private ytv_shortDescription Public Property Get ShortDescription() ShortDescription = ytv_shortDescription End Property Public Property Let ShortDescription(ytv_val) ytv_shortDescription = ytv_val End Property Private ytv_statistics Public Property Get Statistics() Statistics = ytv_statistics End Property Public Property Let Statistics(ytv_val) ytv_statistics = ytv_val End Property Private ytv_rating Public Property Get Rating() Rating = ytv_rating End Property Public Property Let Rating(ytv_val) ytv_rating = ytv_val End Property Private ytv_commentsLink Public Property Get CommentsLink() CommentsLink = ytv_commentsLink End Property Public Property Let CommentsLink(ytv_val) ytv_commentsLink = ytv_val End Property ' Constructor, init object Private Sub Class_Initialize() End Sub ' Print thumbnail ' ' @param string className CSS class name for image Public Sub PrintThumbnailHTML(className) str = "" Response.Write(str) End Sub ' Print OBJECT/EMEBED tag Public Sub PrintEmbededHTML() str = "" _ & "" _ & "" _ & "" _ & "" Response.Write(str) End Sub End Class %>