private void ChannelsListView_MouseDoubleClick(object sender, MouseEventArgs e) { ListViewItem clickedItem = ChannelsListView.GetItemAt(e.X, e.Y); if (clickedItem != null) { Database.Types.Channel channel = (Database.Types.Channel)clickedItem.Tag; if (MessageBox.Show($"Really delete {channel.Title}?{Environment.NewLine}{Environment.NewLine}You will no longer receive notifications and the videos from{Environment.NewLine}this channel will be deleted from the database.", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes) { try { IEnumerable <Database.Types.Video> channelVideos = Database.Channels.GetAllVideos(channel.ID); int channelVideosTotal = channelVideos.Count(); foreach (Database.Types.Video video in channelVideos) { Database.Videos.Delete(video); Database.ImageFile.Delete(video.ID, ImageType.VideoThumbnail); } Database.Channels.Delete(channel); Database.ImageFile.Delete(channel.ID, ImageType.ChannelIcon); AddAllChannels(); FormsManager.StaticMainForm.AddAllChannels(); LoggingManager.Log.Info($"Deleted channel: '{channel.Title}' ({channel.ID})."); MessageBox.Show($"{channel.Title} was deleted and {channelVideosTotal} videos were removed from the database.", "Channel Deleted", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { LoggingManager.Log.Error(ex, $"Failed to delete a channel. Channel: '{channel.Title}' ({channel.ID})."); } } } }
/// <summary> /// Get information about a channel. /// </summary> /// <param name="channelID">ID of the channel to get information for</param> /// <returns>Information about a channel.</returns> public Database.Types.Channel Info(string channelID) { try { // https://developers.google.com/youtube/v3/docs/channels/list API.ChannelsResource.ListRequest channelInfo = APIService.Channels.List("id,snippet"); channelInfo.Id = channelID; channelInfo.MaxResults = 1; channelInfo.PrettyPrint = false; API.Data.Channel response = channelInfo.Execute().Items[0]; Database.Types.Channel channel = new Database.Types.Channel { ID = response.Id, Title = response.Snippet.Title, Description = response.Snippet.Description, ThumbnailURL = GetBestThumbnail(response.Snippet.Thumbnails) }; LoggingManager.Log.Info($"Information processed for '{channel.ID}'."); return(channel); } catch (Exception ex) { LoggingManager.Log.Error(ex, $"Failed to get information for '{channelID}'."); return(null); } }
/// <summary> /// Add a single channel to <see cref="ChannelsListView"/>. /// </summary> /// <param name="channel">Channel to add.</param> private void AddChannelToListView(Database.Types.Channel channel) { if (!ChannelsImageList.Images.ContainsKey(channel.ID)) { Image resizedImage = ImageUtils.ResizeImage((Image)Database.ImageFile.Get(channel.ID, ImageType.ChannelIcon), 24, 24); ChannelsImageList.Images.Add(channel.ID, resizedImage); } ListViewItem channelItem = new ListViewItem { Name = channel.ID, Tag = channel, ImageKey = channel.ID, Font = new Font("Segoe UI Semibold", 10), Text = $" {channel.Title}" }; ChannelsListView.Items.Add(channelItem); }
private void ChannelsListView_MouseDoubleClick(object sender, MouseEventArgs e) { ListViewItem clickedItem = ChannelsListView.GetItemAt(e.X, e.Y); if (clickedItem != null) { Database.Types.Channel clickedChannel = (Database.Types.Channel)clickedItem.Tag; if (Database.Channels.Exists(clickedChannel.ID)) { MessageBox.Show($"{clickedChannel.Title} ({clickedChannel.ID}) already exists.", "Channel Exists", MessageBoxButtons.OK, MessageBoxIcon.Stop); return; } if (MessageBox.Show($"Add {clickedChannel.Title}?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes) { try { DisableControls("Processing...", false); Database.Types.Channel channel = new YouTube.Channel().Info(clickedChannel.ID); Database.Channels.Upsert(channel); Database.ImageFile.Insert(channel.ThumbnailURL, channel.ID, ImageType.ChannelIcon); List <Database.Types.Video> videos = new YouTube.Channel().RecentVideos(channel.ID); foreach (Database.Types.Video video in videos) { video.WatchStatus = SettingsManager.Configuration.NewChannelDefaultVideoStatus; Database.Videos.Insert(video); Database.ImageFile.Insert(video.ThumbnailURL, video.ID, ImageType.VideoThumbnail); } FormsManager.StaticMainForm.AddAllChannels(); LoggingManager.Log.Info($"Added channel '{channel.ID}'."); EnableControls(); MessageBox.Show($"'{channel.Title}' was added along with {videos.Count} new videos.", "Channel Added", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { LoggingManager.Log.Error(ex, $"Failed to add channel '{clickedChannel.ID}'."); EnableControls(); } } } }
/// <summary> /// Add a single channel to <see cref="ChannelsListView"/>. /// </summary> /// <param name="channel">Channel to add.</param> private void AddChannelToListView(Database.Types.Channel channel) { if (!ChannelsImageList.Images.ContainsKey(channel.ID)) { MemoryStream stream = NetworkUtils.DownloadFileToMemoryStream(channel.ThumbnailURL); Image image = Image.FromStream(stream); Image resizedImage = ImageUtils.ResizeImage(image, 24, 24); ChannelsImageList.Images.Add(channel.ID, resizedImage); } ListViewItem channelItem = new ListViewItem { Name = channel.ID, Tag = channel, ImageKey = channel.ID, Font = new Font("Segoe UI Semibold", 10), Text = $" {channel.Title}" }; ChannelsListView.Items.Add(channelItem); }
/// <summary> /// Search for a channel by its name, or a multitude of other information. /// </summary> /// <param name="channelName">The name, or any other data about the channel to search for.</param> public List <Database.Types.Channel> Search(string channelName) { try { // https://developers.google.com/youtube/v3/docs/search/list API.SearchResource.ListRequest channelSearch = APIService.Search.List("id,snippet"); channelSearch.Q = channelName; channelSearch.Type = "channel"; channelSearch.Order = API.SearchResource.ListRequest.OrderEnum.Relevance; channelSearch.MaxResults = 10; channelSearch.PrettyPrint = false; List <Database.Types.Channel> results = new List <Database.Types.Channel>(); foreach (API.Data.SearchResult item in channelSearch.Execute().Items) { if (item.Id.Kind == "youtube#channel") { API.Data.SearchResultSnippet channel = item.Snippet; Database.Types.Channel channelInfo = new Database.Types.Channel { ID = channel.ChannelId, Title = channel.Title, Description = channel.Description, ThumbnailURL = GetBestThumbnail(channel.Thumbnails) }; results.Add(channelInfo); LoggingManager.Log.Info($"Information processed for '{channelInfo.ID}'."); } } return(results); } catch (Exception ex) { LoggingManager.Log.Error(ex, $"Failed to search for '{channelName}'."); return(new List <Database.Types.Channel>()); } }