Exemple #1
0
        /// <summary>
        /// Gets images from Google Images.
        /// </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="size">The image size.</param>
        /// <param name="color">The image color. <see cref="GoogleImageColors"/> contains the colors that can be used here.</param>
        /// <param name="type">The image type.</param>
        /// <param name="time">The image time.</param>
        /// <param name="license">The image license. <see cref="GoogleImageLicenses"/> contains the licenses that can be used here.</param>
        /// <param name="language">The language code to use. <see cref="GoogleLanguages"/> contains the language codes that can be used here.</param>
        /// <returns>A task representing the asynchronous operation. The result contains an <see cref="IEnumerable{T}"/> of <see cref="GoogleImageResult"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="query"/> is null or empty.</exception>
        /// <exception cref="GScraperException">An error occurred during the scraping process.</exception>
        public async Task <IEnumerable <GoogleImageResult> > GetImagesAsync(string query, SafeSearchLevel safeSearch = SafeSearchLevel.Off, GoogleImageSize size = GoogleImageSize.Any,
                                                                            string?color   = null, GoogleImageType type = GoogleImageType.Any, GoogleImageTime time = GoogleImageTime.Any,
                                                                            string?license = null, string?language      = null)
        {
            GScraperGuards.NotNull(query, nameof(query));

            var uri = new Uri(BuildImageQuery(query, safeSearch, size, color, type, time, license, language), UriKind.Relative);

            string page = await _httpClient.GetStringAsync(uri).ConfigureAwait(false);

            JsonElement rawImages;

            try
            {
                rawImages = ExtractDataPack(page);
            }
            catch (Exception e) when(e is ArgumentOutOfRangeException or JsonException)
            {
                throw new GScraperException("Failed to unpack the image object data.", "Google", e);
            }

            return(EnumerateResults(rawImages));
        }
Exemple #2
0
        private static string BuildImageQuery(string query, SafeSearchLevel safeSearch, GoogleImageSize size, string?color,
                                              GoogleImageType type, GoogleImageTime time, string?license, string?language)
        {
            string url = $"?q={Uri.EscapeDataString(query)}&tbs=";

            url += size == GoogleImageSize.Any ? ',' : $"isz:{(char)size},";
            url += string.IsNullOrEmpty(color) ? ',' : $"ic:{color},";
            url += type == GoogleImageType.Any ? ',' : $"itp:{type.ToString().ToLowerInvariant()},";
            url += time == GoogleImageTime.Any ? ',' : $"qdr:{(char)time},";
            url += string.IsNullOrEmpty(license) ? "" : $"il:{license}";

            url += "&espv=2" +
                   "&biw=1366" +
                   "&bih=667" +
                   "&site=webhp" +
                   "&source=lnms" +
                   "&tbm=isch" +
                   "&sa=X" +
                   "&ei=XosDVaCXD8TasATItgE" +
                   "&ved=0CAcQ_AUoAg";

            url += "&safe=" + safeSearch switch
            {
                SafeSearchLevel.Off => "off",
                SafeSearchLevel.Moderate => "medium",
                SafeSearchLevel.Strict => "high",
                _ => throw new ArgumentException("Invalid safe search level.", nameof(safeSearch))
            };

            if (!string.IsNullOrEmpty(language))
            {
                url += $"&lr=lang_{language}&hl={language}";
            }

            return(url);
        }