示例#1
0
        public async ValueTask <MuzlanResponse <IList <SitemapRecord> > > GetVideos(CancellationToken token = default)
        {
            var pageUri    = new Uri($"{_baseUri.DnsSafeHost}/sitemap/videos");
            var pagination = new MuzlanPagination(pageUri, _client, _parser);

            var items = new List <SitemapRecord>();

            try
            {
                await foreach (var page in pagination.FollowForward(token).ConfigureAwait(false))
                {
                    await foreach (var item in EnumerateSitemapItems(page, token).ConfigureAwait(false))
                    {
                        items.Add(item);
                    }
                }

                return(MuzlanResponse <IList <SitemapRecord> > .FromResult(items, pagination.PageUri));
            }
            catch (Exception ex)
            {
                if (pagination.PageNumber == -1)
                {
                    return(MuzlanResponse <IList <SitemapRecord> > .FromException(ex, pagination.PageUri));
                }

                return(MuzlanResponse <IList <SitemapRecord> > .FromPartialResult(items, pagination.PageUri, pagination.PageNumber));
            }
        }
示例#2
0
        public async ValueTask <MuzlanResponse <IList <TrackRecord> > > FindTracks(
            Uri pageUri,
            int?limitItems = null,
            int?limitPages = null,
            CancellationToken cancellationToken = default)
        {
            var pagination = new MuzlanPagination(pageUri, _client, _parser);

            var tracks = new List <TrackRecord>();

            try
            {
                bool reachedLimit = false;
                int  itemCount    = 0;

                await foreach (var pageContent in pagination.FollowForward(cancellationToken).ConfigureAwait(false))
                {
                    reachedLimit = limitPages > 0 && pagination.PageCount >= limitPages;

                    await foreach (var track in EnumerateTracks(pageContent, cancellationToken).ConfigureAwait(false))
                    {
                        tracks.Add(track);

                        itemCount++;

                        reachedLimit = limitItems > 0 && itemCount >= limitItems;
                    }

                    if (reachedLimit)
                    {
                        break;
                    }
                }

                return(MuzlanResponse <IList <TrackRecord> > .FromResult(tracks, pagination.PageUri));
            }
            catch (Exception ex)
            {
                if (pagination.PageNumber == -1)
                {
                    return(MuzlanResponse <IList <TrackRecord> > .FromException(ex, pagination.PageUri, pagination.PageNumber));
                }

                return(MuzlanResponse <IList <TrackRecord> > .FromPartialResult(tracks, pagination.PageUri, pagination.PageNumber));
            }
        }