Beispiel #1
0
        public IEnumerator <IImageSearchResult> ProcessQuery(string query, int maxResults)
        {
            string baseUri      = "http://www.bing.com/images/async?q=";
            string encodedQuery = HttpUtility.UrlEncode(query);
            var    resultsPage  = Path.GetTempFileName();

            WebDownloader.DownloadRemoteFile(baseUri + encodedQuery, resultsPage, userAgent, null, resultPageTimeout);
            string pageContent = File.ReadAllText(resultsPage);
            int    count       = 0;
            int    perPage     = 35;

            while (pageContent != null)
            {
                var resultEnumerator = ParseResultPage(pageContent);
                int prevCount        = count;
                while (resultEnumerator.MoveNext())
                {
                    ++count;
                    yield return(resultEnumerator.Current);

                    if (count >= maxResults)
                    {
                        yield break;
                    }
                }
                if (count != prevCount && count % perPage == 0 && count < maxResults)
                {
                    try
                    {
                        WebDownloader.DownloadRemoteFile(baseUri + encodedQuery + "&first=" + count + "&count=" + perPage,
                                                         resultsPage, userAgent, null, resultPageTimeout);
                        pageContent = File.ReadAllText(resultsPage);
                    }
                    catch
                    {
                        pageContent = null;
                    }
                }
                else
                {
                    pageContent = null;
                }
            }
        }
Beispiel #2
0
        public void DownloadImage(IImageSearchResult result, DirectoryInfo destination, string filenamePrefix, bool downloadSourcePage)
        {
            var imageUri = result.ImageUri;
            var filename = imageUri.Segments[imageUri.Segments.Length - 1];

            filename = Regex.Replace(filename, "[\\/:*?\"<>]", "_");
            var destinationPath = destination.FullName + Path.DirectorySeparatorChar + filenamePrefix + filename;

            WebDownloader.DownloadRemoteFile(imageUri.AbsoluteUri, destinationPath, null, null, imageDownloadTimeout);

            if (downloadSourcePage)
            {
                var pageUri = result.SourcePageUri;
                destinationPath = destination.FullName + Path.DirectorySeparatorChar + "html";
                if (!Directory.Exists(destinationPath))
                {
                    Directory.CreateDirectory(destinationPath);
                }
                destinationPath += Path.DirectorySeparatorChar + filenamePrefix + filename + ".html";
                WebDownloader.DownloadRemoteFile(pageUri.AbsoluteUri, destinationPath, null, null, imageDownloadTimeout);
            }
        }