Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Http;
  8. using System.Runtime.CompilerServices;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Web;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Input;
  15. using App_for_woobe.Annotations;
  16. using HtmlAgilityPack;
  17. using OpenQA.Selenium;
  18. using OpenQA.Selenium.PhantomJS;
  19. using OpenQA.Selenium.Remote;
  20. using OpenQA.Selenium.Support.Extensions;
  21.  
  22. namespace App_for_woobe
  23. {
  24.     /// <summary>
  25.     /// Логика взаимодействия для MainWindow.xaml
  26.     /// </summary>
  27.     public partial class MainWindow : Window, INotifyPropertyChanged
  28.     {
  29.         private string _category;
  30.         private bool _useProxy;
  31.         private bool _authorizeProxy;
  32.  
  33.         public Dictionary<string, string> CityDictionary => App_for_woobe.Resources.CityDictionary;
  34.         public Dictionary<string, string> CategoryDictionary => App_for_woobe.Resources.CategoryDictionary;
  35.         public Dictionary<string, string> AutoTypeDictionary => App_for_woobe.Resources.AutoTypeDictionary;
  36.         public Dictionary<string, string> MarksDictionary => Category == null || Category == "Авто" ? null : App_for_woobe.Resources.MarksDictionary[Category];
  37.  
  38.         public string City { get; set; }
  39.         public string Category
  40.         {
  41.             get { return _category; }
  42.             set { _category = value;
  43.                 OnPropertyChanged(nameof(AutoVisibility));
  44.                 OnPropertyChanged(nameof(MarksDictionary));
  45.             }
  46.         }
  47.         public string Mark { get; set; }
  48.         public List<string> AutoType { get; set; }
  49.         public string AutoRepairType { get; set; }
  50.         public string AutoMark { get; set; }
  51.        
  52.         public string Phone { get; set; }
  53.         public List<Account> Accounts { get; set; } = new List<Account>();
  54.         public int CommentScore { get; set; }
  55.         public string CommentText { get; set; }
  56.  
  57.         public bool UseProxy
  58.         {
  59.             get { return _useProxy; }
  60.             set { _useProxy = value; OnPropertyChanged(nameof(UseProxy)); }
  61.         }
  62.  
  63.         public List<Proxy> Proxies { get; set; } = new List<Proxy>();
  64.  
  65.         public Visibility AutoVisibility => Category == "Авто" ? Visibility.Visible : Visibility.Collapsed;
  66.  
  67.  
  68.         public event PropertyChangedEventHandler PropertyChanged;
  69.  
  70.         [NotifyPropertyChangedInvocator]
  71.         protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  72.         {
  73.             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  74.         }
  75.  
  76.         public MainWindow()
  77.         {
  78.             DataContext = this;
  79.             InitializeComponent();
  80.         }
  81.  
  82.         private void PortPreviewTextInput(object sender, TextCompositionEventArgs e)
  83.         {
  84.             if (!char.IsDigit(e.Text, e.Text.Length - 1))
  85.                 e.Handled = true;
  86.         }
  87.         private void AccountsClick(object sender, RoutedEventArgs e)
  88.         {
  89.             var win = new AccountsAddWindow() { Accounts = Accounts };
  90.             win.ShowDialog();
  91.             if (win.DialogResult == true)
  92.                 Accounts = win.Accounts;
  93.         }
  94.  
  95.         private void ProxiesClick(object sender, RoutedEventArgs e)
  96.         {
  97.             var win = new ProxyWindow() { Proxies = Proxies};
  98.             win.ShowDialog();
  99.             if(win.DialogResult == true)
  100.                 Proxies = win.Proxies;
  101.         }
  102.         private async void ScanClick(object sender, RoutedEventArgs e)
  103.         {
  104.             if (City == null || Category == null)
  105.             {
  106.                 MessageBox.Show("Город или категория не задана.");
  107.                 return;
  108.             }
  109.             ScanBtn.IsEnabled = false;
  110.             try
  111.             {
  112.                 await Scan();
  113.             }
  114.             catch (Exception ee)
  115.             {
  116.                 MessageBox.Show(ee.ToString(), "Ошибка");
  117.                 return;
  118.             }
  119.             finally
  120.             {
  121.                 ScanBtn.IsEnabled = true;
  122.             }
  123.         }
  124.  
  125.         private IEnumerable<Proxy> ProxyNumerable()
  126.         {
  127.             int i = 0;
  128.             while(true)
  129.                 while (i < Proxies.Count)
  130.                     yield return Proxies[i];
  131.         }
  132.  
  133.         private IEnumerator<Proxy> _proxyNumerator;
  134.         private PhantomJSDriver CreateDriver()
  135.         {
  136.             var service = PhantomJSDriverService.CreateDefaultService(Environment.CurrentDirectory);
  137.             service.HideCommandPromptWindow = true;
  138.             if (UseProxy)
  139.             {
  140.                 _proxyNumerator.MoveNext();
  141.                 var proxy = _proxyNumerator.Current;
  142.                 service.Proxy = $"{proxy.IP}:{proxy.Port}";
  143.                 service.ProxyType = "socks5";
  144.                 if (proxy.Authorize)
  145.                 {
  146.                     service.ProxyAuthentication = $"{proxy.Login}:{proxy.Password}";
  147.                 }
  148.             }
  149.             return new PhantomJSDriver(service);
  150.         }
  151.  
  152.         private async Task Scan(int count = 1)
  153.         {
  154.             var url = $"http://zoon.ru/{CityDictionary[City]}/{CategoryDictionary[Category]}/";
  155.             if (!string.IsNullOrWhiteSpace(Mark))
  156.                 url += $"type/{MarksDictionary[Mark]}/";
  157.             if(UseProxy)
  158.                 _proxyNumerator = ProxyNumerable().GetEnumerator();
  159.             var items = await Task.Factory.StartNew(() =>
  160.             {
  161.                 var dr = CreateDriver();
  162.                 dr.Url = url;
  163.                 var ret = GetItemsURL(dr, count);
  164.                 dr.Dispose();
  165.                 return ret;
  166.             });
  167.             const int taskNum = 10;
  168.             for (var i = 0; i < items.Count + taskNum; i += taskNum)
  169.             {
  170.                 var drivers = new List<PhantomJSDriver>();
  171.                 var tasks = items.Skip(i).Take(taskNum).Select(u => Task.Factory.StartNew(() =>
  172.                 {
  173.                     var dr = CreateDriver();
  174.                     drivers.Add(dr);
  175.                     Spam(dr, u, true, true);
  176.                 }));
  177.  
  178.                 await Task.WhenAll(tasks.ToArray());
  179.                 foreach (var d in drivers)
  180.                 {
  181.                     d.Dispose();
  182.                 }
  183.             }
  184.         }
  185.  
  186.         private HashSet<string> GetItemsURL(PhantomJSDriver driver, int count)
  187.         {
  188.             var items = new HashSet<string>();
  189.             while (items.Count < count)
  190.             {
  191.                 var oldcount = items.Count;
  192.                 items.UnionWith(ParseHtml(driver));
  193.                 if (items.Count != 0 && items.Count == oldcount)
  194.                 {
  195.                     Thread.Sleep(500);
  196.                     continue;
  197.                 }
  198.                 try
  199.                 {
  200.                     var nextbtn = driver.FindElement(By.CssSelector("[class*='js-next-page']"));
  201.                     nextbtn.Click();
  202.                     Thread.Sleep(500);
  203.                 }
  204.                 catch (NoSuchElementException)
  205.                 {
  206.                     break;
  207.                 }
  208.             }
  209.             return new HashSet<string>(items.Take(count));
  210.         }
  211.  
  212.         private static IEnumerable<string> ParseHtml(ISearchContext driver)
  213.         {
  214.             var items = driver.FindElements(By.CssSelector("[class*='service-item pd-m js-results-item']"));
  215.             var ret = new List<string>();
  216.             foreach (var i in items)
  217.             {
  218.                 try
  219.                 {
  220.                     ret.Add(i.FindElement(By.CssSelector("[class*='js-item-url']")).GetAttribute("href"));
  221.                 }
  222.                 catch (NoSuchElementException) { continue; }
  223.             }
  224.             return ret;
  225.         }
  226.        
  227.         private void Spam(RemoteWebDriver driver, string url, bool call, bool comment, Account account, int trycount = 0)
  228.         {
  229.             driver.Url = url;
  230.             if (call)
  231.             {
  232.                 try
  233.                 {
  234.                     var callmeopenbtn =
  235.                         driver.FindElement(
  236.                             By.CssSelector("[class*='button button-primary button-action action-button-target']"));
  237.                     callmeopenbtn.Click();
  238.                     Thread.Sleep(200);
  239.                     bool next = true;
  240.                     try
  241.                     {
  242.                         driver.ExecuteJavaScript($"document.getElementsByName(\"phone\")[0].value = \"{Phone}\"");
  243.                         Thread.Sleep(200);
  244.                     }
  245.                     catch
  246.                     {
  247.                         if (trycount == 5)
  248.                             return;
  249.  
  250.                         Spam(driver, url, true, false, account, trycount++);
  251.                         next = false;
  252.                     }
  253.                     if (next)
  254.                     {
  255.                         var callmebtn =
  256.                             driver.FindElement(
  257.                                 By.CssSelector("[class*='button button-purple button-primary button34 pull-left']"));
  258.                         callmebtn.Click();
  259.                         Thread.Sleep(200);
  260.                     }
  261.                 }
  262.                 catch (NoSuchElementException)
  263.                 {
  264.                 }
  265.             }
  266.             if (comment)
  267.             {
  268.                 try
  269.                 {
  270.                     var cmntbtn = driver.FindElement(By.CssSelector("[class*='button-comment-add']"));
  271.                     cmntbtn.Click();
  272.                     Thread.Sleep(100);
  273.  
  274.                     driver.ExecuteScript($"document.getElementsByName(\"content\")[0].value = \"{CommentText}\"");
  275.                     Thread.Sleep(100);
  276.  
  277.                     var stars =
  278.                         driver.FindElement(By.CssSelector("[class*='comment-form-container']"))
  279.                             .FindElements(By.CssSelector("[class*='star-item']"));
  280.                     stars[CommentScore].Click();
  281.                     Thread.Sleep(200);
  282.  
  283.                     var sendbtn = driver.FindElement(By.CssSelector("[class*='button button-primary pull-left mr10']"));
  284.                     sendbtn.Click();
  285.                     Thread.Sleep(100);
  286.  
  287.                     var loginbtn = driver.FindElement(By.CssSelector("[class*='button-holder-auth zn js-login-zoon']"));
  288.                     sendbtn.Click();
  289.                     Thread.Sleep(100);
  290.  
  291.  
  292.                     driver.ExecuteScript($"document.getElementsByName(\"email\")[0].value = \"{Accounts[0].Login}\"");
  293.                     Thread.Sleep(20);
  294.  
  295.                     driver.ExecuteScript($"document.getElementsByName(\"password\")[0].value = \"{Accounts[0].Password}\"");
  296.                     Thread.Sleep(20);
  297.  
  298.                     driver.ExecuteScript(
  299.                         "document.getElementsByClassName(\"button button-large button-purple middle\")[0].click()");
  300.                     Thread.Sleep(100);
  301.                 }
  302.                 catch (NoSuchElementException)
  303.                 {
  304.  
  305.                 }
  306.             }
  307.         }
  308.  
  309.     }
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement