Beispiel #1
0
        /// <summary>
        /// Gets images from DuckDuckGo.
        /// </summary>
        /// <remarks>This method returns at most 100 image results.</remarks>
        /// <param name="query">The search query.</param>
        /// <param name="safeSearch">The safe search level.</param>
        /// <param name="time">The image time.</param>
        /// <param name="size">The image size.</param>
        /// <param name="color">The image color.</param>
        /// <param name="type">The image type.</param>
        /// <param name="layout">The image layout.</param>
        /// <param name="license">The image license.</param>
        /// <param name="region">The region. <see cref="DuckDuckGoRegions"/> contains the regions that can be used here.</param>
        /// <returns>A task representing the asynchronous operation. The result contains an <see cref="IEnumerable{T}"/> of <see cref="DuckDuckGoImageResult"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="query"/> is null or empty.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="query"/> is larger than <see cref="MaxQueryLength"/>.</exception>
        /// <exception cref="GScraperException">An error occurred during the scraping process.</exception>
        public async Task <IEnumerable <DuckDuckGoImageResult> > GetImagesAsync(string query, SafeSearchLevel safeSearch = SafeSearchLevel.Moderate,
                                                                                DuckDuckGoImageTime time = DuckDuckGoImageTime.Any, DuckDuckGoImageSize size     = DuckDuckGoImageSize.All, DuckDuckGoImageColor color       = DuckDuckGoImageColor.All,
                                                                                DuckDuckGoImageType type = DuckDuckGoImageType.All, DuckDuckGoImageLayout layout = DuckDuckGoImageLayout.All, DuckDuckGoImageLicense license = DuckDuckGoImageLicense.All,
                                                                                string region            = DuckDuckGoRegions.UsEnglish)
        {
            GScraperGuards.NotNull(query, nameof(query));
            GScraperGuards.NotNullOrEmpty(region, nameof(region));
            GScraperGuards.ArgumentInRange(query.Length, MaxQueryLength, nameof(query), $"The query cannot be larger than {MaxQueryLength}.");

            string token = await GetTokenAsync(query);

            var uri = new Uri($"i.js{BuildImageQuery(token, query, safeSearch, time, size, color, type, layout, license, region)}", UriKind.Relative);

            byte[] bytes;
            using (var request = new HttpRequestMessage())
            {
                request.Method     = HttpMethod.Get;
                request.RequestUri = uri;

                if (safeSearch == SafeSearchLevel.Strict)
                {
                    request.Headers.Add("cookie", "p=1");
                }
                else if (safeSearch == SafeSearchLevel.Off)
                {
                    request.Headers.Add("cookie", "p=-2");
                }
                var response = await _httpClient.SendAsync(request).ConfigureAwait(false);

                response.EnsureSuccessStatusCode();

                bytes = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
            }

            var document = JsonDocument.Parse(bytes);
            var results  = document.RootElement.GetPropertyOrDefault("results");

            return(EnumerateResults(results));
        }
Beispiel #2
0
        private static string BuildImageQuery(string token, string query, SafeSearchLevel safeSearch, DuckDuckGoImageTime time, DuckDuckGoImageSize size,
                                              DuckDuckGoImageColor color, DuckDuckGoImageType type, DuckDuckGoImageLayout layout, DuckDuckGoImageLicense license, string region)
        {
            string url = $"?l={region}" +
                         "&o=json" +
                         $"&q={Uri.EscapeDataString(query)}" +
                         $"&vqd={token}" +
                         "&f=";

            url += time == DuckDuckGoImageTime.Any ? ',' : $"time:{time},";
            url += size == DuckDuckGoImageSize.All ? ',' : $"size:{size},";
            url += color == DuckDuckGoImageColor.All ? ',' : $"color:{color.ToString().ToLowerInvariant()},";
            url += type == DuckDuckGoImageType.All ? ',' : $"type:{type},";
            url += layout == DuckDuckGoImageLayout.All ? ',' : $"layout:{layout},";
            url += license == DuckDuckGoImageLicense.All ? "" : $"license:{license}";
            url += $"&p={(safeSearch == SafeSearchLevel.Off ? "-1" : "1")}";

            return(url);
        }