/// <summary> /// Gets the channel information by username or channel ID. /// </summary> /// <param name="username">Username or channel id.</param> /// <returns>Channel information (YtChannelInfo)</returns> public async Task <YtChannelInfo> GetYtChannelInfoByUser(string username) { YtChannelInfo channelInfo = new YtChannelInfo(); string webResp; using (var httpClient = new HttpClient()) { webResp = await httpClient.GetStringAsync(YtApiWebQueryTemplates.GetChannelInfoQuery(YtApiWebQueryTemplates.GetYoutubeApiKey(), username)); } JObject googleSearch = JObject.Parse(webResp); IList <JToken> results = googleSearch["items"].Children().ToList(); channelInfo.etag = results[0]["etag"].ToString(); channelInfo.kind = results[0]["kind"].ToString(); channelInfo.id = results[0]["id"].ToString(); return(channelInfo); }
/// <summary> /// Updates the favorites list with channel information /// </summary> /// <param name="URL">Channel URL</param> /// /// Notes/reference: http://stackoverflow.com/questions/30081301/getting-all-videos-of-a-channel-using-youtube-api public async void UpdateInfo(string URL) { string username = GetUserNameFromUrl(URL); bool isChannel = IsUrlFromChannel(URL); YtChannelInfo channelInfo = new YtChannelInfo(); YtLocalFavChannelList videoInfo = new YtLocalFavChannelList(); // Check whether the URL is by channel or username. if (isChannel) { channelInfo.id = username; channelInfo.kind = string.Empty; channelInfo.etag = string.Empty; } else { channelInfo = await GetYtChannelInfoByUser(username); } // Get channel information videoInfo = await GetYtChannelVideoList(channelInfo.id); // Fetch the current favorites list YtFavoritesList = ((App)Application.Current).GetFavoritesList(); // Check whether the channel already exists in the favorites list and throw // an exception if so. try { if (YtFavoritesList != null) { YtLocalFavChannelList channelItem = YtFavoritesList.Where(i => i.channelId.Equals(videoInfo.channelId)).SingleOrDefault(); if (channelItem != null) { throw new Exception("Item already exists!"); } } } catch (Exception ex) { MessageDialog errMessage = new MessageDialog($"Channel (id={videoInfo.channelId}) already exists!"); errMessage.Commands.Add(new UICommand("OK") { Id = 0 }); errMessage.DefaultCommandIndex = 0; var result = await errMessage.ShowAsync(); return; } // Update with username and information whether it's a channel. videoInfo.username = username; videoInfo.isChannel = isChannel; YtFavoritesList.Add(videoInfo); // Save to: C:\Users\Pasi\AppData\Local\Packages\ebade9e5-3eb8-4ce8-a6c4-05919d244312_xd9n95tjqv3aw\LocalState ((App)Application.Current).SetFavoritesList(YtFavoritesList); }