Example #1
0
        /// <summary>Returns a list of categories that can be associated with YouTube videos.</summary>
        List <Category> QueryVideoCategories(Category parentCategory)
        {
            var query = Service.VideoCategories.List("snippet");

            query.RegionCode = regionCode;
            query.Hl         = hl;
            var response = query.Execute();
            var results  = new List <Category>();

            cachedSearchCategories = new Dictionary <string, string>();
            foreach (var item in response.Items)
            {
                if (item.Snippet.Assignable == true)
                {
                    var category = new YouTubeCategory()
                    {
                        Name = item.Snippet.Title, ParentCategory = parentCategory, Kind = YouTubeCategory.CategoryKind.VideoCategory, Id = item.Id
                    };
                    category.Other = (Func <List <VideoInfo> >)(() => QueryCategoryVideos(item.Id));
                    results.Add(category);
                    cachedSearchCategories.Add(item.Snippet.Title, item.Id);
                }
            }
            return(results);
        }
        // Protected methods.
        /// <summary>
        /// An event handler called when a new category has been set.
        /// </summary>
        /// <param name="oldCategory">The old category.</param>
        /// <param name="newCategory">The new category.</param>
        protected virtual void OnSetCategory(YouTubeCategory oldCategory, YouTubeCategory newCategory)
        {
            // If the new and old category are equal, do nothing.
            if (oldCategory == newCategory) return;

            if (newCategory == null)
            {
                this.labelTitle.Text = "No category selected";
                this.tabControl.Visible = false;
            }
            else
            {
                this.labelTitle.Text = newCategory.Label;
                this.textBoxTerm.Text = newCategory.Term;
                this.textBoxLabel.Text = newCategory.Label;
                this.checkBoxAssignable.Checked = newCategory.IsAssignable;
                this.checkBoxDeprecated.Checked = newCategory.IsDeprecated;
                this.listViewBrowsable.Items.Clear();
                if (newCategory.Browsable != null)
                {
                    foreach (string region in newCategory.Browsable)
                    {
                        this.listViewBrowsable.Items.Add(region);
                    }
                }
                this.tabControl.Visible = true;
            }
            this.tabControl.SelectedTab = this.tabPageGeneral;
            if (this.Focused)
            {
                this.textBoxTerm.Select();
                this.textBoxTerm.SelectionStart = 0;
                this.textBoxTerm.SelectionLength = 0;
            }
        }
Example #3
0
        /// <summary>Returns a list of playlists for the given channel.</summary>
        /// <param name="channelId">The channel to use as filter in the query.</param>
        List <Category> QueryChannelPlaylists(YouTubeCategory parentCategory, string channelId, string pageToken = null)
        {
            var query = Service.Playlists.List("snippet, contentDetails");

            if (string.IsNullOrEmpty(channelId))
            {
                query.Mine = true;
            }
            else
            {
                query.ChannelId = channelId;
            }
            query.Hl         = hl;
            query.MaxResults = pageSize;
            query.PageToken  = pageToken;
            var response = query.Execute();
            var results  = new List <Category>();

            if (!string.IsNullOrEmpty(channelId) && pageToken == null && parentCategory.EstimatedVideoCount > 0)
            {
                // before all playlists add a category that will list all uploads of the channel
                results.Add(new YouTubeCategory()
                {
                    Name  = string.Format("{0} {1}", Translation.Instance.UploadsBy, parentCategory.Name),
                    Thumb = parentCategory.Thumb,
                    EstimatedVideoCount = parentCategory.EstimatedVideoCount,
                    ParentCategory      = parentCategory,
                    Kind  = YouTubeCategory.CategoryKind.Channel,
                    Id    = channelId,
                    Other = (Func <List <VideoInfo> >)(() => QuerySearchVideos(null, "videos", parentCategory.Id, null, null, true, null))
                });
            }
            foreach (var item in response.Items)
            {
                if ((long)(item.ContentDetails.ItemCount ?? 0) > 0 || parentCategory.IsMine) // hide empty playlists when not listing the authenticated user's
                {
                    results.Add(new YouTubeCategory()
                    {
                        Name                = item.Snippet.Localized.Title,
                        Description         = item.Snippet.Localized.Description,
                        Thumb               = item.Snippet.Thumbnails != null ? item.Snippet.Thumbnails.High.Url : null,
                        EstimatedVideoCount = (uint)(item.ContentDetails.ItemCount ?? 0),
                        ParentCategory      = parentCategory,
                        Kind                = YouTubeCategory.CategoryKind.Playlist,
                        Id     = item.Id,
                        IsMine = parentCategory.IsMine,
                        Other  = (Func <List <VideoInfo> >)(() => QueryPlaylistVideos(item.Id))
                    });
                }
            }
            if (!string.IsNullOrEmpty(response.NextPageToken))
            {
                results.Add(new NextPageCategory()
                {
                    ParentCategory = parentCategory, Other = (Func <List <Category> >)(() => QueryChannelPlaylists(parentCategory, channelId, response.NextPageToken))
                });
            }
            return(results);
        }
Example #4
0
        /// <summary>Returns a list of the authenticated user's subscriptions (channels).</summary>
        List <Category> QueryMySubscriptions(Category parentCategory, string pageToken = null)
        {
            var query = Service.Subscriptions.List("snippet, contentDetails");

            query.Mine       = true;
            query.MaxResults = pageSize;
            query.PageToken  = pageToken;
            var response = query.Execute();
            var results  = new List <Category>();

            // before all channels add a category that will list all uploads
            results.Add(new YouTubeCategory()
            {
                Name           = "Latest Videos",
                Thumb          = parentCategory.Thumb,
                ParentCategory = parentCategory,
                Kind           = YouTubeCategory.CategoryKind.Other,
                Other          = (Func <List <VideoInfo> >)(() => QueryNewestSubscriptionVideos())
            });

            foreach (var item in response.Items)
            {
                var category = new YouTubeCategory()
                {
                    Name                = item.Snippet.Title,
                    Description         = item.Snippet.Description,
                    Thumb               = item.Snippet.Thumbnails != null ? item.Snippet.Thumbnails.High.Url : null,
                    EstimatedVideoCount = (uint)(item.ContentDetails.TotalItemCount ?? 0),
                    ParentCategory      = parentCategory,
                    HasSubCategories    = true,
                    Kind                = YouTubeCategory.CategoryKind.Channel,
                    Id     = item.Snippet.ResourceId.ChannelId,
                    IsMine = true
                };
                category.Other = (Func <List <Category> >)(() => QueryChannelPlaylists(category, item.Snippet.ResourceId.ChannelId));
                results.Add(category);
            }
            if (!string.IsNullOrEmpty(response.NextPageToken))
            {
                results.Add(new NextPageCategory()
                {
                    ParentCategory = parentCategory, Other = (Func <List <Category> >)(() => QueryMySubscriptions(parentCategory, response.NextPageToken))
                });
            }
            return(results);
        }
Example #5
0
        /// <summary>Returns a list of categories that can be associated with YouTube channels.</summary>
        /// <remarks>
        /// A guide category identifies a category that YouTube algorithmically assigns based on a channel's content or other indicators, such as the channel's popularity.
        /// The list is similar to video categories, with the difference being that a video's uploader can assign a video category but only YouTube can assign a channel category.
        /// </remarks>
        List <Category> QueryGuideCategories(Category parentCategory)
        {
            var query = Service.GuideCategories.List("snippet");

            query.RegionCode = regionCode;
            query.Hl         = hl;
            var response = query.Execute();
            var results  = new List <Category>();

            foreach (var item in response.Items)
            {
                var category = new YouTubeCategory()
                {
                    Name = item.Snippet.Title, HasSubCategories = true, ParentCategory = parentCategory, Kind = YouTubeCategory.CategoryKind.GuideCategory, Id = item.Id
                };
                category.Other = (Func <List <Category> >)(() => QueryChannelsForGuideCategory(category, item.Id));
                results.Add(category);
            }
            return(results);
        }
Example #6
0
        /// <summary>Returns a list of categories for the authenticated user (Watch Later, Watch History, Subscriptions, Playlists)</summary>
        List <Category> QueryUserChannel()
        {
            var query = Service.Channels.List("snippet, contentDetails");

            query.Mine = true;
            var response    = query.Execute();
            var userChannel = response.Items.FirstOrDefault();
            var results     = new List <Category>();

            if (userChannel != null)
            {
                var userName = userChannel.Snippet.Title;
                if (!string.IsNullOrWhiteSpace(userName))
                {
                    userFavoritesPlaylistId = userChannel.ContentDetails.RelatedPlaylists.Favorites;
                    results.Add(new Category()
                    {
                        Name = string.Format("{0}'s {1}", userName, "Watch Later"), Thumb = userChannel.Snippet.Thumbnails.High.Url, Other = (Func <List <VideoInfo> >)(() => QueryPlaylistVideos(userChannel.ContentDetails.RelatedPlaylists.WatchLater))
                    });
                    results.Add(new Category()
                    {
                        Name = string.Format("{0}'s {1}", userName, "Watch History"), Thumb = userChannel.Snippet.Thumbnails.High.Url, Other = (Func <List <VideoInfo> >)(() => QueryPlaylistVideos(userChannel.ContentDetails.RelatedPlaylists.WatchHistory))
                    });

                    var subscriptionsCategory = new Category()
                    {
                        Name = string.Format("{0}'s {1}", userName, Translation.Instance.Subscriptions), Thumb = userChannel.Snippet.Thumbnails.High.Url, HasSubCategories = true
                    };
                    subscriptionsCategory.Other = (Func <List <Category> >)(() => QueryMySubscriptions(subscriptionsCategory));
                    results.Add(subscriptionsCategory);

                    var playlistsCategory = new YouTubeCategory()
                    {
                        Name = string.Format("{0}'s {1}", userName, Translation.Instance.Playlists), Thumb = userChannel.Snippet.Thumbnails.High.Url, HasSubCategories = true, IsMine = true
                    };
                    playlistsCategory.Other = (Func <List <Category> >)(() => QueryChannelPlaylists(playlistsCategory, null));
                    results.Add(playlistsCategory);
                }
            }
            return(results);
        }
Example #7
0
        /// <summary>Returns a list of channels for the given guide category.</summary>
        /// <param name="guideCategoryId">The guide category to use as filter in the query.</param>
        List <Category> QueryChannelsForGuideCategory(Category parentCategory, string guideCategoryId, string pageToken = null)
        {
            var query = Service.Channels.List("snippet, statistics");

            query.CategoryId = guideCategoryId;
            query.Hl         = hl;
            query.MaxResults = pageSize;
            query.PageToken  = pageToken;
            var response = query.Execute();
            var results  = new List <Category>();

            foreach (var item in response.Items)
            {
                var category = new YouTubeCategory()
                {
                    Name                = item.Snippet.Localized.Title,
                    Description         = item.Snippet.Localized.Description,
                    Thumb               = item.Snippet.Thumbnails != null ? item.Snippet.Thumbnails.High.Url : null,
                    EstimatedVideoCount = (uint)(item.Statistics.VideoCount ?? 0),
                    HasSubCategories    = true,
                    ParentCategory      = parentCategory,
                    Kind                = YouTubeCategory.CategoryKind.Channel,
                    Id = item.Id
                };
                category.Other = (Func <List <Category> >)(() => QueryChannelPlaylists(category, item.Id));
                results.Add(category);
            }
            if (!string.IsNullOrEmpty(response.NextPageToken))
            {
                results.Add(new NextPageCategory()
                {
                    ParentCategory = parentCategory, Other = (Func <List <Category> >)(() => QueryChannelsForGuideCategory(parentCategory, guideCategoryId, response.NextPageToken))
                });
            }
            return(results);
        }
Example #8
0
        public override ContextMenuExecutionResult ExecuteContextMenuEntry(Category selectedCategory, VideoInfo selectedItem, ContextMenuEntry choice)
        {
            ContextMenuExecutionResult result = new ContextMenuExecutionResult();

            try
            {
                if (choice.DisplayText == Translation.Instance.AddToFavourites + " (" + Settings.Name + ")")
                {
                    var query = Service.PlaylistItems.Insert(
                        new Google.Apis.YouTube.v3.Data.PlaylistItem()
                    {
                        Snippet = new Google.Apis.YouTube.v3.Data.PlaylistItemSnippet()
                        {
                            Title      = selectedItem.Title,
                            PlaylistId = userFavoritesPlaylistId,
                            ResourceId = new Google.Apis.YouTube.v3.Data.ResourceId()
                            {
                                VideoId = selectedItem.VideoUrl,
                                Kind    = "youtube#video"
                            }
                        }
                    },
                        "snippet");
                    var response = query.Execute();
                    result.ExecutionResultMessage = string.Format("{0} {1}", Translation.Instance.Success, Translation.Instance.AddingToFavorites);
                }
                else if (choice.DisplayText == Translation.Instance.RemoveFromFavorites + " (" + Settings.Name + ")")
                {
                    var query    = Service.PlaylistItems.Delete((selectedItem as YouTubeVideo).PlaylistItemId);
                    var response = query.Execute();
                    result.RefreshCurrentItems = true;
                }
                else if (choice.DisplayText == Translation.Instance.RelatedVideos)
                {
                    base.HasNextPage    = false;
                    nextPageVideosQuery = null;
                    currentVideosTitle  = Translation.Instance.RelatedVideos + " [" + selectedItem.Title + "]";
                    result.ResultItems  = QuerySearchVideos(null, "videos", null, null, (selectedItem as YouTubeVideo).VideoUrl).ConvertAll <SearchResultItem>(v => v as SearchResultItem);
                }
                else if (choice.DisplayText.StartsWith(Translation.Instance.UploadsBy))
                {
                    base.HasNextPage    = false;
                    nextPageVideosQuery = null;
                    currentVideosTitle  = Translation.Instance.UploadsBy + " [" + (selectedItem as YouTubeVideo).ChannelTitle + "]";
                    result.ResultItems  = QuerySearchVideos(null, "videos", (selectedItem as YouTubeVideo).ChannelId, null, null).ConvertAll <SearchResultItem>(v => v as SearchResultItem);
                }
                else if (choice.DisplayText.StartsWith(Translation.Instance.Playlists))
                {
                    var parentCategory = new YouTubeCategory()
                    {
                        Name = Translation.Instance.Playlists + " [" + (selectedItem as YouTubeVideo).ChannelTitle + "]"
                    };
                    parentCategory.SubCategories = QueryChannelPlaylists(parentCategory, (selectedItem as YouTubeVideo).ChannelId);
                    result.ResultItems           = parentCategory.SubCategories.ConvertAll <SearchResultItem>(v => v as SearchResultItem);
                }
                else if (choice.DisplayText == Translation.Instance.RemoveFromPlaylist)
                {
                    var query    = Service.PlaylistItems.Delete((selectedItem as YouTubeVideo).PlaylistItemId);
                    var response = query.Execute();
                    result.RefreshCurrentItems = true;
                    if ((selectedCategory as YouTubeCategory).EstimatedVideoCount != null)
                    {
                        (selectedCategory as YouTubeCategory).EstimatedVideoCount--;
                    }
                }
                else if (choice.DisplayText == Translation.Instance.DeletePlaylist)
                {
                    var query    = Service.Playlists.Delete((selectedCategory as YouTubeCategory).Id);
                    var response = query.Execute();
                    selectedCategory.ParentCategory.SubCategoriesDiscovered = false;
                    result.RefreshCurrentItems = true;
                }
                else if (choice.ParentEntry != null && choice.ParentEntry.DisplayText == Translation.Instance.AddToPlaylist)
                {
                    if (choice.Other == null)
                    {
                        // create new playlist first
                        var query = Service.Playlists.Insert(
                            new Google.Apis.YouTube.v3.Data.Playlist()
                        {
                            Snippet = new Google.Apis.YouTube.v3.Data.PlaylistSnippet()
                            {
                                Title = choice.UserInputText
                            }
                        },
                            "snippet");
                        var response = query.Execute();
                        choice.Other = response.Id;
                    }
                    var queryItem = Service.PlaylistItems.Insert(
                        new Google.Apis.YouTube.v3.Data.PlaylistItem()
                    {
                        Snippet = new Google.Apis.YouTube.v3.Data.PlaylistItemSnippet()
                        {
                            Title      = selectedItem.Title,
                            PlaylistId = choice.Other as string,
                            ResourceId = new Google.Apis.YouTube.v3.Data.ResourceId()
                            {
                                VideoId = selectedItem.VideoUrl,
                                Kind    = "youtube#video"
                            }
                        }
                    },
                        "snippet");
                    var responseItem = queryItem.Execute();
                    // force re-discovery of dynamic subcategories for my playlists category (as either a new catgeory was added or the count changed)
                    var playlistsCategory = Settings.Categories.FirstOrDefault(c => (c is YouTubeCategory) && (c as YouTubeCategory).IsMine && c.Name.EndsWith(Translation.Instance.Playlists));
                    if (playlistsCategory != null)
                    {
                        playlistsCategory.SubCategoriesDiscovered = false;
                    }
                }
            }
            catch (Google.GoogleApiException apiEx)
            {
                throw new OnlineVideosException(string.Format("{0} {1}", apiEx.HttpStatusCode, apiEx.Message));
            }
            catch (Exception ex)
            {
                throw new OnlineVideosException(ex.Message);
            }
            return(result);
        }