private static void FetchVideoDetail(Entities.YoutubeVideo ytvideo, string apiKey, dynamic responseItem) { if (responseItem != null) { if (responseItem["id"] != null) { ytvideo.videoId = Convert.ToString(responseItem["id"]); ytvideo.videoUrl = Convert.ToString("https://www.youtube.com/watch?v=" + responseItem["id"]); } if (responseItem["snippet"]["thumbnails"]["medium"]["url"] != null) { ytvideo.thumbnailUrl = Convert.ToString(responseItem["snippet"]["thumbnails"]["medium"]["url"]); } if (responseItem["snippet"]["title"] != null) { ytvideo.title = Convert.ToString(responseItem["snippet"]["title"]); } if (responseItem["contentDetails"]["duration"] != null) { TimeSpan youTubeDuration = XmlConvert.ToTimeSpan(Convert.ToString(responseItem["contentDetails"]["duration"])); if (youTubeDuration.Hours > 0) { ytvideo.duration = youTubeDuration.Hours.ToString() + ":"; } if (youTubeDuration.Minutes > 0) { ytvideo.duration = ytvideo.duration + (youTubeDuration.Minutes < 10 ? (youTubeDuration.Hours > 0 ? ("0" + youTubeDuration.Minutes.ToString() + ":") : youTubeDuration.Minutes.ToString() + ":") : youTubeDuration.Minutes.ToString() + ":"); } else { ytvideo.duration = ytvideo.duration + "0:"; } if (youTubeDuration.Seconds > 0) { ytvideo.duration = ytvideo.duration + (youTubeDuration.Seconds < 10 ? ("0" + youTubeDuration.Seconds.ToString()) : youTubeDuration.Seconds.ToString()); } else { ytvideo.duration = ytvideo.duration + "00"; } } } }
private static List <Entities.VideoInfo> GetYoutubeVideoList(string ApiKey, string Keyword, int PageNo, int PageSize, Dictionary <string, object> AdditionalData) { List <Entities.VideoInfo> ytVideoList = new List <Entities.VideoInfo>(); dynamic searchListResponse = null; string nextPageToken = string.Empty; if (AdditionalData != null && AdditionalData.ContainsKey("nextPageToken")) { nextPageToken = AdditionalData["nextPageToken"].ToString(); } dynamic AllVideoResponse = null; string queryString = string.Empty; queryString = Keyword + "&key=" + ApiKey + "&part=snippet"; queryString += "&safeSearch=" + "None"; searchListResponse = GetResponse("https://www.googleapis.com/youtube/v3/search?q=" + queryString + "&maxResults=" + PageSize + "&pageToken=" + nextPageToken, searchListResponse); if (searchListResponse != null) { nextPageToken = Convert.ToString(searchListResponse["nextPageToken"]); if (searchListResponse["items"] != null) { string videoIdString = string.Empty; foreach (dynamic responseItem in searchListResponse["items"]) { if (responseItem["id"]["kind"].Value == "youtube#video") { videoIdString += Convert.ToString(responseItem["id"]["videoId"]) + ","; } } if (!string.IsNullOrEmpty(videoIdString)) { AllVideoResponse = GetResponse("https://www.googleapis.com/youtube/v3/videos?id=" + videoIdString.TrimEnd(',') + "&key=" + ApiKey + "&part=snippet,statistics,contentDetails", AllVideoResponse); } if (AllVideoResponse != null) { foreach (dynamic VideoResponse in AllVideoResponse["items"]) { Entities.YoutubeVideo ytVideo = new Entities.YoutubeVideo(); FetchVideoDetail(ytVideo, ApiKey, VideoResponse); // get video details by Id Entities.VideoInfo ytVideoinfo = new Entities.VideoInfo() { ID = ytVideo.videoId, Thumbnail = ytVideo.thumbnailUrl, Url = ytVideo.videoUrl, Title = ytVideo.title, Duration = ytVideo.duration }; ytVideoList.Add(ytVideoinfo); } } } } if (!string.IsNullOrEmpty(nextPageToken) && ytVideoList.Count > 0) { ytVideoList[0].AdditionalData = new Dictionary <string, object> { { "nextPageToken", nextPageToken } }; } return(ytVideoList); }