Пример #1
0
    /// <summary>
    ///     Request to all NSFW endpoints.
    /// </summary>
    /// <param name="count">How many times EACH INDIVIDUAL REQUEST should be made.</param>
    /// <returns>A list of responses to all endpoints.</returns>
    /// <exception cref="ArgumentException">When the count is zero.</exception>
    public async Task <IEnumerable <NekosImage> > RequestAllSfwAsync(uint count = 1)
    {
        if (count == 0)
        {
            throw new ArgumentException("\"count\" must not be zero", nameof(count));
        }

        if (IsLoggingAllowed)
        {
            NekoLogger.LogWarning("Requesting to all SFW endpoints");
        }

        List <NekosImage>         responses      = new();
        IEnumerable <SfwEndpoint> availableFlags = Enum.GetValues <SfwEndpoint>();

        foreach (var endpoint in availableFlags)
        {
            if (endpoint is SfwEndpoint.All or SfwEndpoint.Random)
            {
                continue;
            }

            var dest = endpoint.ToString().ToLower();

            for (var i = 0; i < count; ++i)
            {
                var response = await GetResponse <NekosImage>($"{HostUrl}{MediaRequestUrlSegment}/{dest}").ConfigureAwait(false);

                responses.Add(response);
            }
        }

        return(responses);
    }
Пример #2
0
    /// <summary>
    ///     Get NSFW results.
    /// </summary>
    /// <param name="endpoints">Members of <see cref="NsfwEndpoint" /> enum representing the endpoints.</param>
    /// <param name="count">How many times EACH INDIVIDUAL REQUEST should be made.</param>
    /// <returns>Requested NSFW results.</returns>
    /// <exception cref="ArgumentException">When the count is zero.</exception>
    public async Task <IEnumerable <NekosImage> > RequestNsfwResultsAsync(NsfwEndpoint endpoints, uint count = 1)
    {
        if (count == 0)
        {
            throw new ArgumentException("\"count\" must not be zero", nameof(count));
        }

        List <NekosImage>          responses      = new();
        IEnumerable <NsfwEndpoint> availableFlags = Enum.GetValues <NsfwEndpoint>();

        foreach (var endpoint in availableFlags)
        {
            var isSet = (endpoint & endpoints) != 0;
            if (!isSet)
            {
                continue;
            }

            if (endpoint == NsfwEndpoint.All)
            {
                var images = await RequestAllNsfwAsync(count).ConfigureAwait(false);

                responses.AddRange(images);
                continue;
            }

            NsfwEndpoint tempEndpoint = endpoint;
            while (tempEndpoint == NsfwEndpoint.Random)
            {
                var r = new Random();
                // random is 0
                // so simply ignore it
                var indexPick = r.Next(1, Enum.GetNames(typeof(NsfwEndpoint)).Length - 1);
                tempEndpoint = availableFlags.ToArray()[indexPick];

                if (IsLoggingAllowed)
                {
                    NekoLogger.LogWarning($"Replaced \"Random\" with \"{tempEndpoint.ToString().ToLower()}\"");
                }
            }

            var dest = tempEndpoint.ToString().ToLower();

            // special case
            if (endpoint == NsfwEndpoint.Random_Hentai_Gif)
            {
                dest = "Random_hentai_gif";
            }

            for (var i = 0; i < count; ++i)
            {
                var response = await GetResponse <NekosImage>($"{HostUrl}{MediaRequestUrlSegment}/{dest}").ConfigureAwait(false);

                responses.Add(response);
            }
        }

        return(responses);
    }