Example #1
0
        static void Main(string[] args)
        {
            string          userInput       = "";
            ImageDownloader imageDownloader = new ImageDownloader();

            while (userInput.ToLower() != "exit")
            {
                Console.WriteLine("<url> <filepath> ('exit' to quit)");
                Console.Write(DateTime.Now + " >> ");
                userInput = Console.ReadLine().Trim(' ');
                string[] userParameters = userInput.Split(' ');
                if (userParameters.Length == 2)
                {
                    string url    = userParameters[0];
                    string saveTo = userParameters[1];

                    imageDownloader.DownloadImagesFromUrl(url, saveTo);
                }
                else
                {
                    if (userInput.ToLower() != "exit")
                    {
                        Console.WriteLine("Invalid Parameters.");
                    }
                }
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            string userInput = "";
            ImageDownloader imageDownloader = new ImageDownloader();

            while (userInput.ToLower() != "exit")
            {
                Console.WriteLine("<url> <filepath> ('exit' to quit)");
                Console.Write(DateTime.Now + " >> ");
                userInput = Console.ReadLine().Trim(' ');
                string[] userParameters = userInput.Split(' ');
                if (userParameters.Length == 2)
                {
                    string url = userParameters[0];
                    string saveTo = userParameters[1];

                    imageDownloader.DownloadImagesFromUrl(url, saveTo);

                } else
                {
                    if (userInput.ToLower() != "exit")
                    {
                        Console.WriteLine("Invalid Parameters.");
                    }
                }
            }
        }
Example #3
0
        private void button1_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = null;

            ImageDownloader imageDownloader = new ImageDownloader(textBox1.Text);
            imageDownloader.LoadCompleted += imageDownloader_LoadCompleted;
            imageDownloader.LoadProgressChanged += imageDownloader_LoadProgressChanged;
            imageDownloader.DownloadAsync();
        }
Example #4
0
        static void Main(string[] args)
        {
            for (char c = 'a'; c <= 'z'; ++c)
            {
                alph.Add(c);
            }
            alph.AddRange(Enumerable.Range(1, 9).Select(x => x.ToString()[0]).ToArray());

            HtmlReaderManager hrm = new HtmlReaderManager();
            LimitedConcurrencyLevelTaskScheduler lcts =
                new LimitedConcurrencyLevelTaskScheduler(200);
            TaskFactory factory = new TaskFactory(lcts);
            var         tasks   = new List <Task>();

            int counter = 1;


            foreach (string url in GetNextUrl())
            {
                Options opt = new Options();
                opt.Url     = url;
                opt.counter = counter;

                tasks.Add(factory.StartNew((opts) =>
                {
                    bool isDownloaded = false;
                    int tryCount      = 0;
                    do
                    {
                        Options options  = (Options)opts;
                        string targetUrl = "http://prntscr.com/1" + options.Url;

                        try
                        {
                            hrm.Get(targetUrl);
                            string html = hrm.Html;
                            HtmlAgilityPack.HtmlDocument doc = new HtmlDocument();
                            doc.LoadHtml(html);
                            string imgUrl = doc.DocumentNode.SelectSingleNode("//meta[@name='twitter:image:src']").Attributes["content"].Value;
                            if (!string.IsNullOrEmpty(imgUrl))
                            {
                                ImageDownloader.DownloadRemoteImageFile(imgUrl, "images/" + Guid.NewGuid() + ".jpeg");
                                Console.WriteLine(options.counter + ". Скачиваем " + imgUrl);
                            }
                            else
                            {
                                Console.WriteLine(options.counter + ". " + imgUrl + " пуст");
                            }
                            break;
                        }
                        catch (Exception ex)
                        {
                            tryCount++;
                            if (ex.ToString().StartsWith("System.Net.WebException: The remote server returned an error: (503) Server Unavailable.") && tryCount < 20)
                            {
                                Random s      = new Random();
                                int randomVal = s.Next(1000, 10000);
                                Thread.Sleep(randomVal);
                                Console.WriteLine(options.counter + ". ждём " + targetUrl + " (" + tryCount + " раз)");
                            }
                            else
                            {
                                Console.WriteLine(options.counter + ". ошибка " + targetUrl);
                                isDownloaded = true;
                            }
                        }
                    } while (!isDownloaded);
                }, opt, TaskCreationOptions.LongRunning));

                if (counter % 200 == 0)
                {
                    Task.WaitAll(tasks.ToArray());
                }
                tasks.Clear();
                counter++;
            }

            Console.ReadLine();
        }