public async void SearchPhrase(string phrase)
        {
            PageLoader pageLoader = PageLoader.GetPageLoader();
            List <Func <string, Task <ImageSearchResult> > > imageSearchResultsFunc = null;

            for (int i = 0; i < 5; i++)
            {
                var page = await pageLoader.LoadPageAsync($@"https://www.google.com/search?q={phrase}&source=lnms&tbm=isch");

                var imageUrlGenerator = GoogleImageDOMParser.GetUrls(page);
                imageSearchResultsFunc = imageUrlGenerator.ToList();
                if (imageSearchResultsFunc.Count > 0)
                {
                    break;
                }
            }
            if (imageSearchResultsFunc == null)
            {
                throw new InvalidDataException($"After {5} attempts no search results was found!");
            }
            var imageSearchResultTasks = imageSearchResultsFunc
                                         .AsParallel()
                                         .Select(o => o(CacheDirectory))
                                         .ToList();
            //imageSearchResultTasks.ForEach((imageResultTask) =>
            await Task.Factory.StartNew(() =>
            {
                Parallel.ForEach(imageSearchResultTasks, (imageResultTask) =>
                {
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    //Debug.WriteLine($"Started downloading {imageResultTask.Name} from [{imageResultTask.ImageWebUrl}]");
                    try
                    {
                        imageResultTask.Start();
                        //imageResultTask.Start();
                        imageResultTask.Wait(10000);
                        //var filePath = Path.Combine(CacheDirector, $"{imageResultTask.Name}.{imageResultTask.FileExtension}");
                        //webClient.DownloadFile(imageResultTask.ImageWebUrl, filePath);
                        if (imageResultTask.IsCompleted && imageResultTask.Result != null)
                        {
                            lock (searchResultsLock)
                            {
                                var added = imageResultTask.Result;
                                SearchResults.Add(added);
                                //CollectionChanged.BeginInvoke(SearchResults, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, added), (o) => { }, SearchResults);
                            }
                        }
                        Debug.WriteLine($"Finished downloading {imageResultTask.Result.Name} in {watch.ElapsedMilliseconds}ms", "INFO");
                    }
                    catch
                    {
                        Debug.WriteLine("Exception occured");
                    }
                    watch.Stop();
                });
            });

            Debug.WriteLine("All images were downloaded succesfully", "INFO");
        }
Example #2
0
 public static void Main(string[] args)
 {
     Parser.Default.ParseArguments <Options>(args).WithParsed <Options>(o =>
     {
         if (o.ResultDirectory == "pwd")
         {
             o.ResultDirectory = Directory.GetCurrentDirectory();
         }
         if (!Directory.Exists(o.ResultDirectory))
         {
             Directory.CreateDirectory(o.ResultDirectory);
         }
         PageLoader pageLoader = PageLoader.GetPageLoader();
         List <Func <string, Task <ImageSearchResult> > > imageSearchResultFuncs = null;
         foreach (var SearchPhrase in o.SearchPhrases)
         {
             var OutputDirectory = Path.Combine(o.ResultDirectory, SearchPhrase);
             Directory.CreateDirectory(OutputDirectory);
             for (int i = 0; i < o.Attempts; i++)
             {
                 var task = pageLoader.LoadPageAsync($@"https://www.google.com/search?q={SearchPhrase}&source=lnms&tbm=isch");
                 task.Wait();
                 var imageUrlGenerator  = GoogleImageDOMParser.GetUrls(task.Result);
                 imageSearchResultFuncs = imageUrlGenerator.Take(o.NumberOfImages).ToList();
                 if (imageSearchResultFuncs.Count > 0)
                 {
                     break;
                 }
             }
             if (imageSearchResultFuncs == null)
             {
                 throw new InvalidDataException($"After {o.Attempts} attempts no search results was found!");
             }
             var imageSearchResultTasks = imageSearchResultFuncs
                                          .AsParallel()
                                          .Select(m => m(OutputDirectory))
                                          .ToList();
             var a = Parallel.ForEach(imageSearchResultTasks, (imageResultTask) =>
             {
                 Stopwatch watch = new Stopwatch();
                 watch.Start();
                 imageResultTask.Start();
                 imageResultTask.Wait();
                 watch.Stop();
                 Console.WriteLine($"Finished downloading {imageResultTask.Result.Name} in {watch.ElapsedMilliseconds}ms");
             });
         }
         Console.WriteLine("All images were downloaded succesfully");
         Console.ReadKey();
     });
 }