Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Net;
- using HtmlAgilityPack;
- using System.IO;
- namespace mlpreactor
- {
- class Program
- {
- static void Main(string[] args)
- {
- ServicePointManager.DefaultConnectionLimit = 10000;
- Console.Write("1. Скачать все картинки\n2. Скачивать по тегам(теги разделяются пробелом)\n\n>Выбор: ");
- int choice = int.Parse(Console.ReadLine());
- Console.Clear();
- if (choice == 1)
- {
- Directory.CreateDirectory("images");
- int maxPage = int.Parse(getMaxPage("http://mlp.reactor.cc/"));
- for (int i = maxPage; i > 1; i--)
- {
- Console.WriteLine(">парсим страницу: " + "http://mlp.reactor.cc/" + i);
- var pictures = getPictureList("http://mlp.reactor.cc/" + i);
- Parallel.ForEach(pictures, pic =>
- {
- var client = new WebClient();
- client.DownloadFile(pic, Path.Combine(Directory.GetCurrentDirectory(), "images", pic.Substring(pic.LastIndexOf('-') + 1)));
- });
- }
- }
- else
- {
- Console.Write(">тег: ");
- string tag = Console.ReadLine();
- Console.Clear();
- Directory.CreateDirectory(tag);
- string encoded = tag.Replace(" ", "%2B");
- int maxPage = int.Parse(getMaxPage("http://mlp.reactor.cc/tag/" + encoded));
- for (int i = maxPage; i > 1; i--)
- {
- Console.WriteLine(">парсим страницу: " + "http://mlp.reactor.cc/tag/" + encoded + "/" + i);
- var pictures = getPictureList("http://mlp.reactor.cc/tag/" + encoded + "/" + i);
- Parallel.ForEach(pictures, pic =>
- {
- var client = new WebClient();
- client.DownloadFile(pic, Path.Combine(Directory.GetCurrentDirectory(), tag, pic.Substring(pic.LastIndexOf('-') + 1)));
- });
- }
- }
- }
- private static string getMaxPage(string url)
- {
- HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
- Stream stream = req.GetResponse().GetResponseStream();
- StreamReader reader = new StreamReader(stream);
- string html = reader.ReadToEnd();
- HtmlDocument doc = new HtmlDocument();
- doc.LoadHtml(html);
- string page = doc.DocumentNode.SelectSingleNode("//./a[@class='next']").Attributes["href"].Value;
- return page.Substring(page.LastIndexOf(@"/") + 1);
- }
- private static List<string> getPictureList(string url)
- {
- List<string> pictures = new List<string>();
- HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
- Stream stream = req.GetResponse().GetResponseStream();
- StreamReader reader = new StreamReader(stream);
- string html = reader.ReadToEnd();
- HtmlDocument doc = new HtmlDocument();
- doc.LoadHtml(html);
- //full-size
- if (html.Contains("prettyPhotoLink"))
- {
- var fullSz = doc.DocumentNode.SelectNodes("//./div[@class='image']/a");
- foreach (var div in fullSz)
- pictures.Add(div.Attributes["href"].Value);
- }
- else
- {
- var stdSz = doc.DocumentNode.SelectNodes("//./div[@class='image']//./img");
- foreach (var div in stdSz)
- pictures.Add(div.Attributes["src"].Value);
- }
- return pictures;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment