public async Task <SyndicationFeedFormatter> GetUserFeedAsync(
            string userId,
            string encoding,
            int maxLength,
            bool isPopular)
        {
            Logger.Info($"{nameof(GetUserFeedAsync)} for user {userId} requested");

            var baseAddress = GetBaseAddress();

            const string fields = "items(contentDetails,id,snippet)";
            var          listRequestForUsername = _youtubeService.Channels.List("snippet,contentDetails");

            listRequestForUsername.ForUsername = userId;
            listRequestForUsername.MaxResults  = 1;
            listRequestForUsername.Fields      = fields;

            var listRequestForId = _youtubeService.Channels.List("snippet,contentDetails");

            listRequestForId.Id         = userId;
            listRequestForId.MaxResults = 1;
            listRequestForId.Fields     = fields;

            var channel = (await Task.WhenAll(listRequestForUsername.ExecuteAsync(), listRequestForId.ExecuteAsync())).
                          SelectMany(_ => _.Items).
                          First();

            var arguments = new Arguments(
                channel.ContentDetails.RelatedPlaylists.Uploads,
                encoding,
                maxLength,
                isPopular);
            var cachedFeed = GetFromCache(arguments);

            if (cachedFeed != null)
            {
                return(cachedFeed);
            }

            var feed = new ItunesFeed(
                GetTitle(channel.Snippet.Title, arguments),
                channel.Snippet.Description,
                new Uri(string.Format(_channelUrlFormat, channel.Id)))
            {
                ImageUrl = new Uri(channel.Snippet.Thumbnails.Medium.Url),
                Items    = await GenerateItemsAsync(
                    baseAddress,
                    channel.Snippet.PublishedAt.GetValueOrDefault(),
                    arguments),
            };

            return(SetCache(arguments, GetFormatter(feed)));
        }
        public async Task <SyndicationFeedFormatter> GetPlaylistFeedAsync(
            string playlistId,
            string encoding,
            int maxLength,
            bool isPopular)
        {
            Logger.Info($"{nameof(GetPlaylistFeedAsync)} for playlist {playlistId} requested");

            var baseAddress = GetBaseAddress();

            var arguments = new Arguments(
                playlistId,
                encoding,
                maxLength,
                isPopular);

            var playlistRequest = _youtubeService.Playlists.List("snippet");

            playlistRequest.Id         = playlistId;
            playlistRequest.MaxResults = 1;

            var playlist   = (await playlistRequest.ExecuteAsync()).Items.First();
            var cachedFeed = GetFromCache(arguments);

            if (cachedFeed != null)
            {
                return(cachedFeed);
            }

            var feed = new ItunesFeed(
                GetTitle(playlist.Snippet.Title, arguments),
                playlist.Snippet.Description,
                new Uri(string.Format(_playlistUrlFormat, playlist.Id)))
            {
                ImageUrl = new Uri(playlist.Snippet.Thumbnails.Medium.Url),
                Items    = await GenerateItemsAsync(
                    baseAddress,
                    playlist.Snippet.PublishedAt.GetValueOrDefault(),
                    arguments),
            };

            return(SetCache(arguments, GetFormatter(feed)));
        }
Exemple #3
0
        public async Task <SyndicationFeedFormatter> GetPlaylistFeedAsync(
            string playlistId,
            string encoding,
            int maxLength,
            bool isPopular)
        {
            var baseAddress = GetBaseAddress();

            var arguments = new Arguments(
                playlistId,
                encoding,
                maxLength,
                isPopular);

            if (_feedCache.TryGet(arguments, out var formatter))
            {
                return(formatter);
            }

            var playlistRequest = _youtubeService.Playlists.List("snippet");

            playlistRequest.Id         = playlistId;
            playlistRequest.MaxResults = 1;

            var playlist = (await playlistRequest.ExecuteAsync()).Items.First();

            var feed = new ItunesFeed(
                GetTitle(playlist.Snippet.Title, arguments),
                playlist.Snippet.Description,
                new Uri(string.Format(_playlistUrlFormat, playlist.Id)))
            {
                ImageUrl = new Uri(playlist.Snippet.Thumbnails.Medium.Url),
                Items    = await GenerateItemsAsync(
                    baseAddress,
                    playlist.Snippet.PublishedAt.GetValueOrDefault(),
                    arguments),
            };

            return(CacheFeed(arguments, feed));
        }