Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using HtmlAgilityPack;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Threading;
- using System.ComponentModel;
- class Program
- {
- public static int OpenThreads = 0;
- public static int FilesLoaded = 0;
- public static void CloseThread(object sender, AsyncCompletedEventArgs e)
- {
- FilesLoaded++;
- OpenThreads--;
- }
- public static void Main()
- {
- 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";
- HtmlDocument HD = new HtmlDocument();
- var web = new HtmlWeb
- {
- AutoDetectEncoding = false,
- OverrideEncoding = Encoding.UTF8,
- };
- HD = web.Load(htmlBase);
- var nodes = HD.DocumentNode.SelectNodes("//a");
- List<string> folders = new List<string>();
- if (nodes != null)
- {
- foreach (var tag in nodes)
- {
- if (tag.Attributes["href"] != null)
- {
- var link = tag.Attributes["href"].Value;
- if (!link.Contains("liyuans") && !link.Contains("java"))
- folders.Add(link);
- }
- }
- }
- Thread thread = new Thread(() =>
- {
- ReadFolder(folders);
- while (OpenThreads != 0)
- {
- Console.Clear();
- Console.WriteLine("Загружающиеся файлы:" + OpenThreads);
- Console.WriteLine("Загруженные файлы:" + FilesLoaded);
- Thread.Sleep(100);
- }
- });
- thread.Start();
- thread.Join();
- }
- public static void ReadFolder(List<string> folders)
- {
- int i = 0;
- foreach (var folder in folders)
- {
- i++;
- CreateFolder(Convert.ToString(i));
- HtmlDocument hfolder = new HtmlDocument();
- var webimg = new HtmlWeb
- {
- AutoDetectEncoding = false,
- OverrideEncoding = Encoding.UTF8,
- };
- hfolder = webimg.Load("http://storage.liyuans.com/" + folder);
- var imgnodes = hfolder.DocumentNode.SelectNodes("//a");
- if (imgnodes != null)
- {
- int filename = 0;
- foreach (var tag in imgnodes)
- {
- if (tag.Attributes["href"] != null)
- {
- var link = tag.Attributes["href"].Value;
- if (link.Contains("jpg"))
- {
- filename++;
- DownloadFile(filename,link, i);
- }
- }
- }
- }
- }
- }
- public static void DownloadFile(int filename, string link, int i)
- {
- try
- {
- WebClient client = new WebClient();
- client.DownloadFileCompleted += new AsyncCompletedEventHandler(CloseThread);
- client.DownloadFileAsync(new Uri("http://storage.liyuans.com/" + link), Directory.GetCurrentDirectory() + @"\content\" + Convert.ToString(i) + @"\" + filename + ".jpg");
- }
- catch (Exception) { }
- OpenThreads++;
- Console.Clear();
- Console.WriteLine("Загружающиеся файлы:" + OpenThreads);
- Console.WriteLine("Загруженные файлы:" + FilesLoaded);
- Thread.Sleep(5);
- }
- public static void CreateFolder(string name)
- {
- string path = Directory.GetCurrentDirectory();
- DirectoryInfo dirInfo = new DirectoryInfo(path);
- if (!dirInfo.Exists)
- {
- dirInfo.Create();
- }
- dirInfo.CreateSubdirectory(@"content\" + name);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement