Advertisement
MyOnAsSalat

Untitled

Mar 18th, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.91 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.  
  57.     }
  58.     public static void ReadFolder(List<string> folders)
  59.     {
  60.         int i = 0;
  61.         foreach (var folder in folders)
  62.         {
  63.             i++;
  64.             CreateFolder(Convert.ToString(i));          
  65.             HtmlDocument hfolder = new HtmlDocument();
  66.             var webimg = new HtmlWeb
  67.             {
  68.                 AutoDetectEncoding = false,
  69.                 OverrideEncoding = Encoding.UTF8,
  70.             };
  71.             hfolder = webimg.Load("http://storage.liyuans.com/" + folder);
  72.             var imgnodes = hfolder.DocumentNode.SelectNodes("//a");
  73.             if (imgnodes != null)
  74.             {
  75.                 int filename = 0;
  76.                 foreach (var tag in imgnodes)
  77.                 {                  
  78.                     if (tag.Attributes["href"] != null)
  79.                     {
  80.                         var link = tag.Attributes["href"].Value;
  81.  
  82.                         if (link.Contains("jpg"))
  83.                         {
  84.                             filename++;
  85.                             DownloadFile(filename,link, i);
  86.                         }
  87.                     }
  88.                 }
  89.             }
  90.         }
  91.     }
  92.     public static void DownloadFile(int filename, string link, int i)
  93.     {
  94.         try
  95.         {
  96.             WebClient client = new WebClient();
  97.             client.DownloadFileCompleted += new AsyncCompletedEventHandler(CloseThread);
  98.             client.DownloadFileAsync(new Uri("http://storage.liyuans.com/" + link), Directory.GetCurrentDirectory() + @"\content\" + Convert.ToString(i) + @"\" + filename + ".jpg");
  99.         }
  100.         catch (Exception) { }
  101.         OpenThreads++;
  102.         Console.Clear();
  103.         Console.WriteLine("Загружающиеся файлы:" + OpenThreads);
  104.         Console.WriteLine("Загруженные файлы:" + FilesLoaded);
  105.         Thread.Sleep(5);
  106.     }
  107.     public static void CreateFolder(string name)
  108.     {
  109.         string path = Directory.GetCurrentDirectory();
  110.         DirectoryInfo dirInfo = new DirectoryInfo(path);
  111.         if (!dirInfo.Exists)
  112.         {
  113.             dirInfo.Create();
  114.         }
  115.         dirInfo.CreateSubdirectory(@"content\" + name);
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement