Beispiel #1
0
        private static async Task Main()
        {
            Console.WriteLine("GScraper Example Program");
            var scraper = new GoogleScraper();

            while (true)
            {
                Console.Write("Query (enter \'e\' to exit): ");
                string text = Console.ReadLine();
                if (text == null || text == "e")
                {
                    break;
                }

                Console.Write("Limit?: ");
                if (!int.TryParse(Console.ReadLine(), NumberStyles.Integer, CultureInfo.InvariantCulture, out int limit))
                {
                    continue;
                }

                IReadOnlyList <ImageResult> images;
                try
                {
                    images = await scraper.GetImagesAsync(text, limit).ConfigureAwait(false);
                }
                catch (HttpRequestException e)
                {
                    Console.WriteLine(e);
                    continue;
                }
                catch (GScraperException e)
                {
                    Console.WriteLine(e);
                    continue;
                }

                foreach (var image in images)
                {
                    Console.WriteLine($"Title: {image.Title}");
                    Console.WriteLine($"Link: {image.Link}");
                    Console.WriteLine($"ThumbnailLink: {image.ThumbnailLink}");
                    Console.WriteLine($"ContextLink: {image.ContextLink}");
                    Console.WriteLine($"DisplayLink: {image.DisplayLink}");
                    Console.WriteLine($"Width: {image.Width}");
                    Console.WriteLine($"Height: {image.Height}");
                    Console.WriteLine();
                }
            }

            scraper.Dispose();
        }
Beispiel #2
0
        public static async Task Download(string query, string aspectRatio)
        {
            var scraper = new GoogleScraper();
            IReadOnlyList <ImageResult> images = await scraper.GetImagesAsync(query + " music wallpaper " + aspectRatio, 1).ConfigureAwait(false);

            Console.WriteLine($"Link: {images[0].Link}");
            scraper.Dispose();

            Directory.CreateDirectory("downloads");
            Directory.CreateDirectory("downloads\\" + query);
            using (WebClient webClient = new WebClient())
            {
                webClient.DownloadFile(images[0].Link, "downloads\\" + query + "\\image.jpg");
            }
            Console.WriteLine("Downloaded");
        }