Beispiel #1
0
        public async Task <ResultSearchDto> SearchText(string param)
        {
            ResultSearchDto result = null;

            try
            {
                _logger.LogInformation("Searching in Bing new");
                long totalResult = 0;
                var  client      = _clientFactory.CreateClient("BingService");
                var  response    = await client.GetAsync($"?q={Uri.EscapeDataString(param)}");

                if (response.IsSuccessStatusCode)
                {
                    var content = await response.Content.ReadAsStringAsync();

                    dynamic data = JsonConvert.DeserializeObject(content);
                    long.TryParse(data?.webPages?.totalEstimatedMatches?.Value?.ToString(), out totalResult);
                }

                result = new ResultSearchDto
                {
                    SearchType  = SearchType.Bing.GetValue(),
                    SearchParam = param,
                    Total       = totalResult
                };
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
            }
            return(result);
        }
Beispiel #2
0
        public async Task <ResultSearchDto> SearchText(string param)
        {
            ResultSearchDto result = null;

            try
            {
                _logger.LogInformation("Searching in Google");
                long totalResult = 0;
                var  apiKey      = _configuration["Searches:Google:apiKey"];
                var  cx          = _configuration["Searches:Google:apiCustomSearch"];
                var  client      = _clientFactory.CreateClient("GoogleService");

                var response = await client.GetAsync($"?key={apiKey}&cx={cx}&q={Uri.EscapeDataString(param)}");

                if (response.IsSuccessStatusCode)
                {
                    var content = await response.Content.ReadAsStringAsync();

                    dynamic data = JsonConvert.DeserializeObject(content);
                    long.TryParse(data?.searchInformation?.totalResults?.Value, out totalResult);
                }

                return(new ResultSearchDto
                {
                    SearchType = SearchType.Google.GetValue(),
                    SearchParam = param,
                    Total = totalResult
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
            }
            return(result);
        }