Guest User

LiveInternet

a guest
Jan 10th, 2011
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Net;
  7. using System.IO;
  8. using System.Text.RegularExpressions;
  9.  
  10. namespace LiveInternet
  11. {
  12.     public class Category
  13.     {
  14.         public string url;
  15.         public string name;
  16.     }
  17.  
  18.     public class InfoCategory
  19.     {
  20.         public string countPage;
  21.         public string countSite;
  22.     }
  23.  
  24.     public class CategoryEventArgs : EventArgs
  25.     {
  26.         public List<Category> listCategory { get; private set; }
  27.         public CategoryEventArgs(List<Category> listCategory)
  28.         {
  29.             this.listCategory = listCategory;
  30.         }
  31.     }
  32.  
  33.     public class ThreadEventArgs : EventArgs
  34.     {
  35.         public Thread currentThread { get; private set; }
  36.         public ThreadEventArgs(Thread currentThread)
  37.         {
  38.             this.currentThread = currentThread;
  39.         }
  40.     }
  41.  
  42.     public class LinkEventArgs : EventArgs
  43.     {
  44.         public string linkUrl { get; private set; }
  45.         public Thread currentThread { get; private set; }
  46.         public LinkEventArgs(string linkUrl, Thread currentThread)
  47.         {
  48.             this.linkUrl = linkUrl;
  49.             this.currentThread = currentThread;
  50.         }
  51.     }
  52.  
  53.     public class InfoEventArgs : EventArgs
  54.     {
  55.         public InfoCategory infoCategory { get; private set; }
  56.         public InfoEventArgs(InfoCategory infoCategory)
  57.         {
  58.             this.infoCategory = infoCategory;
  59.         }
  60.     }
  61.  
  62.     class Rating
  63.     {
  64.         public Uri URL
  65.         {
  66.             set
  67.             {
  68.                 _url = value;
  69.             }
  70.         }
  71.  
  72.         public int CurrentCategory
  73.         {
  74.             set
  75.             {
  76.                 if (value >= 0 && value < _listCategory.Count)
  77.                 {
  78.                     _currentCategory = value;
  79.                 }
  80.             }
  81.         }
  82.  
  83.         public int CountThread
  84.         {
  85.             set
  86.             {
  87.                 if (value > 0)
  88.                 {
  89.                     _countThread = value;
  90.                 }
  91.                 else
  92.                 {
  93.                     _countThread = 1;
  94.                 }
  95.             }
  96.         }
  97.  
  98.         public int WaitTime
  99.         {
  100.             set
  101.             {
  102.                 if (value > 0)
  103.                 {
  104.                     _waitTime = value;
  105.                 }
  106.                 else
  107.                 {
  108.                     _waitTime = 0;
  109.                 }
  110.             }
  111.         }
  112.  
  113.         public int CountViews
  114.         {
  115.             set
  116.             {
  117.                 if (value > 0)
  118.                 {
  119.                     _countViews = value;
  120.                 }
  121.                 else
  122.                 {
  123.                     _countViews = 1;
  124.                 }
  125.             }
  126.         }
  127.  
  128.         public delegate void CategoryEventHandler(object sender, CategoryEventArgs e);
  129.         public event CategoryEventHandler CategoryReceived;
  130.         public delegate void InfoEventHandler(object sender, InfoEventArgs e);
  131.         public event InfoEventHandler CategoryInformed;
  132.         public event ErrorEventHandler ErrorDetected;
  133.         public delegate void ThreadEventHandler(object sender, ThreadEventArgs e);
  134.         public event ThreadEventHandler ThreadStarted;
  135.         public event ThreadEventHandler ThreadStopped;        
  136.         public delegate void LinkEventHandler(object sender, LinkEventArgs e);
  137.         public event LinkEventHandler LinkReceived;
  138.         public event LinkEventHandler LinkSended;
  139.  
  140.         private Uri _url;
  141.         private int _currentCategory;
  142.         private int _waitTime = 0;
  143.         private int _countThread = 1;
  144.         private int _countViews = 1;
  145.         private List<Category> _listCategory = new List<Category>();
  146.         private List<string> _listUrl = new List<string>();
  147.         private object _lockThread = new object();
  148.         private bool _workThread;
  149.         private bool _stopExecution;
  150.  
  151.         private Thread catThread;
  152.         private Thread lnkThread;
  153.         private Thread[] multiThread;
  154.  
  155.         private const int MAXPAGE = 1000000;
  156.  
  157.         public void Start()
  158.         {
  159.             _workThread = true;
  160.             _stopExecution = false;
  161.             SetLinks(_countThread);
  162.             GetLinks();
  163.         }
  164.  
  165.         public void Stop()
  166.         {
  167.             _stopExecution = true;
  168.         }
  169.  
  170.         public void KillAllThread()
  171.         {
  172.             catThread.Abort();
  173.             lnkThread.Abort();
  174.             for (int i = 0; i < _countThread; i++)
  175.             {
  176.                 multiThread[i].Abort();
  177.             }
  178.         }
  179.  
  180.         public void GetCategory()
  181.         {
  182.             catThread = new Thread(new ThreadStart(ThGetCategory));
  183.             catThread.Name = "+";
  184.             catThread.Start();
  185.             if (ThreadStarted != null)
  186.             {
  187.                 ThreadStarted(this, new ThreadEventArgs(catThread));
  188.             }
  189.         }
  190.  
  191.         private void GetLinks()
  192.         {
  193.             lnkThread = new Thread(new ThreadStart(ThGetLinks));
  194.             lnkThread.Name = "*";
  195.             lnkThread.Start();
  196.             if (ThreadStarted != null)
  197.             {
  198.                 ThreadStarted(this, new ThreadEventArgs(lnkThread));
  199.             }
  200.         }
  201.  
  202.         private void SetLinks(int countThread)
  203.         {
  204.             multiThread = new Thread[countThread];
  205.             for (int i = 0; i < multiThread.Length; i++)
  206.             {
  207.                 multiThread[i] = new Thread(new ThreadStart(ThSetLinks));
  208.                 multiThread[i].Name = Convert.ToString(i);
  209.                 multiThread[i].Start();
  210.                 if (ThreadStarted != null)
  211.                 {
  212.                     ThreadStarted(this, new ThreadEventArgs(multiThread[i]));
  213.                 }
  214.             }
  215.         }
  216.  
  217.         private void ThGetCategory()
  218.         {
  219.             try
  220.             {
  221.                 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://www.li.ru/rating/ru/");
  222.                 HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
  223.                 Stream streamResponse = webResponse.GetResponseStream();
  224.                 StreamReader readerResponse = new StreamReader(streamResponse);
  225.                 string dataContent = readerResponse.ReadToEnd();
  226.                 MatchCollection listMatch = Regex.Matches(dataContent, "option value=\"(.*?)\">(.*?)<");
  227.                 lock (this)
  228.                 {
  229.                     _listCategory.Clear();
  230.                     foreach (Match categoryMatch in listMatch)
  231.                     {
  232.                         Category categoryItem = new Category();
  233.                         categoryItem.url = categoryMatch.Groups[1].Value;
  234.                         categoryItem.name = categoryMatch.Groups[2].Value;
  235.                         _listCategory.Add(categoryItem);
  236.                     }
  237.                 }
  238.                 webResponse.Close();
  239.                 streamResponse.Close();
  240.                 readerResponse.Close();
  241.             }
  242.             catch (Exception e)
  243.             {
  244.                 if (ErrorDetected != null)
  245.                 {
  246.                     ErrorDetected(this, new ErrorEventArgs(e));
  247.                 }
  248.             }
  249.             if (CategoryReceived != null)
  250.             {
  251.                 CategoryReceived(this, new CategoryEventArgs(_listCategory));
  252.             }
  253.             if (ThreadStopped != null)
  254.             {
  255.                 ThreadStopped(this, new ThreadEventArgs(Thread.CurrentThread));
  256.             }
  257.         }
  258.  
  259.         private void ThGetLinks()
  260.         {
  261.             int currentPage = MAXPAGE;
  262.             _listUrl.Clear();
  263.             while (currentPage > 0)
  264.             {
  265.                 if (_stopExecution)
  266.                 {
  267.                     if (ThreadStopped != null)
  268.                     {
  269.                         ThreadStopped(this, new ThreadEventArgs(Thread.CurrentThread));
  270.                     }
  271.                     Thread.CurrentThread.Abort();
  272.                 }
  273.                 try
  274.                 {
  275.                     HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://www.li.ru/rating/" + _listCategory[_currentCategory].url + "/index.html?page=" + currentPage);
  276.                     HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
  277.                     Stream streamResponse = webResponse.GetResponseStream();
  278.                     StreamReader readerResponse = new StreamReader(streamResponse);
  279.                     string dataContent = readerResponse.ReadToEnd();
  280.                     if (currentPage == MAXPAGE)
  281.                     {
  282.                         Match matchPage = Regex.Match(dataContent, "<b>([0-9]+)</b>");
  283.                         Match matchSite = Regex.Match(dataContent, "Всего: <b>(.*?)</b>");
  284.                         InfoCategory infoCategory = new InfoCategory();
  285.                         infoCategory.countPage = matchPage.Groups[1].Value;
  286.                         infoCategory.countSite = matchSite.Groups[1].Value;
  287.                         currentPage = Convert.ToInt32(matchPage.Groups[1].Value);
  288.                         if (CategoryInformed != null)
  289.                         {
  290.                             CategoryInformed(this, new InfoEventArgs(infoCategory));
  291.                         }
  292.                     }
  293.                     MatchCollection listMatch = Regex.Matches(dataContent, "href=\"/go\\?(.*?)\"");
  294.                     lock (this)
  295.                     {
  296.                         foreach (Match matchUrl in listMatch)
  297.                         {
  298.                             _listUrl.Add(matchUrl.Groups[1].Value);
  299.                             if (LinkReceived != null)
  300.                             {
  301.                                 LinkReceived(this, new LinkEventArgs(matchUrl.Groups[1].Value, Thread.CurrentThread));
  302.                             }
  303.                         }
  304.                     }
  305.                     webResponse.Close();
  306.                     streamResponse.Close();
  307.                     readerResponse.Close();
  308.                 }
  309.                 catch (Exception e)
  310.                 {
  311.                     if (ErrorDetected != null)
  312.                     {
  313.                         ErrorDetected(this, new ErrorEventArgs(e));
  314.                     }
  315.                 }
  316.                 currentPage--;
  317.                 Thread.Sleep(_waitTime);
  318.                 if (currentPage == 0)
  319.                 {
  320.                     _workThread = false;
  321.                 }
  322.             }
  323.             if (ThreadStopped != null)
  324.             {
  325.                 ThreadStopped(this, new ThreadEventArgs(Thread.CurrentThread));
  326.             }
  327.         }
  328.  
  329.         private void ThSetLinks()
  330.         {
  331.             string linkUrl = null;
  332.             Uri generalUrl = _url;
  333.             Random rndNum = new Random();
  334.             int countUrl;
  335.             bool workThread;
  336.             while (true)
  337.             {
  338.                 while (true)
  339.                 {
  340.                     lock (this)
  341.                     {
  342.                         workThread = _workThread;
  343.                         countUrl = _listUrl.Count;
  344.                         if (countUrl > 0)
  345.                         {
  346.                             linkUrl = _listUrl[0];
  347.                             _listUrl.RemoveAt(0);
  348.                         }
  349.                     }
  350.                     if (countUrl == 0 && workThread)
  351.                     {
  352.                         Thread.Sleep(10000);
  353.                     }
  354.                     else if (countUrl == 0 && workThread == false)
  355.                     {                        
  356.                         if (ThreadStopped != null)
  357.                         {
  358.                             ThreadStopped(this, new ThreadEventArgs(Thread.CurrentThread));
  359.                         }
  360.                         Thread.CurrentThread.Abort();
  361.                     }
  362.                     else
  363.                     {
  364.                         break;
  365.                     }
  366.                 }
  367.                 for (int i = 0; i < _countViews; i++)
  368.                 {
  369.                     if (_stopExecution)
  370.                     {
  371.                         if (ThreadStopped != null)
  372.                         {
  373.                             ThreadStopped(this, new ThreadEventArgs(Thread.CurrentThread));
  374.                         }
  375.                         Thread.CurrentThread.Abort();
  376.                     }
  377.                     string totalUrl = "http://counter.yadro.ru/hit?t24.6;r" + Uri.EscapeDataString(generalUrl.AbsoluteUri) + ";s1024*768*32;uhttp%3A//" + linkUrl + ";" + rndNum.NextDouble().ToString().Replace(",", ".");
  378.                     try
  379.                     {
  380.                         HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(totalUrl);
  381.                         webRequest.CookieContainer = new CookieContainer();
  382.                         webRequest.Referer = "http://" + linkUrl;
  383.                         webRequest.Accept = "*/*";
  384.                         webRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10";
  385.                         webRequest.Headers.Add("Accept-Encoding", "deflate");
  386.                         webRequest.Headers.Add("Accept-Language", "ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4");
  387.                         webRequest.Headers.Add("Accept-Charset", "windows-1251,utf-8;q=0.7,*;q=0.3");
  388.                         webRequest.GetResponse().Close();
  389.                     }
  390.                     catch (Exception e)
  391.                     {
  392.                         if (ErrorDetected != null)
  393.                         {
  394.                             ErrorDetected(this, new ErrorEventArgs(e));
  395.                         }
  396.                     }
  397.                     Thread.Sleep(_waitTime);
  398.                 }
  399.                 if (LinkSended != null)
  400.                 {
  401.                     LinkSended(this, new LinkEventArgs(linkUrl, Thread.CurrentThread));
  402.                 }
  403.             }
  404.         }
  405.     }
  406. }
Add Comment
Please, Sign In to add comment