/// <summary> /// Gets the newest video information from the channel. /// </summary> /// <param name="channelId">Username or channel id.</param> /// <returns>Newest video information.</returns> public async Task <YtLocalFavChannelList> GetYtChannelVideoList(string channelId) { YtLocalFavChannelList videoList = new YtLocalFavChannelList(); string webResp; using (var httpClient = new HttpClient()) { webResp = await httpClient.GetStringAsync(YtApiWebQueryTemplates.GetChannelVideoListQuery(YtApiWebQueryTemplates.GetYoutubeApiKey(), channelId)); } JObject googleSearch = JObject.Parse(webResp); IList <JToken> results = googleSearch["items"].Children().ToList(); videoList.channelId = channelId; videoList.channelTitle = results[0]["snippet"]["channelTitle"].ToString(); videoList.videoTitle = results[0]["snippet"]["title"].ToString(); videoList.videoId = results[0]["id"]["videoId"].ToString(); videoList.imagePath = results[0]["snippet"]["thumbnails"]["default"]["url"].ToString(); DateTime pubTime = DateTime.Parse(results[0]["snippet"]["publishedAt"].ToString(), null, System.Globalization.DateTimeStyles.RoundtripKind); videoList.publishedAt = $"{pubTime.Date.DayOfWeek}, {pubTime.Date.Day}.{pubTime.Date.Month}.{pubTime.Date.Year} at {pubTime.TimeOfDay.ToString()}"; return(videoList); }
/// <summary> /// Handles channel grid tap event, basically removes the "new" -indication from the top-left corner /// (if any). /// </summary> private void ChannelGridTapped(object sender, TappedRoutedEventArgs e) { if (ImgNewIcon.Visibility == Visibility.Visible) { var tag = ((Grid)sender).Tag; ObservableCollection <YtLocalFavChannelList> YtFavoritesList = ((App)Application.Current).GetFavoritesList(); YtLocalFavChannelList channelItem = YtFavoritesList.Where(i => i.channelId.Equals(tag.ToString())).Single(); int videoIndex = YtFavoritesList.IndexOf(channelItem); YtFavoritesList[videoIndex].isNew = false; ((App)Application.Current).SetFavoritesList(YtFavoritesList); ImgNewIcon.Visibility = Visibility.Collapsed; } }
/// <summary> /// Updates individual channel. /// </summary> private async void btnUpdateVideo_Click(object sender, RoutedEventArgs e) { var tag = ((Button)sender).Tag; ObservableCollection <YtLocalFavChannelList> YtFavoritesList = ((App)Application.Current).GetFavoritesList(); YtLocalFavChannelList videoInfo = new YtLocalFavChannelList(); YtWebInterface webIf = new YtWebInterface(); videoInfo = await webIf.GetYtChannelVideoList(tag.ToString()); YtLocalFavChannelList channelItem = YtFavoritesList.Where(i => i.channelId.Equals(tag.ToString())).Single(); int videoIndex = YtFavoritesList.IndexOf(channelItem); if (YtFavoritesList[videoIndex].videoId != videoInfo.videoId) { YtFavoritesList[videoIndex].imagePath = videoInfo.imagePath; YtFavoritesList[videoIndex].publishedAt = videoInfo.publishedAt; YtFavoritesList[videoIndex].videoId = videoInfo.videoId; YtFavoritesList[videoIndex].videoTitle = videoInfo.videoTitle; ((App)Application.Current).SetFavoritesList(YtFavoritesList); ImgNewIcon.Visibility = Visibility.Visible; } }
/// <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); }
/// <summary> /// Refreshes all channels in the favorites list. /// </summary> private async void RefeshAllChannels() { try { YtWebInterface webIf = new YtWebInterface(); ObservableCollection <YtLocalFavChannelList> YtFavoritesList; YtFavoritesList = ((App)Application.Current).GetFavoritesList(); bool updated = false; // Null check. if (YtFavoritesList == null) { throw new Exception("Cannot update empty favorites list, please add channels first."); } // Start progress ring animation. ProgressRing.Visibility = Visibility.Visible; ProgressRing.IsActive = true; // Update favorites list items and check that the video id is really changed. for (int i = 0; i < YtFavoritesList.Count; i++) { YtLocalFavChannelList videoInfo = new YtLocalFavChannelList(); videoInfo = await webIf.GetYtChannelVideoList(YtFavoritesList[i].channelId); if (YtFavoritesList[i].videoId != videoInfo.videoId) { updated = true; YtFavoritesList[i].imagePath = videoInfo.imagePath; YtFavoritesList[i].publishedAt = videoInfo.publishedAt; YtFavoritesList[i].videoId = videoInfo.videoId; YtFavoritesList[i].videoTitle = videoInfo.videoTitle; YtFavoritesList[i].isNew = true; } } // Update the list if there has been updates in the list. if (updated) { ((App)Application.Current).SetFavoritesList(YtFavoritesList); ProgressRing.Visibility = Visibility.Collapsed; ProgressRing.IsActive = false; ContentFrame.Navigate(typeof(FavoritesPage)); } else { ProgressRing.Visibility = Visibility.Collapsed; ProgressRing.IsActive = false; if (isVideoListUpdatedOnStartup == false) { ContentFrame.Visibility = Visibility.Visible; ContentFrame.Navigate(typeof(FavoritesPage)); } } // Disable auto-update after the startup. isVideoListUpdatedOnStartup = true; } catch (Exception exp) { ((App)Application.Current).ShowErrorMessage(exp.Message); } finally { isVideoListUpdatedOnStartup = true; } }