package com.zehfernando.net.apis.youtube.data { import com.zehfernando.net.apis.youtube.YouTubeConstants; import com.zehfernando.net.apis.youtube.YouTubeDataUtils; import com.zehfernando.utils.DateUtils; /*FDT_IGNORE*/ import com.zehfernando.utils.DateUtils; /*FDT_IGNORE*/ /** * @author zeh */ public class YouTubeVideo { // http://gdata.youtube.com/feeds/api/videos/jsvzVFL0Da8 // Properties public var id:String; public var published:Date; public var updated:Date; public var keywords:Vector.; public var categories:Vector.; public var title:String; public var content:String; public var author:String; public var comments:int; public var thumbnails:Vector.; public var duration:Number; public var favoriteCount:int; public var viewCount:int; // Example: http://gdata.youtube.com/feeds/api/videos/jsvzVFL0Da8 // TODO: add: // DE // // responses // http://code.google.com/apis/youtube/2.0/developers_guide_protocol_api_query_parameters.html // ================================================================================================================ // CONSTRUCTOR ---------------------------------------------------------------------------------------------------- public function YouTubeVideo() { } // ================================================================================================================ // PUBLIC INTERFACE ----------------------------------------------------------------------------------------------- public function getHighestResolutionThumbnailURL(): String { // Returns the URL of the biggest thumbnail var url:String = ""; var maxWidth:int = 0; for (var i:int = 0; i < thumbnails.length; i++) { if (thumbnails[i].width > maxWidth) { maxWidth = thumbnails[i].width; url = thumbnails[i].url; } } return url; } // ================================================================================================================ // STATIC INTERFACE ----------------------------------------------------------------------------------------------- public static function fromXMLList(__xmlList:XMLList): Vector. { var videos:Vector. = new Vector.(); if (!Boolean(__xmlList)) return videos; for (var i:int = 0; i < __xmlList.length(); i++) { videos.push(YouTubeVideo.fromXML(__xmlList[i])); } return videos; } public static function fromXML(__xml:XML): YouTubeVideo { // Creates a YouTubeVideo instance from XML parsed from the youtube response /* http://gdata.youtube.com/feeds/api/videos/jsvzVFL0Da8 2010-06-30T21:19:01.000Z 2010-07-12T18:17:58.000Z Trailer for website.wmv Brand New Trailer For Our Website!! deepinsidetitanic http://gdata.youtube.com/feeds/api/users/deepinsidetitanic Nonprofit Brand New Trailer For Our Website!! trailer, for, website DE Trailer for website.wmv */ var video:YouTubeVideo = new YouTubeVideo(); var i:int; var tempArray:Array; var tempList:XMLList; var tempStr:String; // Default namespace data ------------------- var ns:Namespace = __xml.namespace(); default xml namespace = ns; tempArray = __xml.child("id").toString().split("/"); video.id = tempArray[tempArray.length - 1] as String; video.keywords = new Vector.(); video.categories = new Vector.(); video.thumbnails = new Vector.(); video.published = DateUtils.xsdDateTimeToDateUniversal(__xml.child("published").toString()); video.updated = DateUtils.xsdDateTimeToDateUniversal(__xml.child("updated").toString()); video.title = __xml.child("title").toString(); video.content = __xml.child("content").toString(); video.author = __xml.child("author").child("name").toString(); tempList = __xml.child("category"); for (i = 0; i < tempList.length(); i++) { tempStr = tempList[i].@scheme; if (tempStr == YouTubeConstants.SCHEMA_KEYWORD) { // It's a keywork video.keywords.push(tempList[i].@term); } else if (tempStr == YouTubeConstants.SCHEMA_CATEGORY) { // It's a category video.categories.push(new YouTubeCategory(tempList[i].@term, tempList[i].@label)); } } // GD namespace data ------------------- var gdns:Namespace = __xml.namespace(YouTubeConstants.NAMESPACE_GD); video.comments = parseInt(__xml.gdns::comments.gdns::feedLink[0].@countHint, 10); // YT namespace data ------------------- var ytns:Namespace = __xml.namespace(YouTubeConstants.NAMESPACE_YT); if (Boolean(__xml.ytns::statistics) && __xml.ytns::statistics.length() > 0) { video.favoriteCount = parseInt(__xml.ytns::statistics[0].@favoriteCount, 10); video.viewCount = parseInt(__xml.ytns::statistics[0].@viewCount, 10); } // Media namespace data ------------------- var medians:Namespace = __xml.namespace(YouTubeConstants.NAMESPACE_MEDIA); tempList = __xml.medians::group.medians::thumbnail; for (i = 0; i < tempList.length(); i++) { video.thumbnails.push(new YouTubeThumbnail(tempList[i].@url, parseInt(tempList[i].@height, 10), parseInt(tempList[i].@width), YouTubeDataUtils.fromStringToSeconds(tempList[i].@time))); } // Mixed namespace data ------------------- video.duration = parseInt(__xml.medians::group[0].ytns::duration.@seconds); default xml namespace = new Namespace(""); // WTF! one needs this otherwise the function below fails! return video; } } }