private IEnumerable <string> QueryGoogle(string url) { string result = null; for (int i = 0; i < QueryRetries && String.IsNullOrEmpty(result); i++) { try { result = WebUtils.DownloadText(url, false); } catch (WebException exception) { if (exception.Status != WebExceptionStatus.Timeout) { throw exception; } LoggerWriter.WriteStep(Tokens.Info, "Retry", url); } } if (!String.IsNullOrEmpty(result) && result.Contains("dyn.setResults([")) { return(ParseGoogleResponse(result)); } else { return(new List <string>()); } }
public ImageResult Query(string query) { if (!cache.ContainsKey(query)) { ImageResult result = new ImageResult(); string googleQuery = BuildGoogleQuery(query); LoggerWriter.WriteStep(Tokens.Info, "Google query", googleQuery); string[] imageUrls = QueryGoogle(googleQuery).ToArray(); for (int i = 0; i < Math.Min(imageUrls.Length, DownloadRetries); i++) { LoggerWriter.WriteStepIndent(Tokens.Info, "\"" + imageUrls[i] + "\""); } for (int i = 0 ; i < Math.Min(imageUrls.Length, DownloadRetries) && !result.Succeeded ; i++) { string url = imageUrls[i]; LoggerWriter.WriteStep(Tokens.Info, "Download", url); try { byte[] image = WebUtils.DownloadBinary(url); if (!Object.ReferenceEquals(image, null) && image.Length >= MinImageSize && image.Length <= MaxImageSize) { result = new ImageResult(image, url); } } catch (Exception ex) { Logger.WriteLine(Tokens.Exception, ex); } } cache[query] = result; } if (cache.ContainsKey(query) && cache[query].Succeeded) { return(cache[query]); } else { return(null); } }