Advertisement
MyOnAsSalat

Untitled

Mar 18th, 2018
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.39 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using HtmlAgilityPack;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Net;
  7. using System.Threading;
  8. using System.ComponentModel;
  9.  
  10. class Program
  11. {
  12.     public static int OpenThreads = 0;
  13.     public static int FilesLoaded = 0;
  14.     public static void CloseThread(object sender, AsyncCompletedEventArgs e)
  15.     {
  16.         FilesLoaded++;
  17.         OpenThreads--;
  18.     }
  19.     public static void Main()
  20.     {
  21.         string htmlBase = "http://storage.liyuans.com/?dir=02.%E5%A3%81%E7%BA%B8%2F%E5%9B%A2%E5%AD%90%E5%B0%91%E5%A5%B3";
  22.         HtmlDocument HD = new HtmlDocument();
  23.         var web = new HtmlWeb
  24.         {
  25.             AutoDetectEncoding = false,
  26.             OverrideEncoding = Encoding.UTF8,
  27.         };
  28.         HD = web.Load(htmlBase);
  29.         var nodes = HD.DocumentNode.SelectNodes("//a");
  30.         List<string> folders = new List<string>();
  31.         if (nodes != null)
  32.         {
  33.             foreach (var tag in nodes)
  34.             {
  35.                 if (tag.Attributes["href"] != null)
  36.                 {
  37.                     var link = tag.Attributes["href"].Value;
  38.                     if (!link.Contains("liyuans") && !link.Contains("java"))
  39.                         folders.Add(link);
  40.                 }
  41.             }
  42.         }
  43.         Thread thread = new Thread(() =>
  44.         {
  45.             ReadFolder(folders);
  46.             while (OpenThreads != 0)
  47.             {
  48.                 Console.Clear();
  49.                 Console.WriteLine("Загружающиеся файлы:" + OpenThreads);
  50.                 Console.WriteLine("Загруженные файлы:" + FilesLoaded);
  51.                 Thread.Sleep(100);
  52.             }
  53.         });
  54.         thread.Start();
  55.         thread.Join();
  56.         Console.Clear();
  57.         Console.WriteLine("Все файлы загружены");
  58.         Console.ReadKey();
  59.  
  60.     }
  61.     public static void ReadFolder(List<string> folders)
  62.     {
  63.         int i = 0;
  64.         foreach (var folder in folders)
  65.         {
  66.             try
  67.             {
  68.                 i++;
  69.                 CreateFolder(Convert.ToString(i));
  70.                 HtmlDocument hfolder = new HtmlDocument();
  71.                 var webimg = new HtmlWeb
  72.                 {
  73.                     AutoDetectEncoding = false,
  74.                     OverrideEncoding = Encoding.UTF8,
  75.                 };
  76.                 hfolder = webimg.Load("http://storage.liyuans.com/" + folder);
  77.                 var imgnodes = hfolder.DocumentNode.SelectNodes("//a");
  78.                 if (imgnodes != null)
  79.                 {
  80.                     int filename = 0;
  81.                     foreach (var tag in imgnodes)
  82.                     {
  83.                         if (tag.Attributes["href"] != null)
  84.                         {
  85.                             var link = tag.Attributes["href"].Value;
  86.  
  87.                             if (link.Contains("jpg"))
  88.                             {
  89.                                 try
  90.                                 {
  91.                                     filename++;
  92.                                     DownloadFile(filename, link, i);
  93.                                 }
  94.                                 catch (Exception) { }
  95.                             }
  96.                         }
  97.                     }
  98.                 }
  99.             }
  100.             catch (Exception) { }
  101.         }
  102.     }
  103.     public static void DownloadFile(int filename, string link, int i)
  104.     {
  105.         try
  106.         {
  107.             using (WebClient client = new WebClient())
  108.             {
  109.                 client.DownloadFileCompleted += new AsyncCompletedEventHandler(CloseThread);
  110.                 client.DownloadFileAsync(new Uri("http://storage.liyuans.com/" + link), Directory.GetCurrentDirectory() + @"\content\" + Convert.ToString(i) + @"\" + filename + ".jpg");
  111.             }
  112.     }
  113.         catch (Exception) { }
  114.         OpenThreads++;
  115.         Console.Clear();
  116.         Console.WriteLine("Загружающиеся файлы:" + OpenThreads);
  117.         Console.WriteLine("Загруженные файлы:" + FilesLoaded);
  118.         Thread.Sleep(5);
  119.     }
  120.     public static void CreateFolder(string name)
  121.     {
  122.         string path = Directory.GetCurrentDirectory();
  123.         DirectoryInfo dirInfo = new DirectoryInfo(path);
  124.         if (!dirInfo.Exists)
  125.         {
  126.             dirInfo.Create();
  127.         }
  128.         dirInfo.CreateSubdirectory(@"content\" + name);
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement