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;
- using System.Net;
- using System.IO;
- using System.Text.RegularExpressions;
- namespace LiveInternet
- {
- public class Category
- {
- public string url;
- public string name;
- }
- public class InfoCategory
- {
- public string countPage;
- public string countSite;
- }
- public class CategoryEventArgs : EventArgs
- {
- public List<Category> listCategory { get; private set; }
- public CategoryEventArgs(List<Category> listCategory)
- {
- this.listCategory = listCategory;
- }
- }
- public class ThreadEventArgs : EventArgs
- {
- public Thread currentThread { get; private set; }
- public ThreadEventArgs(Thread currentThread)
- {
- this.currentThread = currentThread;
- }
- }
- public class LinkEventArgs : EventArgs
- {
- public string linkUrl { get; private set; }
- public Thread currentThread { get; private set; }
- public LinkEventArgs(string linkUrl, Thread currentThread)
- {
- this.linkUrl = linkUrl;
- this.currentThread = currentThread;
- }
- }
- public class InfoEventArgs : EventArgs
- {
- public InfoCategory infoCategory { get; private set; }
- public InfoEventArgs(InfoCategory infoCategory)
- {
- this.infoCategory = infoCategory;
- }
- }
- class Rating
- {
- public Uri URL
- {
- set
- {
- _url = value;
- }
- }
- public int CurrentCategory
- {
- set
- {
- if (value >= 0 && value < _listCategory.Count)
- {
- _currentCategory = value;
- }
- }
- }
- public int CountThread
- {
- set
- {
- if (value > 0)
- {
- _countThread = value;
- }
- else
- {
- _countThread = 1;
- }
- }
- }
- public int WaitTime
- {
- set
- {
- if (value > 0)
- {
- _waitTime = value;
- }
- else
- {
- _waitTime = 0;
- }
- }
- }
- public int CountViews
- {
- set
- {
- if (value > 0)
- {
- _countViews = value;
- }
- else
- {
- _countViews = 1;
- }
- }
- }
- public delegate void CategoryEventHandler(object sender, CategoryEventArgs e);
- public event CategoryEventHandler CategoryReceived;
- public delegate void InfoEventHandler(object sender, InfoEventArgs e);
- public event InfoEventHandler CategoryInformed;
- public event ErrorEventHandler ErrorDetected;
- public delegate void ThreadEventHandler(object sender, ThreadEventArgs e);
- public event ThreadEventHandler ThreadStarted;
- public event ThreadEventHandler ThreadStopped;
- public delegate void LinkEventHandler(object sender, LinkEventArgs e);
- public event LinkEventHandler LinkReceived;
- public event LinkEventHandler LinkSended;
- private Uri _url;
- private int _currentCategory;
- private int _waitTime = 0;
- private int _countThread = 1;
- private int _countViews = 1;
- private List<Category> _listCategory = new List<Category>();
- private List<string> _listUrl = new List<string>();
- private object _lockThread = new object();
- private bool _workThread;
- private bool _stopExecution;
- private Thread catThread;
- private Thread lnkThread;
- private Thread[] multiThread;
- private const int MAXPAGE = 1000000;
- public void Start()
- {
- _workThread = true;
- _stopExecution = false;
- SetLinks(_countThread);
- GetLinks();
- }
- public void Stop()
- {
- _stopExecution = true;
- }
- public void KillAllThread()
- {
- catThread.Abort();
- lnkThread.Abort();
- for (int i = 0; i < _countThread; i++)
- {
- multiThread[i].Abort();
- }
- }
- public void GetCategory()
- {
- catThread = new Thread(new ThreadStart(ThGetCategory));
- catThread.Name = "+";
- catThread.Start();
- if (ThreadStarted != null)
- {
- ThreadStarted(this, new ThreadEventArgs(catThread));
- }
- }
- private void GetLinks()
- {
- lnkThread = new Thread(new ThreadStart(ThGetLinks));
- lnkThread.Name = "*";
- lnkThread.Start();
- if (ThreadStarted != null)
- {
- ThreadStarted(this, new ThreadEventArgs(lnkThread));
- }
- }
- private void SetLinks(int countThread)
- {
- multiThread = new Thread[countThread];
- for (int i = 0; i < multiThread.Length; i++)
- {
- multiThread[i] = new Thread(new ThreadStart(ThSetLinks));
- multiThread[i].Name = Convert.ToString(i);
- multiThread[i].Start();
- if (ThreadStarted != null)
- {
- ThreadStarted(this, new ThreadEventArgs(multiThread[i]));
- }
- }
- }
- private void ThGetCategory()
- {
- try
- {
- HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://www.li.ru/rating/ru/");
- HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
- Stream streamResponse = webResponse.GetResponseStream();
- StreamReader readerResponse = new StreamReader(streamResponse);
- string dataContent = readerResponse.ReadToEnd();
- MatchCollection listMatch = Regex.Matches(dataContent, "option value=\"(.*?)\">(.*?)<");
- lock (this)
- {
- _listCategory.Clear();
- foreach (Match categoryMatch in listMatch)
- {
- Category categoryItem = new Category();
- categoryItem.url = categoryMatch.Groups[1].Value;
- categoryItem.name = categoryMatch.Groups[2].Value;
- _listCategory.Add(categoryItem);
- }
- }
- webResponse.Close();
- streamResponse.Close();
- readerResponse.Close();
- }
- catch (Exception e)
- {
- if (ErrorDetected != null)
- {
- ErrorDetected(this, new ErrorEventArgs(e));
- }
- }
- if (CategoryReceived != null)
- {
- CategoryReceived(this, new CategoryEventArgs(_listCategory));
- }
- if (ThreadStopped != null)
- {
- ThreadStopped(this, new ThreadEventArgs(Thread.CurrentThread));
- }
- }
- private void ThGetLinks()
- {
- int currentPage = MAXPAGE;
- _listUrl.Clear();
- while (currentPage > 0)
- {
- if (_stopExecution)
- {
- if (ThreadStopped != null)
- {
- ThreadStopped(this, new ThreadEventArgs(Thread.CurrentThread));
- }
- Thread.CurrentThread.Abort();
- }
- try
- {
- HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://www.li.ru/rating/" + _listCategory[_currentCategory].url + "/index.html?page=" + currentPage);
- HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
- Stream streamResponse = webResponse.GetResponseStream();
- StreamReader readerResponse = new StreamReader(streamResponse);
- string dataContent = readerResponse.ReadToEnd();
- if (currentPage == MAXPAGE)
- {
- Match matchPage = Regex.Match(dataContent, "<b>([0-9]+)</b>");
- Match matchSite = Regex.Match(dataContent, "Всего: <b>(.*?)</b>");
- InfoCategory infoCategory = new InfoCategory();
- infoCategory.countPage = matchPage.Groups[1].Value;
- infoCategory.countSite = matchSite.Groups[1].Value;
- currentPage = Convert.ToInt32(matchPage.Groups[1].Value);
- if (CategoryInformed != null)
- {
- CategoryInformed(this, new InfoEventArgs(infoCategory));
- }
- }
- MatchCollection listMatch = Regex.Matches(dataContent, "href=\"/go\\?(.*?)\"");
- lock (this)
- {
- foreach (Match matchUrl in listMatch)
- {
- _listUrl.Add(matchUrl.Groups[1].Value);
- if (LinkReceived != null)
- {
- LinkReceived(this, new LinkEventArgs(matchUrl.Groups[1].Value, Thread.CurrentThread));
- }
- }
- }
- webResponse.Close();
- streamResponse.Close();
- readerResponse.Close();
- }
- catch (Exception e)
- {
- if (ErrorDetected != null)
- {
- ErrorDetected(this, new ErrorEventArgs(e));
- }
- }
- currentPage--;
- Thread.Sleep(_waitTime);
- if (currentPage == 0)
- {
- _workThread = false;
- }
- }
- if (ThreadStopped != null)
- {
- ThreadStopped(this, new ThreadEventArgs(Thread.CurrentThread));
- }
- }
- private void ThSetLinks()
- {
- string linkUrl = null;
- Uri generalUrl = _url;
- Random rndNum = new Random();
- int countUrl;
- bool workThread;
- while (true)
- {
- while (true)
- {
- lock (this)
- {
- workThread = _workThread;
- countUrl = _listUrl.Count;
- if (countUrl > 0)
- {
- linkUrl = _listUrl[0];
- _listUrl.RemoveAt(0);
- }
- }
- if (countUrl == 0 && workThread)
- {
- Thread.Sleep(10000);
- }
- else if (countUrl == 0 && workThread == false)
- {
- if (ThreadStopped != null)
- {
- ThreadStopped(this, new ThreadEventArgs(Thread.CurrentThread));
- }
- Thread.CurrentThread.Abort();
- }
- else
- {
- break;
- }
- }
- for (int i = 0; i < _countViews; i++)
- {
- if (_stopExecution)
- {
- if (ThreadStopped != null)
- {
- ThreadStopped(this, new ThreadEventArgs(Thread.CurrentThread));
- }
- Thread.CurrentThread.Abort();
- }
- string totalUrl = "http://counter.yadro.ru/hit?t24.6;r" + Uri.EscapeDataString(generalUrl.AbsoluteUri) + ";s1024*768*32;uhttp%3A//" + linkUrl + ";" + rndNum.NextDouble().ToString().Replace(",", ".");
- try
- {
- HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(totalUrl);
- webRequest.CookieContainer = new CookieContainer();
- webRequest.Referer = "http://" + linkUrl;
- webRequest.Accept = "*/*";
- 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";
- webRequest.Headers.Add("Accept-Encoding", "deflate");
- webRequest.Headers.Add("Accept-Language", "ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4");
- webRequest.Headers.Add("Accept-Charset", "windows-1251,utf-8;q=0.7,*;q=0.3");
- webRequest.GetResponse().Close();
- }
- catch (Exception e)
- {
- if (ErrorDetected != null)
- {
- ErrorDetected(this, new ErrorEventArgs(e));
- }
- }
- Thread.Sleep(_waitTime);
- }
- if (LinkSended != null)
- {
- LinkSended(this, new LinkEventArgs(linkUrl, Thread.CurrentThread));
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment