Exemplo n.º 1
0
        /**
         * @brief get all the informations of a video from an URL which is a string
         *
         * @return a structure of Video Youtube
         */
        public async Task <VideoYoutube> GetVideoByUrl(string url)
        {
            VideoYoutube toReturn = new VideoYoutube {
                Dislikes = 0, Likes = 0, Title = "", VideoUrl = "", ViewCount = 0
            };
            string id = GetIdVideo(url);

            if (id == null)
            {
                return(toReturn);
            }
            return(await GetVideoById(id));
        }
Exemplo n.º 2
0
        /**
         * @brief get all the informations of a video from an Id which is a string
         *
         * @return a structure of Video Youtube
         */
        public async Task <VideoYoutube> GetVideoById(string id)
        {
            VideoYoutube toReturn = new VideoYoutube {
                Dislikes = 0, Likes = 0, Title = "", VideoUrl = "", VideoId = "", ViewCount = 0
            };
            YouTubeService yt = new YouTubeService(new BaseClientService.Initializer()
            {
                ApiKey = MyYOUTUBE_DEVELOPER_KEY
            });
            var videoStats = yt.Videos.List("statistics");

            videoStats.Id = id;

            if (id.Length != 11)
            {
                return(toReturn);
            }

            try
            {
                var videoStatsResponse = await videoStats.ExecuteAsync();

                if (videoStatsResponse.Items.Count == 1)
                {
                    toReturn.Title     = "";
                    toReturn.VideoId   = id;
                    toReturn.VideoUrl  = BASE_URL + id.ToString();
                    toReturn.Likes     = videoStatsResponse.Items[0].Statistics.LikeCount;
                    toReturn.Dislikes  = videoStatsResponse.Items[0].Statistics.DislikeCount;
                    toReturn.ViewCount = videoStatsResponse.Items[0].Statistics.ViewCount;
                }
                else
                {
                    return(toReturn);
                }

                var videoContent = yt.Videos.List("snippet");
                videoContent.Id = id;
                var videoContentResponse = await videoContent.ExecuteAsync();

                if (videoContentResponse.Items.Count == 1)
                {
                    toReturn.Title = videoContentResponse.Items[0].Snippet.Title;
                }
            } catch (Exception ex) {
                Console.WriteLine("Error: " + ex.Message);
            }
            return(toReturn);
        }