Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8. using xNet;
  9.  
  10. namespace GitChecker
  11. {
  12.  
  13.     class Site
  14.     {
  15.         public string Url = null; //URL сайта
  16.         public bool GitExist = false; //существует ли .git
  17.         public bool Git403 = false; //существует, но 403
  18.  
  19.         public Site(string _Url)
  20.         {
  21.             //инициализатор класса
  22.             Url = _Url;
  23.         }
  24.  
  25.         public void GitCheck()
  26.         {
  27.             //проверка на сущетвование .git дериктории
  28.             HttpRequest Request = new HttpRequest();
  29.             Request.IgnoreProtocolErrors = true;
  30.             Request.UserAgent = Http.ChromeUserAgent();
  31.             Request.ConnectTimeout = 10000;
  32.             try
  33.             {
  34.                 HttpResponse Responce = Request.Get(Url + "/.git/config");
  35.                 string Result = Responce.ToString();
  36.  
  37.                 GitExist = Result.Contains("[core]");
  38.  
  39.                 Git403 = (Responce.StatusCode == HttpStatusCode.Forbidden);
  40.  
  41.                 Request.Close();
  42.                 Request.Dispose();
  43.             }
  44.             catch
  45.             {
  46.                 GitExist = false;
  47.             }
  48.            
  49.         }
  50.     }
  51.  
  52.  
  53.     class Checker
  54.     {
  55.         public List<Site> Sites = new List<Site>(); //Сайты
  56.  
  57.         int CheckCount = 0;
  58.         int CheckGood = 0;
  59.         private object Locker = new object();
  60.  
  61.         public void CheckStart()
  62.         {
  63.             //Проверка сайтов из списка
  64.  
  65.             CheckCount = 0;
  66.             CheckGood = 0;
  67.  
  68.             Parallel.For(0, Sites.Count, new ParallelOptions { MaxDegreeOfParallelism = 500 },
  69.                      i =>
  70.                      {
  71.                          int x = i;
  72.  
  73.                          Sites[i].GitCheck();
  74.  
  75.                          if (Sites[i].Git403)
  76.                          {
  77.                              lock (Locker)
  78.                              {
  79.                                  ++CheckCount;
  80.  
  81.                                  Console.ForegroundColor = ConsoleColor.Yellow;
  82.                                  Console.WriteLine(Sites[i].Url + " -> Git Forbidden. [{0}/{1}/{2}]",CheckGood,CheckCount,Sites.Count);
  83.                                  Console.ForegroundColor = ConsoleColor.Gray;
  84.                              }
  85.                          }
  86.                          else
  87.                          {
  88.                              if (Sites[i].GitExist)
  89.                              {
  90.                                  lock (Locker)
  91.                                  {
  92.                                      ++CheckCount;
  93.                                      ++CheckGood;
  94.  
  95.                                  Console.ForegroundColor = ConsoleColor.Green;
  96.                                  Console.WriteLine(Sites[i].Url + " -> Git exist! [{0}/{1}/{2}]",CheckGood,CheckCount,Sites.Count);
  97.                                  Console.ForegroundColor = ConsoleColor.Gray;
  98.                                  }
  99.                              }
  100.                              else
  101.                              {
  102.                                  lock (Locker)
  103.                                  {
  104.                                      ++CheckCount;
  105.  
  106.                                      Console.WriteLine(Sites[i].Url + " -> Git don't exist :( [{0}/{1}/{2}]",CheckGood,CheckCount,Sites.Count);
  107.                                  }
  108.                              }
  109.                          }
  110.                      });
  111.  
  112.  
  113.         }
  114.  
  115.  
  116.         public void SiteAdd(string Url)
  117.         {
  118.             //Добавление сайта в список
  119.             Site N = new Site(Url);
  120.             //N.GitExist = true;
  121.             Sites.Add(N);
  122.         }
  123.  
  124.  
  125.  
  126.         public void AddFromRambler()
  127.         {
  128.             HttpRequest Request = new HttpRequest();
  129.             Request.IgnoreProtocolErrors = true;
  130.             Request.UserAgent = Http.ChromeUserAgent();
  131.             Request.ConnectTimeout = 10000;
  132.             Regex regex = new Regex("\"([^\"]{1,})\"[ \r\n]{1,}class=\"link link_catalogue-site-link\"");
  133.             for (int i = 1; i <= 27; ++i) //to 40
  134.             {
  135.                 HttpResponse Responce = Request.Get("http://top100.rambler.ru/navi/?theme=1166%2F1167&statord=1&page="+i);
  136.                 string Result = Responce.ToString();
  137.                 Match match = regex.Match(Result);
  138.                 while (match.Success)
  139.                 {
  140.                     SiteAdd(match.Groups[1].Value);
  141.                     match = match.NextMatch();
  142.                 }
  143.                 Console.WriteLine("Page #{0} is downloaded...",i);
  144.             }
  145.  
  146.  
  147.             Request.Close();
  148.             Request.Dispose();
  149.         }
  150.  
  151.  
  152.         public void AddFromDump()
  153.         {
  154.             //Добавление сайтов из сырого дампа
  155.  
  156.             string Dump = File.ReadAllText("in.txt");
  157.  
  158.             string[] DumpArray = Dump.Split(new string[] {"\r\n"},StringSplitOptions.RemoveEmptyEntries);
  159.  
  160.             for(int i=0; i<DumpArray.Length;++i)
  161.                 SiteAdd(DumpArray[i]);
  162.  
  163.         }
  164.  
  165.  
  166.         public void SaveResult()
  167.         {
  168.             //Сохранение результата
  169.             string _Out = "";
  170.  
  171.             for (int i = 0; i < Sites.Count;++i )
  172.                 if (Sites[i].GitExist)
  173.                     _Out += Sites[i].Url+"\r\n";
  174.  
  175.             File.WriteAllText("Result.txt", _Out);
  176.         }
  177.  
  178.     }
  179.  
  180.     class Program
  181.     {
  182.         static void Main(string[] args)
  183.         {
  184.             Console.WriteLine("Нажмите Enter, чтобы начать...");
  185.             Console.ReadKey();
  186.  
  187.             Checker Now = new Checker();
  188.            
  189.            
  190.             Now.AddFromDump();
  191.             //Now.AddFromRambler();
  192.  
  193.             Console.WriteLine("Sites count: "+Now.Sites.Count);
  194.  
  195.             Now.CheckStart();
  196.             Now.SaveResult();
  197.  
  198.             Console.WriteLine("Done.");
  199.             Console.ReadKey();
  200.            
  201.         }
  202.     }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement