SHARE
TWEET

Batch image downloader by index 0000-9999

MaKiPL Oct 21st, 2015 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4.  
  5. namespace ImageCrawler
  6. {
  7.     class Program
  8.     {
  9.         private const string Prepath = @"http://www.example.com/Images/";
  10.         private const string Name = "Image";
  11.         private const string Extension = ".jpg";
  12.         private const string Out = @"D:\SampleOutput\Image_";
  13.  
  14.         static void Main(string[] args)
  15.         {
  16.             for (int i = 0; i != 1E4; i++)
  17.             {
  18.                 DownloadRemoteImageFile(Prepath+Name+PrepareNumber(i)+Extension, Out+PrepareNumber(i)+Extension, i);
  19.             }
  20.         }
  21.  
  22.         private static string PrepareNumber(int a)
  23.         {
  24.             if (a < 10)
  25.                 return "000" + a.ToString();
  26.             else if (a >= 10 && a < 100)
  27.                 return "00" + a.ToString();
  28.             else if (a >= 100 && a < 1E3)
  29.                 return "0" + a.ToString();
  30.             else if (a >= 1000 && a < 1E4)
  31.                 return a.ToString();
  32.             else //Just in case
  33.                 return a.ToString();
  34.         }
  35.  
  36.  
  37. //Below code: http://stackoverflow.com/a/12631127/4509036
  38.         private static bool DownloadRemoteImageFile(string uri, string fileName, int Debug_i_)
  39.         {
  40.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
  41.             HttpWebResponse response;
  42.             try
  43.             {
  44.                 response = (HttpWebResponse)request.GetResponse();
  45.             }
  46.             catch (Exception)
  47.             {
  48.  
  49.                 Console.WriteLine("NOPE: " + Debug_i_); //FAIL
  50.                 return false;
  51.             }
  52.            
  53.             Console.WriteLine(Debug_i_.ToString()); //SUCCESS
  54.             if(response != null)
  55.             if ((response.StatusCode == HttpStatusCode.OK ||
  56.                 response.StatusCode == HttpStatusCode.Moved ||
  57.                 response.StatusCode == HttpStatusCode.Redirect) &&
  58.                 response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase))
  59.             {
  60.                 using (Stream inputStream = response.GetResponseStream())
  61.                 using (Stream outputStream = File.OpenWrite(fileName))
  62.                 {
  63.                     byte[] buffer = new byte[4096];
  64.                     int bytesRead;
  65.                     do
  66.                     {
  67.                         bytesRead = inputStream.Read(buffer, 0, buffer.Length);
  68.                         outputStream.Write(buffer, 0, bytesRead);
  69.                     } while (bytesRead != 0);
  70.                 }
  71.             }
  72.             return true;
  73.         }
  74.        
  75.     }
  76. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top