Example #1
0
    private Channel Extract(ChannelPageExtractor channelPage)
    {
        var channelId =
            channelPage.TryGetChannelId() ??
            throw new YoutubeExplodeException("Could not extract channel ID.");

        var title =
            channelPage.TryGetChannelTitle() ??
            throw new YoutubeExplodeException("Could not extract channel title.");

        var logoUrl =
            channelPage.TryGetChannelLogoUrl() ??
            throw new YoutubeExplodeException("Could not extract channel logo URL.");

        var logoSize = Regex
                       .Matches(logoUrl, @"\bs(\d+)\b")
                       .Cast <Match>()
                       .LastOrDefault()?
                       .Groups[1]
                       .Value
                       .NullIfWhiteSpace()?
                       .ParseIntOrNull() ?? 100;

        var thumbnails = new[] { new Thumbnail(logoUrl, new Resolution(logoSize, logoSize)) };

        return(new Channel(channelId, title, thumbnails));
    }
    private async ValueTask <ChannelPageExtractor> GetChannelPageAsync(
        string channelRoute,
        CancellationToken cancellationToken = default)
    {
        var url = $"https://www.youtube.com/{channelRoute}?hl=en";

        for (var retry = 0; retry <= 5; retry++)
        {
            var raw = await SendHttpRequestAsync(url, cancellationToken);

            var channelPage = ChannelPageExtractor.TryCreate(raw);
            if (channelPage is not null)
            {
                return(channelPage);
            }
        }

        throw new YoutubeExplodeException(
                  "Channel page is broken. " +
                  "Please try again in a few minutes."
                  );
    }